Social Nerwork

Telephone us on: +353 87 223 6429 or email us using: info@ridgesolutions.ie
Telephone us on: +353 87 223 6429 or email us using: info@ridgesolutions.ie

Bash – send data to serial (rs232) port and wait for response

Sending data to a serial port is quite easy in Bash, for example:

And you can read from a serial port using cat:

However cat must typically be run from a different shell instance as it blocks waiting for data. So is it possible to write and then read the response from a single shell instance?

Well, it is, but it requires a bit of sleight-of-hand. For example, if we start cat in the background and then send the command, cat will report the response as follows:

Which works but it a bit of a mouth-full! cat continues to run in the background, and will print more responses as they arrive.

But what if you want to just send one packet and then wait for a single response?

This is a bit harder, but if your response ends with an end of line character, or another known character then we can use read to help with this…

First we setup a read command in the background, unlike cat, this command will end when a response is received or when the timeout time arrives, then we can send our command:

This gets read to wait for up to 20 seconds (-t20) for a line of data (max size, 60 characters -n60) from /dev/ttyS0, which it reads into RESP, it then echos $RESP – all of this happens in the background. echo then sends the packet which will result in a response.

If your response packed ends with a character other than an EOL character then you can specify a delimiter to read using the -d command-line option.

Again it’s all a bit long winded, so we can wrap it all up in a bash script (send_tty.sh) as follows:

An example of using the script:

LEAVE A REPLY

 

Google+