On Tue, Dec 25, 2012 at 8:36 PM, Ken Arck wrote:
> I'm pretty much a php newbie but have been programming for years in other
> languages
>
> Anyway...
>
> Using /dev/ttyS0. I need to set the serial port to 9600, send one command
> to an embedded processor board, then change the baud rate to 57600 and wait
> for a response. My code is working up to (and including) sending the
> command (and is being received correctly and acted on by the other device)
> but hangs up while waiting for the device to ACK back.
> --**--**
> -
> //set ttyS0 speed to 9600 to send command
> exec('stty -F /dev/ttyS0 9600');
>
> // first open comport if it exists
> $fp = fopen ("/dev/ttyS0", "w+");
> if (!$fp) {
> die( "Uh-oh. Port not opened.");
> } else {
>}
>
> //send command
> $fw = fwrite($fp, "1*21\r\n");
>
>
> //set ttyS0 speed to 57600 to for bootloader
>exec('stty -F /dev/ttyS0 57600');
>
> // wait for bootloader to send "^" (does not send CR or LF)
>
>echo "Waiting for Bootloader Active Character.\n" ;
>
>
>// Were good to
> here***
>
>
> Do {
> $InData = fgets($fp,1);
> }
> While ($InData != "^") ;
> --**--**-
>
> I have also tried both While ($InData !=="^") ; and While ($InData <>
> "^") ; for the last line but it always hangs in this loop and times out
> after 30 seconds
>
> The ^ is sent by the remove device with \r\n so I assumed telling fgets to
> look for one character should be enough but apparently it isn't.
>
> Any help would be greatly appreciated
>
> Ken
>
You should probably use fgetc if you only need one character.
You could also try $InData = exec('head -c 1 /dev/ttyS0');
- Matijn