By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,882 Members | 2,482 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,882 IT Pros & Developers. It's quick & easy.

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

P: 2
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;
  3.  
  4. $serial->deviceSet("COM1");
  5.  
  6. // We can change the baud rate
  7. $serial->confBaudRate(9600);
  8. $serial->confCharacterLength(8);
  9. $serial->confStopBits(1);
  10. $serial->confParity("none");
  11.  
  12. //$serial->confFlowControl ("none");
  13. // Then we need to open it
  14. $serial->deviceOpen();
  15.  
  16. $serial->sendMessage(chr(0xff).chr(0x02).chr(0x4B)); // Sends with no problem
  17.  
  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
  25.  
  26. $serial->deviceClose();
  27.  
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
Share this Question
Share on Google+
1 Reply


P: 2
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);
  4.  
  5.         if ($this->_dHandle !== false)
  6.         {
  7.             stream_set_blocking($this->_dHandle, 0);
  8.             $this->_dState = SERIAL_DEVICE_OPENED;
  9.             return true;
  10.         }
  11.  
  12.         $this->_dHandle = null;
  13.         trigger_error("Unable to open the device", E_USER_WARNING);
  14.         return false;
  15.     }
  16.  
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.