471,305 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 software developers and data experts.

Using Serial Port in Linux. Weird problem with receiving data.

A baffling end to my week! I open my serial port on my Debian PC (Debian version 5.0.1) I have a decent Null Modem lead going to another PC (I'll call it PC#2), with its port open with the same parameters. I send a series of bytes from the Debian PC which comes through, no problem.

I reply from PC#2, but the read buffer of the Debian PC's port shows nothing. What's more, the message gets sent directly back to the PC#2, with certain bytes getting changed. (I'm guessing its just invalid ascii codes that get changed, e.g. 0x02 always gets changed to 0x5e 0x42, which is ^B. Displayable bytes don't get changed.)

I've tried changing the Flow control method but that hasn't helped.

I'm using php_serial.class.php, from http://www.phpclasses.org/browse/package/3679.html. It is a highly rated class and I'm guessing its not to blame.
My code is a very slight modification of the example that comes with the class (modifying just so it keeps reading any messages for a number of seconds):

Expand|Select|Wrap|Line Numbers
  1. include "php_serial.class.php";
  2. $serial = new phpSerial;
  4. $serial->deviceSet("COM1");
  6. // We can change the baud rate
  7. $serial->confBaudRate(9600);
  8. $serial->confCharacterLength(8);
  9. $serial->confStopBits(1);
  10. $serial->confParity("none");
  12. //$serial->confFlowControl ("none");
  13. // Then we need to open it
  14. $serial->deviceOpen();
  16. $serial->sendMessage(chr(0xff).chr(0x02).chr(0x4B)); // Sends with no problem
  18. do{
  19.     // Or to read from
  20.     $read = $serial->readPort();
  21.     echo $read;
  22.     $pause1 += 0.01;
  23. } while (empty($read) && $pause1 < 99999);
  24. // Read never becomes non-empty whatever I send back from other PC within this time
  26. $serial->deviceClose();
If anyone has a strong hunch about why I'm getting this problem, or any thing helpful to say, thank you very much.
Oct 9 '09 #1
1 5402
I ought to have included this from the actual class that I downloaded. I now think that this is where the problem is:

Expand|Select|Wrap|Line Numbers
  1.         function deviceOpen ($mode = "r+b")
  2.     {
  3.         $this->_dHandle = fopen($this->_device, $mode);
  5.         if ($this->_dHandle !== false)
  6.         {
  7.             stream_set_blocking($this->_dHandle, 0);
  8.             $this->_dState = SERIAL_DEVICE_OPENED;
  9.             return true;
  10.         }
  12.         $this->_dHandle = null;
  13.         trigger_error("Unable to open the device", E_USER_WARNING);
  14.         return false;
  15.     }
I'm pretty sure now that the problem is use of the function stream_set_blocking(). According to user comments on the manual, this function is ignored by php5.2. So it seems the serial port cannot be unblocked - hence data coming in not getting read and getting sent straight back down the line.

Is my only solution to use php4? Anyone know a workaround for the stream_set_blocking() bug?
Oct 15 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Quentin | last post: by
9 posts views Thread by Hal Vaughan | last post: by
4 posts views Thread by Xavier | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.