Jump to content

Include?


KenHorse

Recommended Posts

I have a file (global.php) that used by many different other files in a project I'm doing. This file contains many things including connecting to a MySQL database as well as loading a php_serial_class.

So at the top of php file that needs these resources, I use

include("global.php");

In one of those other php files, I also have a function that I'm using that calls the php_serial_class:

$read = $serial->readPort();

This throws an error:

PHP Fatal error:  Uncaught Error: Call to a member function readPort() on string in /var/www/controlserial.php:370


Stack trace:
#0 /var/www/controlserial.php(104): checkInput('11111')
#1 {main}
  thrown in /var/www/controlserial.php on line 370
root@mypi:/var/www/#

Line 104 is the call to the function and Line 370 is the actual read call to the class

I assume this error is thrown as the function doesn't know about that class? If so, can I simply also load the include within the function or is there a better way to do it?

Link to comment
Share on other sites

I think we want to see the CheckInput function since that is what you have decided to alter.  Please?

Otherwise, why did you show us that you have modified that call to checkinput?  Still sounds like you need to find where you assigned the string '11111' to $serial.  But now that you are changing the call to checkinput to use a different argument now you are getting an error about using a null variable so you have not assigned anything to that new argument.  

Edited by ginerjm
Link to comment
Share on other sites

function checkInput($stuff){
	$starttime = microtime(true);	
	while($read =="" && $timediff < '5'){	
		$read = $serial->readPort();
		$substring = substr($read,0,1);
		//echo "SubString " . "$substring";
		if($substring =="-"){			
				print"<CENTER><H2>We encountered an error when sending $read". "Please close window and try again</H2></CENTER>";	
				$noDataToSend ="False";	
				exit();		
		}
		$endtime = microtime(true);
		$timediff = $endtime - $starttime;
		if($substring =="+"){   //if ACK received, reset timer
			$starttime = microtime(true);
		}else{
			if($substring !=""){echo "Substring " . "$substring\r\n";}			
		}		
		if($timediff > '4'){
			print"<CENTER><H2>No response from the Station. Please close this window, check your serial connections and try again</H2></CENTER>";	
			$noDataToSend ="False";	
			exit();
		}
		sleep(0.1);	
		
	}//while
} //end function

And the line calling it:

$senddata = "122222";		
$serial->sendMessage("$senddata\r");
checkInput($senddata);	

Yes, I am not currently using the passed parameter ($stuff) but am planning to after I fix this problem

Edited by KenHorse
Link to comment
Share on other sites

OK - I cleaned up your function to be able to make better sense of it (nothing wrong) and this is what I see - Check the CAPS.

function checkInput($stuff)	// WHAT IS $STUFF USED FOR? Not in here.
{
	$starttime = microtime(true);	
	//  WHERE ARE $READ AND $TIMEDIFF DEFINED - NOT IN THIS FUNCTION
	while($read == "" && $timediff < '5')
	{
		$read = $serial->readPort();	// WHERE IS SERIAL DEFINED - NOT IN THIS FUNCTION
		$substring = substr($read,0,1);
		if($substring == "-")
		{			
			print"<CENTER><H2>We encountered an error when sending $read". "Please close window and try again</H2></CENTER>";	
			$noDataToSend = "False";	
			exit();		
		}
		$endtime = microtime(true);
		$timediff = $endtime - $starttime;
		if($substring == "+")
		{   //if ACK received, reset timer
			$starttime = microtime(true);
		}
		else
		{
			if($substring != "")
			{
				echo "Substring " . "$substring\r\n";
			}			
		}		
		if($timediff > '4')
		{
			print"<CENTER><H2>No response from the Station. Please close this window, check your serial connections and try again</H2></CENTER>";	
			$noDataToSend ="False";	
			exit();
		}
		sleep(0.1);	

	}//while
} //end function

Do you understand how functions work with variables?  If one is not defined inside the function it has to be passed into it either as an argument (preferred) or as a global variable.  

Link to comment
Share on other sites

10 minutes ago, ginerjm said:

OK - I cleaned up your function to be able to make better sense of it (nothing wrong) and this is what I see - Check the CAPS.

