Posts: 2
Joined: Thu Aug 28, 2014 9:28 pm

stty hangs from php code

Sun Sep 16, 2018 11:51 am

Hi all,
In my PHP project I wrote 2 scripts to read tele-information data from the serial port (gpio 15) /dev/ttyAMA0.
The problem is that I read from 2 different speed sources : one at 9600 bds and the other at 1200 bds. I use a 74HC251 to select the input source via gpio 7.

So, in my PHP scripts I issue commands :

Code: Select all

exec('gpio mode 7 out');
exec('gpio write 7 1');
exec('stty -F /dev/ttyAMA0 1200 sane evenp -isig -icrnl -icanon -crtscts');

Code: Select all

exec('gpio mode 7 out');
exec('gpio write 7 0');
exec('stty -F /dev/ttyAMA0 9600 sane evenp -cstopb -isig -icrnl -crtscts -icanon');
To read informations, I use fopen(...), fread(...).

Code: Select all

$handle = fopen($inputDevice, 'r');
I add a stream timeout to not blocking if there is no data :

Code: Select all

stream_set_blocking($handle, true);
stream_set_timeout($handle, 15);
In the reading loop I read until I get a chr(2) or if a timeout occurred :

Code: Select all

do {
        $char = fread($handle, 1);
        $info = stream_get_meta_data($handle); 
while (! $info['timed_out'] && $char != chr(2))
I can usually execute one time each script and soon after that a script hangs on the stty command. I search a long time for a cause, but don't understand what's wrong for the moment.

When my script hangs and after killing all my waiting processes, I enter manually the command

Code: Select all

stty -F /dev/ttyAMA0 -a
speed 1200 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>; start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V;
discard = ^O; min = 1; time = 0;
parenb -parodd -cmspar cs7 hupcl -cstopb cread clocal -crtscts
-ignbrk brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff -iuclc -ixany imaxbel -iutf8
opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon iexten echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke -flusho -extproc
The command hangs after displaying all those informations. I have to hit CTRL-C to terminate it.

An idea for me ?

Return to “Other programming languages”

Who is online

Users browsing this forum: No registered users and 1 guest