PHP Serial port communication won't give read output
one text
I want to read out a QR reader with PHP. I used the library: https://github.com/Xowap/PHP-Serial to setup communication with a serial port. The device is connected but won't read output. Below my code.
<?php
include 'src/PhpSerial.php';
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float) $usec + (float) $sec);
}
// Let's start the class
$serial = new PhpSerial;
// First we must specify the device. This works on both linux and windows (if
// your linux serial device is /dev/ttyS0 for COM1, etc)
// We can change the baud rate, parity, length, stop bits, flow control
$serial->deviceSet("COM1");
$serial->confBaudRate(115200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");
// Then we need to open it
$serial->deviceOpen();
$read = '';
$theResult = '';
$start = microtime_float();
// To write into
// Or to read from
while ( ($read == '') && (microtime_float() <= $start + 2) ) {
$read = $serial->readPort();
if ($read != '') {
$theResult .= $read;
$read = '';
}
}
$serial->deviceClose();
// If you want to change the configuration, the device must be closed
// We can change the baud rate
// etc...
//
//
/* Notes from Jim :
> Also, one last thing that would be good to document, maybe in example.php:
> The actual device to be opened caused me a lot of confusion, I was
> attempting to open a tty.* device on my system and was having no luck at
> all, until I found that I should actually be opening a cu.* device instead!
> The following link was very helpful in figuring this out, my USB/Serial
> adapter (as most probably do) lacked DTR, so trying to use the tty.* device
> just caused the code to hang and never return, it took a lot of googling to
> realize what was going wrong and how to fix it.
>
> http://lists.apple.com/archives/darwin-dev/2009/Nov/msg00099.html
Riz comment : I've definately had a device that didn't work well when using cu., but worked fine with tty. Either way, a good thing to note and keep for reference when debugging.
*/
At first I thought the problem was in the serial port configuration. But i double checkt everything and it's setup correctly. If i open the serial port with an serial port reader program "Hercules" then it reads a value if i scan an qr code.
Source