function checkInput($stuff)	// WHAT IS $STUFF USED FOR? Not in here.
{
	$starttime = microtime(true);	
	//  WHERE ARE $READ AND $TIMEDIFF DEFINED - NOT IN THIS FUNCTION
	while($read == "" && $timediff < '5')
	{
		$read = $serial->readPort();	// WHERE IS SERIAL DEFINED - NOT IN THIS FUNCTION
		$substring = substr($read,0,1);
		if($substring == "-")
		{			
			print"<CENTER><H2>We encountered an error when sending $read". "Please close window and try again</H2></CENTER>";	
			$noDataToSend = "False";	
			exit();		
		}
		$endtime = microtime(true);
		$timediff = $endtime - $starttime;
		if($substring == "+")
		{   //if ACK received, reset timer
			$starttime = microtime(true);
		}
		else
		{
			if($substring != "")
			{
				echo "Substring " . "$substring\r\n";
			}			
		}		
		if($timediff > '4')
		{
			print"<CENTER><H2>No response from the Station. Please close this window, check your serial connections and try again</H2></CENTER>";	
			$noDataToSend ="False";	
			exit();
		}
		sleep(0.1);	

	}//while
} //end function

Do you understand how functions work with variables?  If one is not defined inside the function it has to be passed into it either as an argument (preferred) or as a global variable.  

As my OP said, I'm using php_serial_class, which is loaded in global.php (hence the include):

require_once("php_serial.class.php");

// Let's start the serial class
$serial = new phpSerial;

$comport = "/dev/ttyUSB0";

$serial->deviceSet($comport);

// We can change the baud rate
$serial->confBaudRate(19200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");

// Then we need to open it
$serial->deviceOpen();
//clear out crap
$serial->sendMessage("\r");
$serial->sendMessage("\r");
sleep(0.1);

// Or to read from
//$read = $serial->readPort();

//to send to
//$serial->sendMessage("data to send");

// If you want to change the configuration, the device must be closed
//$serial->deviceClose();
//and then reopened

Also, $stuff is not yet used but I plan on using it in the function after I get passed this issue

As for $timediff, yes. I have not defined it yet. Again, I need to get passed this to clean other things up (I guess I should point out that this code was originally written by someone else long before PHP 7.0 was released. I'm trying to get it functional under 7.3xxx but I am certainly not overly fluent in PHP yet)

Edited by KenHorse
Link to comment
Share on other sites

Well - I told you what is wrong with your function.  You have to fix those issues to get 'past' this error.  Simple as that.  Try passing them into the function when you call it?

As for this latest block of code - I don't see a reference to CheckInput so it means nothing to us.

Link to comment
Share on other sites

No - I hope I'm not wasting your time.  You need to do a little reading up on how to use functions.  And look for references to 'scope' which will explain how variables work in and outside of functions.

PS - when one writes a function it is either to put a block of code into one place so that it can be run multiple times from more than one place so that you don't have to "re-create the wheel".   One good thing to make use of is how a function can return a value when it is done.  Sometimes the function is written to actually produce that value so you need to return it.  Other times it simply does something for you and you should probably return an answer that tells the rest of your code whether the function was successful or not.

Your current code simply calls the function but doesn't return anything.  Sure - it does an exit  with an error message but you might want to re-think that or at the very least understand how they work for future reference.

Currently: 

$senddata = "122222";		
$serial->sendMessage("$senddata\r");
checkInput($senddata);	

you are doing the above.  Note that there is no value coming back from the call to CheckInput.

A better way to design your script would be to complete the function by issuing a "return $answer" line and catch that with the calling line being "$result = checkInput(Ssenddata);"  and then checking that $result to see how to proceed.

 

 

Edited by ginerjm
Link to comment
Share on other sites

this issue has nothing to do with any php version/change. it appears that the checkInput() function definition was just thrown around some main code, based on the number of undefined/uninitialized variables and on the result of the function processing not being returned to the calling code.

the main problem causing the error is here -

$serial->sendMessage("$senddata\r");
checkInput($senddata);

what does supplying $senddata, which contains a string, i.e. the data that was defined to be sent by calling sendMessage(), have to do with calling checkInput? hint: checkInput($serial);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.