Read line from serial device with BASH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read line from serial device with BASH
# 1  
Old 11-02-2016
Ubuntu 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:

Code:
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:

Code:
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]



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 11-02-2016 at 03:49 PM.. Reason: Added CODE tags.
# 2  
Old 11-02-2016
Did you try a
Code:
read INPUT </dev/ttyACM0
echo $INPUT

You may need to redefine the delimiter for your reads (man bash):
Quote:
-d delim
The first character of delim is used to terminate the input line, rather than newline.
# 3  
Old 11-02-2016
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?
# 4  
Old 11-02-2016
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
Code:
read -d $'\r' INPUT </dev/ttyACM0

For testing, you might want to experiment with other read options:
Quote:
-n nchars
read returns after reading nchars characters rather than waiting for a complete line of input, but honor a delimiter if fewer than nchars characters are
read before the delimiter.
-N nchars
read returns after reading exactly nchars characters rather than waiting for a complete line of input, unless EOF is encountered or read times out.
.
.
.
-t timeout
Cause read to time out and return failure if a complete line of input (or a specified number of characters) is not read within timeout seconds. timeout
may be a decimal number with a fractional portion following the decimal point.
# 5  
Old 11-02-2016
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.
# 6  
Old 11-02-2016
What if you read just a few chars like read -n2 (or 3,4,...)? Will any chars show up in your input variable?
# 7  
Old 11-02-2016
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

With script bash, read file line per line starting at the end

Hello, I'm works on Ubuntu server My goal : I would like to read file line per line, but i want to started at the end of file. Currently, I use instructions : while read line; do COMMAND done < /var/log/apache2/access.log But, the first line, i don't want this. The file is long... (5 Replies)
Discussion started by: Fuziion
5 Replies

2. Shell Programming and Scripting

[BASH] read 'line' issue with leading tabs and virtual line breaks

Heyas I'm trying to read/display a file its content and put borders around it (tui-cat / tui-cat -t(ypwriter). The typewriter-part is a 'bonus' but still has its own flaws, but thats for later. So in some way, i'm trying to rewrite cat using bash and other commands. But sadly it fails on... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Bash script to read a file from particular line till required line and process

Hi All, Am trying to write wrapper shell/bash script on a utility tool for which i need to pass 2 files as arugment to execute utility tool. Wraper script am trying is to do with above metion 2 files. utility tool accepts : a. userinfo file : which contains username b. item file : which... (2 Replies)
Discussion started by: Optimus81
2 Replies

4. Shell Programming and Scripting

Bash while read line

I have a script batch_vmdgenpqr.sh which has a problem: #!/bin/bash while read line do vmd.sh -dispdev text -e vmdgenpqr.tcl -args $line done<file The do line calls another program, VMD (called by vmd.sh) and it requires the values from $line, but this is not recognised by... (3 Replies)
Discussion started by: chrisjorg
3 Replies

5. SCO

Modifying serial printers device

I am trying to change one of my serial printers from /dev/ttyr002 to /dev/ttyr014: lpstat -s device for check3: /dev/ttyr002 device for check4: /dev/ttyr002 I changed the tty setting for check3 in: /etc/printcap /var/spool/lp/admins/lp/printers/check3 to /dev/ttyr014 Then I get:... (4 Replies)
Discussion started by: herot
4 Replies

6. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

7. Programming

Problem with read data from serial device

I have problem with C programming. I want to send & receive data through serial communication. I send data(command) to device to get data from device but when receive data, it can't get altogether of data. It get only some data. What should I do to get altogether of data? If all of... (7 Replies)
Discussion started by: noppon_s
7 Replies

8. Ubuntu

Ubuntu 9.04 Serial application to telnet to serial device

Hello! I am working on an application which reads environmental instruments which have serial ports. The application requires a serial port to be present to talk to the device (i.e. /dev/ttyS0 ). In some instances the environmental devices will be 100's of yards away from the computer, so a... (5 Replies)
Discussion started by: mvona
5 Replies

9. Linux

Device serial number

Hey! I'm trying to figure out a sollution for a problem I have at my company with an Iomega MiniMax 500 GB USB disk. If i run cat /proc/bus/usb/devices I get this information: T: Bus=01 Lev=02 Prnt=04 Port=00 Cnt=01 Dev#= 5 Spd=480 MxCh= 0 D: Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00... (2 Replies)
Discussion started by: noratx
2 Replies

10. UNIX for Dummies Questions & Answers

serial port device path

hi. Im trying to install a switch. And the manual says i should type a command including a SerialPortDevicePath. which is the filepath to serial port used for connection. However.. nothing about how to find this info. Could anyone help me where to find this path? thx mr.T (6 Replies)
Discussion started by: tyskertøs
6 Replies
Login or Register to Ask a Question