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:
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]
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?
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.
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.
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.)
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: