Read line from serial device with BASH

I'm new to Linux (Ubuntu 16.04), and very new to BASH scripting. I have a Numato 8-channel USB GPIO device, which is a DAQ that appears in the system as a serial port. In Linux it appears as ttyACM0 . I can easily manipulate a GPO with, for example:

echo "gpio set 7" > /dev/ttyACM0
...followed by a carriage return:
echo -e '\r' > /dev/ttyACM0

But if I want to read a GPI, or one of the ADCs, I need to be able to read lines of data, (separated by carriage returns), sent back by the device...and save the information as a variable. So, for example I can send a request:

echo "adc read 0" > /dev/ttyACM0
echo -e '\r' > /dev/ttyACM0

I would then need to do two "read lines". The first would return an echo of the command I sent. The second would return the value of the ADC. I've not been able to figure out how to do these line reads. Does anyone know a simple command or series of commands to retrieve this serial information?

[I'm successfully doing automation with these devices with PowerShell under Windows 7, but I'd love to do the same from Linux machines]

Did you try a

read INPUT </dev/ttyACM0
echo $INPUT

You may need to redefine the delimiter for your reads ( man bash ):

Yes, I've tried that read command. All I get is a blinking cursor...have to use cntrl-c to bail out and get back to prompt. Can you elaborate more on defining delimiter?

The cursor blinks as the read didn't stop as it didn't receive a <newline> char. I guess your device sends <carriage return>s? Try

read -d $'\r' INPUT </dev/ttyACM0

For testing, you might want to experiment with other read options:

Thanks for this information. After more experimentation I've discovered something that may be a clue. I've noticed that I can send one successful command to the device from BASH. Then I can make various unsuccessful attempts to read, which produce no viewable data...but can be forced to time out. I can send no more successful commands from BASH unless I open a Gnu Screen session and send some carriage returns to the device. So it appears that the device's buffer has data sitting in it that must be cleared before it will interact further. Bottom line, I'm not successfully reading it with BASH commands so far.

What if you read just a few chars like read -n2 (or 3,4,...)? Will any chars show up in your input variable?

No. I tested that variation also. The read won't end unless I use a timeout. Then the variable is blank. And, of course, the device becomes unresponsive until I intervene with Gnu Screen. I must be overlooking something simple. There must be a way to duplicate what I'm doing with this device in PowerShell.

I set up a serial connection between a Windows 7 PC and a Ubuntu 16 laptop. Using TeraTerm on the Windows machine, I can send a string...then handily read it from BASH on the laptop with:

read whatever < /dev/ttyUSB0

No problem. So...there's something different about the Numato device...something I'm missing.

Check what it actually sends, esp. the terminator/delimiter. cat the device and pipe that through an octal or hex dumper.

You might also want to consider using:

printf 'add read 0\n\r'

instead of:

echo "add read 0" > /dev/ttyACM0
echo -e '\r' > /dev/ttyACM0

Note that the two echo commands each open and close the device and each prints a <newline> character that might neither be needed nor desirable from the second echo .

Alternatively, you might want to consider opening the device once at the start of your script using exec to open (and hold open) your device:

exec <> /dev/ttyACM0

(replacing standard input for the entire script in this case, but a different file descriptor can also be specified if some other file is being fed into your script). Then you could consider using the stty utility to set appropriate parameters for your device such as icrnl or -icrnl and igncr or -igncr (depending on whether your device sends <carriage-return> and/or <newline> characters as line terminators when you read data from your device) and ocrnl or -ocrnl , onlcr or -onlcr , and onocr or -onocr depending on what it wants to see as line terminators in commands it receives from your script. (And, of course, check the stty man page on your system for other operands that might simplify using your device.)

I thought about proposing using the stty utility as well but wasn't sure if it would work/apply, as we are dealing with a USB GPIO device .

Using stty might or might not work, but it is really easy to find out...

If the command stty < /dev/ttyACM0 works (printing the current settings for the device), it will work. If it exits with a non-zero exit status and prints a diagnostic similar to:

stty: stdin isn't a terminal

then it won't work.

Thanks Don. I will try your suggestions today. One question: If I use

exec <> /dev/ttyACM0

to open the port for the duration of the script, what is the command for closing it when I exit the script?

There is no need, it will close itself on exit, but:

exec <&-