Bagoro / php-serial

Automatically exported from code.google.com/p/php-serial
0 stars 0 forks source link

missing data reading port #2

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. connect serial port to insteon PLM
2. send PLM data from another script
3. sometimes retrieve incomplete data when watching serial port with following 
code:
require('php_serial.class.php');
$serial = new phpSerial();
$serial->deviceSet("/dev/ttyS0");
$serial->confBaudRate(19200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
do{
    $line = $serial->readPort();
    if($line){
        //unpack() gives me an array.  Then because it's hex, I need to typecast
        //it to string, otherwise I'll have a very big number!
        $hex = (string)join("", unpack("H*", $line));
        do{
            $match = 0;

            $pattern = '/^0262([0-9A-F]{6})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})06/i';
            if(preg_match($pattern, $hex, $matches)){
                $hex = preg_replace($pattern, "", $hex);
                print "PLM ACK'd SD command ${matches[3]} ${matches[4]} sent to ${matches[1]}\n";
                $match = 1;
            }

            $pattern = '/^0250([0-9A-F]{6})([0-9A-F]{6})([0-9A-F]{2})([0-9A-F]{2})([0-9A-F]{2})/i';
            if(preg_match($pattern, $hex, $matches)){
                $hex = preg_replace($pattern, "", $hex);
                print "PLM received SD sent from: ${matches[1]} -> ${matches[2]}; Flag: ${matches[3]} Commands: ${matches[4]} ${matches[5]}\n";
                $match = 1;
            }
        }while($match);
        if($hex){
            print "Line: $hex\n";
        }
    }
    sleep(1);
}while(1);

What is the expected output? What do you see instead?
Expect the following two lines repeated as often as I send data:
PLM ACK'd SD command 13 00 sent to 111111
PLM received SD sent from: 111111 -> aaaaaa; Flag: 21 Commands: 13 00

Get (randomly) missing data:
PLM ACK'd SD command 13 00 sent to 111111
PLM received SD sent from: 111111 -> aaaaaa; Flag: 21 Commands: 13 00
PLM ACK'd SD command 13 00 sent to 111111
PLM ACK'd SD command 13 00 sent to 111111
PLM ACK'd SD command 13 00 sent to 111111
PLM received SD sent from: 111111 -> aaaaaa; Flag: 21 Commands: 13 00

What version of the product are you using? On what operating system?
latest source from this repository.
ClearOS 5.1 - 2.6.18-164.11.1.v5PAE #1 SMP

Please provide any additional information below.
Reading the serial port has improved; I first found an earlier version of this 
class, which was even more sporadic.  This version is much better, but still 
missing data.  I am confident the data should be there, but this class isn't 
capturing it from the port.  Perhaps my logic above is bad; I'd love it to be 
that easy!

Original issue reported on code.google.com by sec...@gmail.com on 14 Jun 2010 at 5:38

GoogleCodeExporter commented 9 years ago
# I reworked the code above as indicated by the changes below.  I did this
# after seeing an updated example in the change log.
# With the changes below (which only change the code above beginning with do{
# and ending with $hex = (string)...)
# -- I am still not capturing all the data from the serial port.
function microtime_float(){
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);
}
do{
    $read = '';
    $theResult = '';
    $start = microtime_float();
    while ( ($read == '') && (microtime_float() <= $start + 0.5) ) {
        $read = $serial->readPort();
        if ($read != '') {
                $theResult .= $read;
                $read = '';
        }
    }
#    $line = $serial->readPort();
    $hex = (string)"";
    if($theResult){
        //unpack() gives me an array.  Then because it's hex, I need to typecast
        //it to string, otherwise I'll have a very big number!
        $hex .= (string)join("", unpack("H*", $theResult));
#Continue with no more changes.

Original comment by sec...@gmail.com on 14 Jun 2010 at 5:58

GoogleCodeExporter commented 9 years ago
Have you had any luck with this? I am very interested because the plmtools that 
I used to use to connect with the PLM no longer seem to work since I upgraded 
to Fedora 13.

Original comment by ccata...@cox.net on 11 Oct 2010 at 5:18

GoogleCodeExporter commented 9 years ago
I had issue with my serial port configuration, specifically that special 
characters were allowed and ending the communication.  Try "-isig -icanon" stty 
flags.

Original comment by cnm3...@gmail.com on 19 Apr 2011 at 9:53

GoogleCodeExporter commented 9 years ago
With regard to my previous comment, this was only needed for linux, haven't 
checked OSX, and they werent required for windows.

Original comment by cnm3...@gmail.com on 20 Apr 2011 at 9:09