Re: [PHP] Serial Comm problem

2012-12-26 Thread Matijn Woudt
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


[PHP] Re: Nested loopa

2012-12-26 Thread Jim Giner
While I fully understand the purpose of the do...while construct, I just 
never get used to seeing it used. (in other langs I had to deal with a 
'repeat...until construct and dis-liked that also).  I pretty much know 
if I'm going to have to deal with a "run at least once" when I'm coding 
and therefore code appropriately, altho I don't know in what ways I've 
handled it at this very moment.


That said - here is how I would ahve done your exercise.  I just find it 
easier to read one's code when the conditions are declared up front 
rather than after reading thru some lines not knowing when or why they run:


";
$a++;
$b = 0;
while ($b <= 10)
{
echo "$indent B = $b";
$b++;
}
}


To me - so much easier to comprehend at first glance.  As soon as my eye 
comes to a block of code starting with a conditional like 'while', I 
readily see what makes it tick.  Again I see the potential usefulness of 
doing it the other way as you did in your example, but in your case 
there wasn't a need for using 'do...while' since you structured it so 
that there was never a case where you had to force the loop to happen 
regardless of conditions.



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php