Serial port programming

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nabucodonosor
Forum Newbie
Posts: 8
Joined: Tue Feb 15, 2005 3:22 am
Location: Verona, Italy
Contact:

Serial port programming

Post by nabucodonosor »

I've spent time searching in the web, but it seems that nobody has written php scripts comunicating with a serial controller through COM port.
I want to pass binary strings (in hex format) to the serial port and recieve a binary string in response. The first target is ok, but for the second I encoutered some problems.
The code is something like that:

Code: Select all

$streamSeriale=fopen("COM3:", "w+b");
if(!($streamSeriale)) {
    echo "Errore nell'apertura della porta seriale.";
}
else {
    
    define("LEGGI_TEMP", "010300010001D5CA");
    $hexString = SET_TEMP;
    $binString = pack("H16",$hexString);
    
    fwrite($streamSeriale, $binString);
    fflush($streamSeriale);
    sleep(1);    
    $read_string = '';

    //while ($read_string=="")

    while(!feof($streamSeriale))
        $stringaLetta = fread($streamSeriale,7);
/******************************************
    At this point the script waits indefinitely....
**************************************/    
}

    pack("H*", bin2hex($read_string));
    echo "Valore letto: $read_string";
    */
}
As I said I expect to recieve 7 byte in response of the sent message, but nothing is read! I've tested the same string with a "serial port monitor" and everything goes on...the COM port recieves the right string.
Even though the COM port recieve buffer is limited, there should be a "software buffer" provided by the O.S. (win in this case)... and the data shouldn't be lost!
Note that I've tried to comunicate with my serial modem and in this case the reception works(but in thsi case I recieve AT strings, not binary packets).
I'll thank anybody who has an idea about what could be the problem...this situation is frustrating me!!! Thanks.
Msssimo
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Stab in the dark: does feof on win properly handle COM ports? You may try to put an echo statement into your while(!feof($seriale)) loop to check this....
Post Reply