ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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]
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.
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?
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.
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).
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...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.