-   Programming (
-   -   bash: serial port write THEN read (

Metermon 08-11-2010 07:17 AM

bash: serial port write THEN read
Hi folks,

I need to communicate with a target device using serial port and want to access it with bash commands solely. I poll the device by sending a string and then wait for a response string.

Setting up the connection using stty works fine and I can send messages to the device simply by "abcdef > /dev/ttyS2", which are well received. I'm facing a problem when trying a WRITE/READ operation:


abcdef > /dev/ttyS2 ; read -n20 RESPONSE < /dev/ttyS2 ; echo $RESPONSE
This way I'll end up stuck waiting for a response, although I know that my target device sends more than 20 characters.

It works, if I send the message and put a breakpoint in my device before it responds. That way "read -n20 ..." will receive the 20 characters perfectly.

I can only guess that the read command is executed too late.
Unfortunately I can't get into read mode before sending, since then I'm told "Permission denied".
Is there any way to open /dev/ttyS2 for both writing and reading? Or can I redirect input buffering in a way, that no messages will get lost?

Appreciating your suggestions.

theNbomr 08-11-2010 09:05 AM

Your problem most likely has to do with the way serial ports tend to be configured by default. It is waiting for a line terminated by an end-of-line character(s). You probably want to get it into raw mode.

stty -F /dev/ttyS2 raw
You may also need to adjust the permissions to allow non-root access. This can probably be made automatic at boot time using appropriate udev rules.

I would consider bash to be a poor choice for developing serial communications applications.

--- rod.

All times are GMT -5. The time now is 03:02 PM.