简体   繁体   中英

php read data from serial port on linux

I try to read serial port on Linux platform using PHP.
But I cant read any data. When I try to read using .net, this time I can read.

I use "php_serial.class.php" class for serial port operations. You can read this class from this link :
here

My code is like this :

<?php  
 include "php_serial.class.php";  

// 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)
$serial->deviceSet("/dev/ttyS1");

// We can change the baud rate, parity, length, stop bits, flow control
$serial->confBaudRate(19200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->confFlowControl("none");

// Then we need to open it
$serial->deviceOpen();  

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

//Determine if a variable is set and is not NULL
if(isset($read)){
   while(1){
       $read = $serial->readPort();
       print_r(" (size ".strlen($read). " ) ");
       for($i = 0; $i < strlen($read); $i++)
       {
          echo ord($read[$i])." ";
       }
       print_r("\n");
       sleep(1);
  }// end while
}// end if  


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

// We can change the baud rate
$serial->confBaudRate(19200);  
?>  

the line "print_r(" (size ".strlen($read). " ) ");" always return zero. What is the reason why I cant read data from serial port?

I am sure you have rsolved this by now, but here is my 2c worth.

You read the serial port twice. Once to check if there is data and then again when there is data. In my experience, reading it once clears the buffer and thus reading it again will yield an empty result.

Just do not read it the second time

Had the same problem. I needed two options set using stty -isig -icanon once they were set the script read no problem.

Hello this is REALLY old but I am currently (still am) working on this, I got the bytes back correctly (remove ord() to read as a string btw).

The reason it is coming through as zero is because of the infinite loop, even if you send something nothing is being returned (so it would seem) Though using your code I managed to get things returned as strings.

I actually entered data the device side into the console...this then returned me what i entered into the device, it appears you would need to fork the process in order to do it 100% your way. The reason it works sometimes is because if you enter a command which returns a few lines it will most likely get some of them.

use screen to connect to the device and then just type random things and hit enter... you will see it appear on your php output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM