Edit report at https://bugs.php.net/bug.php?id=49976&edit=1
ID: 49976
Comment by: karenlarsonn023 at outlook dot com
Reported by: rpiccini at software4u dot it
Summary: Can't read anything from serial port
Status: No Feedback
Type: Feature/Change Request
Package: Streams related
Operating System: *
PHP Version: *
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
I honestly have the best ceramic and steel knives for this. But i was wondering which one is better
so i read this review (https://smokekitchen.net/ceramic-vs-steel-knives/) and it cleared everything
up.
Previous Comments:
------------------------------------------------------------------------
[2021-08-29 04:22:08] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
------------------------------------------------------------------------
[2021-08-19 14:27:48] cmb@php.net
Is this still an issue with any of the actively supported PHP
versions[1]?
[1] <https://www.php.net/supported-versions.php>
------------------------------------------------------------------------
[2011-04-22 21:59:22] beekerstudios at gmail dot com
I struggled to find solutions to doing serial with php. I went so far as to do it in python,
which I'm a novice at, but it brought to light why it wasn't working for me in php, my
commands
were just wrong. It's amazing how little documentation on it there is. Someone needs to do a
good
blog post about it. (looks at self)
On to why my actually useful comment...
Sleeping the script between fputs and fgets seems to do the job of resolving this. Yah it stalls
the script a bit, but for the most part it's workable. It just means you can't constantly
send
messages to a device with serial commands.
function sendSerialCommand($port,$command){
mode $port: BAUD=9600 PARITY=N data=8 stop=1 xon=off;
$fp = fopen ($port.":", "r+");
if (!$fp) {
echo "Uh-oh. Port not opened.";
} else {
if($command == "on"){
echo "Turning on.<br />";
$string = "\x2a\x70\x6f\x77\x3d\x6f\x6e\x23";
} else if($command == "off"){
echo "Turning off.<br />";
$string = "\x2a\x70\x6f\x77\x3d\x6f\x66\x66\x23";
} else if($command == "status"){
echo "Checking status.<br />";
$string = "\x2a\x70\x6f\x77\x3d\x3f\x23";
}
fputs ($fp, $string);
sleep(1);
$response="";
while(!feof($fp)){
$response.=stream_get_contents($fp, 2);
$lines=explode("\n",$response);
}
print_r($lines);
fclose ($fp);
}
}
sendSerialCommand("COM3","on")
------------------------------------------------------------------------
[2010-01-15 20:19:37] stash_85 at hotmail dot com
I also have the same problem trying to connect to a gprs modem via serial port.
I can send commands using fputs but the script hangs while trying to read from the resource using
fgets, fread or stream_get_contents...
I think the problem, at least in my case, is that the response doesn't have an end or something
and the fact that the reading limit doesn't work in this case.
The strange thing in my case is that back in 2007 i wrote an sms gateway using a GPRS modem and it
used to work with fgets. The same script today, does not work.
exec('mode com9 baud=9600 data=8 stop=1 parity=n xon=on');
$fd = fopen('COM9', "r+");
if ($fd){
stream_set_timeout($fd, 1);
stream_set_blocking($fd, 0);
fputs($fd, "AT\r");
$response="";
while(!feof($fd)){
$response.=stream_get_contents($fd, 2); // This will not work as it hangs the script here.
// $response.=fread($fd, 2); // This will not work as it hangs the script here.
// $response.=fgets($fd, 2); // This will not work as it hangs the script here.
// $response.=fgets($fd); // This will not work as it hangs the script here. It used to work back
in 2007.
$lines=ereg_replace("\r","",$response);
$lines=explode("\n",$lines);
if (in_array("OK",$lines)) break;
if (in_array("ERROR",$lines)) break;
}
print_r($lines);
fclose($fd);
}
I managed to make it work using the php_dio extension on windows php 5.2.9.
exec('mode com9 baud=9600 data=8 stop=1 parity=n xon=on');
$fd = dio_open('COM9', O_RDWR);
if ($fd){
dio_write($fd, "AT\r");
$response="";
while(1){
$response.=dio_read($fd, 2);
// $response.=dio_read($fd, 5); // This will not work as it hangs the script here.
$lines=ereg_replace("\r","",$response);
$lines=explode("\n",$lines);
if (in_array("OK",$lines)) break;
if (in_array("ERROR",$lines)) break;
}
print_r($lines);
dio_close($fd);
}
RESULT:
Array ( [0] => AT [1] => OK )
I hope i helped a bit with the above script. But please, someone has to resolve this bug. Those
problems make people underestimate PHP...
--
Christos.
------------------------------------------------------------------------
[2009-10-26 11:12:12] rpiccini at software4u dot it
Thanks for reply, but binary mode doesn't help to make it works. On WIN XP and Ubuntu too
nothing's changed.
Anyway, I found a difference during fread instruction between WIN and Ubuntu that I haven't
seen before (doesn't matter if binary mode is activated or not):
[CODE]
$buffer = fread($fp,128);
//$buffer = fgets($fp); // tried also with fgets
[/CODE]
On WIN XP "fread" (and fgets too) hangs (never ending loop), while on UBUNTU $buffer is
always empty (can't read reply from serial port) but the script doesn't loop on fread and
goes (for the rest of my code) correctly to the end.
It seems like "stream_set_blocking($fp,0)" doesn't work at all on WIN.
By the way, I haven't yet solved my problem...
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=49976
--
Edit this bug report at https://bugs.php.net/bug.php?id=49976&edit=1