LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-02-2016, 01:55 PM   #1
Chalk-X
LQ Newbie
 
Registered: Nov 2016
Posts: 3

Rep: Reputation: Disabled
Question 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]
 
Old 11-02-2016, 03:18 PM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
To do I/O properly you should use a programming language like python or C. That lets you use select calls, timeouts, error handling and libraries full of debugged, working code. However, if you insist on bash, the "read" command may be used to get a line of input from a filehandle opened with "exec". See the bash man pages.
 
Old 11-02-2016, 04:42 PM   #3
Chalk-X
LQ Newbie
 
Registered: Nov 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
I started with BASH because...it was there. I'd have preferred PowerShell, but its Linux flavors are under-developed at this point. I'm sorry, but I don't understand your reference: line of input from a filehandle opened with "exec". Can you elaborate?
 
Old 11-02-2016, 04:50 PM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Welcome to LQ!

As smallpond has said, within a shell script something based around read is probably the way to go. But depending on the actual level of automation you need, you may want to look at other programming languages.

That said, your initial description sounds very much like something that you could easily do with minicom. I am sure that is available for Ubuntu and may come pre-installed.

Basically, minicom will allow you to open a terminal emulator to a com port (i.e. your device) with pre-configured options, then interact with that device live or through scripted commands, and to capture the data received into a file. Might be just what you need.
 
Old 11-02-2016, 04:54 PM   #5
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Quote:
Originally Posted by Chalk-X View Post
I started with BASH because...it was there. I'd have preferred PowerShell, but its Linux flavors are under-developed at this point. I'm sorry, but I don't understand your reference: line of input from a filehandle opened with "exec". Can you elaborate?
Our replies crossed in space-time...

The best way to answer that might be to direct you to the Advanced Bash Scripting Guide. Information specific to this use may be found on this page (and others).

Last edited by astrogeek; 11-02-2016 at 04:57 PM.
 
Old 11-02-2016, 05:00 PM   #6
Chalk-X
LQ Newbie
 
Registered: Nov 2016
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks, astro...I will investigate MINICOM. I remember seeing it mentioned somewhere in my recent cyber travels...but didn't know it could be interacted with via command line scripts. BASH read seems to be lacking something that this particular device needs. I've connected the serial ports between a Windows 7 PC and a Linux laptop and used TeraTerm to send a string from the Windows PC...and with no problem read it with BASH. So there must be something slightly different about the Numato device's protocol. I'll keep tinkering...
 
Old 11-04-2016, 12:36 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
You could try 'read' command. You might have to use 'stty' beforehand to set serial-line options.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash read a file line by line and give percentage of task completed dunryc Programming 2 06-13-2016 03:38 PM
bash : read every line from text file starting at given line number quadmore Programming 4 02-20-2009 12:29 PM
I would like need a suggestion on bash shell : Read a file line by line and do stuff madi3d8 Linux - Newbie 1 01-15-2009 09:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:16 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration