KenHorse Posted December 22, 2021 Share Posted December 22, 2021 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? Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/ Share on other sites More sharing options...
mac_gyver Posted December 22, 2021 Share Posted December 22, 2021 the error means that $serial contains a string, rather than an instance of a class. you would need to determine how a string got assigned to the variable. Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592907 Share on other sites More sharing options...
KenHorse Posted December 22, 2021 Author Share Posted December 22, 2021 How do I do that? Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592909 Share on other sites More sharing options...
requinix Posted December 22, 2021 Share Posted December 22, 2021 You're probably using the $serial variable for something in another file. If you use this outdated pattern of including files then you have to make sure you don't accidentally reuse variables. Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592916 Share on other sites More sharing options...
KenHorse Posted December 22, 2021 Author Share Posted December 22, 2021 outdated? How else do you include other files? (admittedly, I am not overly well versed in PHP but do a lot with VB) Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592918 Share on other sites More sharing options...
requinix Posted December 22, 2021 Share Posted December 22, 2021 What's outdated is not the include() function itself but how you use files and write the code inside them. But first things first: see if you can track down the conflicting $serial variable and change it to be something else. Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592920 Share on other sites More sharing options...
KenHorse Posted December 22, 2021 Author Share Posted December 22, 2021 BTW, PHP version is 7.3.29-1 So I changed the variable in the Function: function checkInput($stuff){ No change PHP Fatal error: Uncaught Error: Call to a member function readPort() on null in /var/www/controlserial.php:371 Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592924 Share on other sites More sharing options...
ginerjm Posted December 22, 2021 Share Posted December 22, 2021 If line 371 is in the checkinput function could you show us that function and indicate that line? Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592925 Share on other sites More sharing options...
KenHorse Posted December 22, 2021 Author Share Posted December 22, 2021 (edited) It's the actual call to the class $read = $serial->readPort(); Edited December 22, 2021 by KenHorse Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592926 Share on other sites More sharing options...
ginerjm Posted December 22, 2021 Share Posted December 22, 2021 (edited) 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 December 22, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592927 Share on other sites More sharing options...
KenHorse Posted December 22, 2021 Author Share Posted December 22, 2021 (edited) 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 December 22, 2021 by KenHorse Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592928 Share on other sites More sharing options...
ginerjm Posted December 22, 2021 Share Posted December 22, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592929 Share on other sites More sharing options...
KenHorse Posted December 22, 2021 Author Share Posted December 22, 2021 (edited) 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 December 22, 2021 by KenHorse Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592930 Share on other sites More sharing options...
ginerjm Posted December 22, 2021 Share Posted December 22, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592931 Share on other sites More sharing options...
KenHorse Posted December 22, 2021 Author Share Posted December 22, 2021 Ok thanks. Guess I'm too much of a noob. Sorry for wasting your time Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592932 Share on other sites More sharing options...
ginerjm Posted December 22, 2021 Share Posted December 22, 2021 (edited) 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 December 22, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592933 Share on other sites More sharing options...
mac_gyver Posted December 22, 2021 Share Posted December 22, 2021 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); Quote Link to comment https://forums.phpfreaks.com/topic/314340-include/#findComment-1592934 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.