KenHorse Posted September 5, 2021 Share Posted September 5, 2021 I have a USB to serial convertor (/etc/ttyUSB0) that I read from, using fgets. I'm looking for a way, that if nothing is received with a few seconds, the script terminates. As in a "serial timeout" Of course, fgets seems to hang forever if it never sees a EOL and I need a way around that Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/ Share on other sites More sharing options...
gw1500se Posted September 5, 2021 Share Posted September 5, 2021 The manual explains stream_set_timeout. Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/#findComment-1589671 Share on other sites More sharing options...
KenHorse Posted September 5, 2021 Author Share Posted September 5, 2021 Doesn't that require that /dev/ttyUSB0 is opened as a socket? I don't know how to do that Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/#findComment-1589672 Share on other sites More sharing options...
gw1500se Posted September 5, 2021 Share Posted September 5, 2021 'fgets' reads until it finds an EOF, newline or reaches a specified number of bytes. You didn't show any code so if none of those conditions occurs in your case it will hang forever. I am guessing what you are trying to accomplish but I suspect you are using the wrong read for a ttyUSB device. You might try minicom instead. Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/#findComment-1589673 Share on other sites More sharing options...
KenHorse Posted September 5, 2021 Author Share Posted September 5, 2021 (edited) Here's the code I'm using, which works just fine as long as serial data is returned: //***NEW SERIAL FUNCTION*** function serial($cmd) { $port = "/dev/ttyUSB0"; $c = exec('stty -F '. $port .' cs8 -parenb -cstopb -echo raw speed 57600'); if(!file_exists($port)) { echo "I am not blocked!"; } else { $f = fopen($port, "w+"); fwrite($f, $cmd."\r"); while(1) { $read = fgets($f); if(strlen($read) >5) { // echo $read; $x = $read; fclose($f); break; } else { fwrite($f, $cmd."\r"); // echo "[Error Resend]\n"; } } } $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(); } return $x; } BUT, if the communicated-with device doesn't respond, obviously fgets hangs. Normally I'd refer to that circumstance as a serial timeout. That's what I'm trying to get around. And I do know minicom but that's not a help in this case Edited September 5, 2021 by KenHorse Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/#findComment-1589676 Share on other sites More sharing options...
gw1500se Posted September 6, 2021 Share Posted September 6, 2021 I don't think there is a way to do what you want with fgets. I don't know why you think mincom won't work. I haven't tried it but perhaps, as you suggested earlier, using sockets might work. Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/#findComment-1589688 Share on other sites More sharing options...
KenHorse Posted September 6, 2021 Author Share Posted September 6, 2021 Are you saying it's possible to call minicom from a script, pass it an argument and have it return data? Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/#findComment-1589690 Share on other sites More sharing options...
gw1500se Posted September 6, 2021 Share Posted September 6, 2021 Yes, by using exec. Quote Link to comment https://forums.phpfreaks.com/topic/313667-timeout-with-fgets/#findComment-1589691 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.