Posts: 3
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 ?

Posts: 132
Joined: Thu Jan 12, 2017 3:34 pm

Re: stty hangs from php code

Mon Oct 01, 2018 6:33 pm

I cant really offer any help.. (I'm in my own RPi/PHP serial READ hell of my own currently.. can WRITE every time.. but cant READ anything, using straight USB cable, not GPIO pins of any kind)..

Seems we have a similar project going on.


"That being said.. when I had an exec() command in my PHP script.. and posted (here or perhaps stack overflow?).. I was told that exec() does not exist in Linux/RPi? (not even sure what the means)"

^^ I was wrong... I went back into that thread.. the comment was:

"The "MODE" command does not exist on Linux." not EXEC()...

*I Need to go back and check that out then.. I ran in only once in the terminal... but does this need to be executed each and everytime the PHP script runs/attempts to READ from the port??


I can WRITE to the serial port all day using PHP...... but I can NOT READ to save my life!

I read something about somone having same problems.. and used minicom?..

I installed it and ran it... set the serial port config up.. and left it on/running....

I got '2' successful web responses.... on the 3rd.. minicom 'stole' my data.. and displayed in its own screen.. (not passing it on to the awaiting AJAX callback)...

the 4th time.. minicom was frozen.. didnt display the data in minicom.. didnt pass it along to the awaiting webpage/AJAX listener either...

sub'ing to thread.. in case it yields any insight for my project!

Posts: 1
Joined: Thu Jul 02, 2020 8:29 am

Re: stty hangs from php code

Thu Jul 02, 2020 8:40 am

Did you have any success meanwhile?
Did you try to loop back on the RasPi (i.e. connect TXD and RXD)?
I did it and it worked:
Open a console on the RasPi. Connect another PC to it and open another console via SSH. Start the listening script on one of them (my script will pre-process the incoming string and store the result in either a short file, or a database, where e.g. a webserver can pick it up).
Then, on the other terminal do something like: cat myFile.txt > /dev/serial0
The myFile.txt is any file, or even a specially written one, simulating the expeced input from the external device (your Arduino in your case).
When I did that loopback, I could read the input.
But when I connected the device that I am interested in, I only got sometimes some bytes back.
My current suspicion is, that the RasPi treats some of the input as control characters, instead of just passing them through.
Need to go through all the stty settings ...

Return to “Other programming languages”