1
votes

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?

3
DId you try to run "cat /dev/ttyS1" for example as the PHP user (probably named Apache or www-data)? Does this user have read-access to /dev/ttyS1 ? - Erwin Moller
yes, user has read-access to /dev/ttyS1. I am using com2 port and I can open port and send data to port. But I cant read data from port. Also I try "cat /dev/ttyS1" but cant read anything as well. - cukcuk
Have you checked that your PHP install is configured to report errors and warnings? The class uses warnings to report issues. Try adding trigger_error("logging is working", E_USER_WARNING); at the top of the script - if you don't see it in the output, then it's likely something is failing later and you're suppressing the error message. - symcbean
I have already use trigger_error in php_serial class to check any error or warning. My problem is not about it. My problem is to read data from serial port on linux. I use $read = $serial->readPort(); for reading but sometimes it can read , sometimes it cant - cukcuk
did you ever solve this problem? - thenetimp

3 Answers

3
votes

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

0
votes

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

0
votes

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.