Score:2

write to device and capture response

br flag

I have a device which is /dev/ttyACM0. I can "screen" to this device screen /dev/ttyACM0 and interact with the device. I need to be able to script the sending of a simple command and capture the response. The command is simple, like type "ver" and hit return, the device responds with a numerical version value. It works in screen.

I am trying to script this in bash, and cannot seem to figure out how to make it work. stuff like echo "ver" > /dev/ttyACM0 produces nothing. I have tried print, same thing. Can someone point me at how to do this?

waltinator avatar
it flag
`expect` is a good tool for this. Read `man expect`.
jpbrain avatar
ca flag
hello @alabamatoy. I Have a question. is your device is an Ardunio Mega?. Arduino resets every time you switch the DTR. so open/close via bash will reboot your device. screen opens a connection and do not close it until you close the window. So it will be easy to do a small c program for that. All this assuming you dont have "rw" access problems.
alabamatoy avatar
br flag
@jpbrain it si not an arduino, it is a Numato USB IO board. https://numato.com/product/8-channel-usb-gpio-module-with-analog-inputs/
Score:1
ca flag

In Unix/Linux everything is a file. Every time you echo to a device (/dev) you are opening for writing, then writing an the closing. A simple way to catch the output of a device is as simple as

tail -f /dev/ttyACM0 (-f keeps the line open)

this will cat the output and keep the device open.
As an example, in my case, I have one project that read sensors in an Arduino and in parallel send the information to a server and also the serial port.

Now, serial ttys need to match speed on the os and the device. So sometimes you do not get the output unless you have the right speed and setting. if you type in a terminal

stty /dev/ttyACM0

you will get the current speed configuration.
So, you could try something like this (but not always work)
updated ++

#!/bin/bash

input="/dev/ttyACM1"  #sets the device

stty -F $input 115200 min 0
#added to initialize the line
stty -F $input -brkint -icrnl -imaxbel -opost -onlcr -isig 
stty -F $input -icanon -iexten -echo -echoe -echok -echoctl -echoke

echo "ver" >>$input   #send the command
tail -f $input &      #reads and wait for ever
sleep 2s
killall tail          #Could be an issue if other tail running

in my case that shell output this (it will no take the ver command as it is not implemented, but after that reads from the tty whatever the tty is sending). With the change tail stops earlier and output gets truncated at 2 seconds

1970-00-00-00:00:00: Init SDCard...
1970-00-00-00:00:00: SDCard Init done.
1970-00-00-00:00:00: Reading configuration...
1970-00-00-00:00:00: Done...
1970-01-01-00:00:01: IP: 192.168.22.199

Again, the -f flag keeps the terminal open but, if I take it off I get nothing (I lost the buffer output)

That is why I think a small c program will do better.

Hope this helps.

alabamatoy avatar
br flag
how do I close the tail once it has been opened? I too think a small C program may be better, but that would be a whole new research project for me as C is beyond my skillset. I think your script should give me plenty of guidance towards the solution.
alabamatoy avatar
br flag
The tail command never completes and nothing comes back. I wind up having to kill the process for the tail command. The cool thing is, if I send "stty -F /dev/ttyACM0 -cstopb -crtscts cs8 9600 ; echo "ver" >>/dev/ttyACM0" when I open a "screen /dev/ttyACM0" the version value is sitting there, so I know Im talking to it, but it cant seem to talk back to the script.
jpbrain avatar
ca flag
I will put a small change but need to be tweaked according your needs. This is an autokill feature :-)
jpbrain avatar
ca flag
In my case. This kills the shell but I still get some more messages after a while.
jpbrain avatar
ca flag
Hi @alabamatoy. I tested a different approach. This last one kills tail after 2 seconds.
alabamatoy avatar
br flag
I still get nothing from the device via the tail command. Screen works, but not cat or tail of the /dev/ttyACM0, it just display nothing.
jpbrain avatar
ca flag
what is the configuration of screen? if you type "stty -F /dev/ttyACM0". whta dou you get?
alabamatoy avatar
br flag
> stty -F /dev/ttyACM0 speed 9600 baud; line = 0; kill = ^H; min = 100; time = 2; -icrnl -imaxbel -opost -onlcr -isig -icanon -echo
jpbrain avatar
ca flag
Hello @alabamatoy. I have run some tests. Cannot reproduce your situation. As said I am using an arduino and ubuntu 20.04 using the shell provided. When you run the shell and screen do you run them separate in different terminals?
jpbrain avatar
ca flag
Hell @alabamatoy. added a couple of lines. how did I get there? executed stty on the dev when it was not working and then again when worked. That way I put everything was missing from the first one. Meyabe not all are needed but it is a safe bet.
jpbrain avatar
ca flag
Sorry... I meant Hello in the last comment. I just saw the typo.
Score:0
it flag

Many device access problems can be resolved through group membership changes.

Specifically, if ls -l shows that the group permissions (the second "rwx" triplet) is "rw" (e.g."-rw-rw----"), then, adding oneself to the group that owns the device will grant rw access.

Here's how:

device="/dev/ttyACM0"
sudo adduser $USER $(stat -c "%G" $device)

This allows you membership in the group that can rw the device, but there is one more step.

To make all your processes members of the new group, logout and login. Group memberships are set up at login time.

To create a single process in the new group (for testing, prior to logout/login):

newgrp $(stat -c "%G" $device)  

or, just type the group name. See man newgrp.

alabamatoy avatar
br flag
> ls -al /dev/ttyACM0 crw-rw---- 1 root dialout 166, 0 Sep 1 11:52 /dev/ttyACM0 I cannot figure out how to format this in stackexchange, but its pretty clear to me that my problem is not a permissions issue, especially when I am sudo. I need to know how to capture the output coming back from a command sent to ttyACM0....
waltinator avatar
it flag
Please [edit] your post to add new information, properly formatted. Information added via comments is hard for you to format, hard for us to read and ignored by future readers. Please click [edit] and add that vital information to your question so all the facts we need are in the question. Please don't use Add Comment, since that's our uplink to you. All facts about your system should go in the Question with [edit]
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.