0

I have plugged in a serial-USB, and run the command:

stty -F /dev/ttyUSB0 38400

Then I am using PHP to send calls to the serial-USB like:

file_put_contents("/dev/ttyUSB0", chr(6).chr(255).chr(0).chr(0).chr(0).chr(5));

And the call sends ok, and works, but the question is:

  • If I send multiple calls to the serial-USB sometimes (rarely) the call is missed and the action its meant to do is not initiated, is there a way to solve this?

As I think there is block while the serial-USB is being used - if I send many calls at once, if I add usleep(50000); this helps, but does not solve it.

Can I setup the stty so that it puts all calls to it in a Queue or kills the previous call allowing the new one to come, or is there another config other than stty or can do something in the PHP to fix the blocked and unused call?

Thanks for any help.

1

Linux buffers serial input and output. I don't know the buffer size but believe it to be thousands of bytes.

You can use the C tcflush function (man tcflush) to flush the buffer.

TCIFLUSH  flushes data received but not read.
TCOFLUSH  flushes data written but not transmitted.
TCIOFLUSH flushes both

Remember that a serial link will not be error free. Your software should be able to deal with bit errors.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.