Reading from a USB device using bash and raspberry pi 3? - linux

I'm trying to catch the response from a device that is connected to the USB.
With this code:
sudo stty -F /dev/ttyUSB0 speed 115200 cs8 -cstopb -parenb -echo
sudo stty raw; cat > /home/received.log < /dev/ttyUSB0
echo "Monitor started"
On port ready, send the command request to the device
echo -en '\x5A\x00\x00\x0D\x0A\x71' > /dev/ttyUSB0
then read the log and paste the response in another file converting it properly
xxd -plain /home/received.log > /home/output.txt
so I can show the data,
cat -v < /home/output.txt
But I'm needing something more stable, that code is showing me this error:
stty: 'standard input': Inappropriate ioctl for device
which is weird, because it was working...
I would like to catch the response and store it in a variable.

i suggest getting rid of the stty raw.
Unless you are sending/receiving special characters that the tty subsystem will process, which is unlikely - because you said it is working - you almost certainly won't need it.
If it did work, you might find some undesirable side effects such as:
backspace does not erase a character that you miss-typed
control-c won't terminate your process
and other things that you might rely on in your terminal session.
FWIW, i am doing a similar thing with Arduino to Mac, Windows (cygwin) & Linux (read from the usb/serial port) and i have not been tempted to stty raw in any of those environments.

Related

Loopback/Echo bytes received over serial port

On an embedded Linux system running Busybox I am trying to receive bytes over a serial port and echo back everything received.
The system setup is like this:
Linux <-USB-> FTDI chip <-UART-> MCU
On the UART line I have a logic analyser monitoring the data between the FTCI chip and the MCU. Both the MCU and Linux have the same UART configuration.
The script I have written runs on the Linux system and is supposed to send back all data it received from the MCU.
So far I have this simple Bash script which is to receive bursts of data 62 bytes long. The timeout is set to 5 seconds as a sort of alive signal.
#!/bin/bash
# Enable debugging
set -x
# Set the baudrate of the port
stty -F /dev/ttyUSB0 1500000
while true
do
# Read 62 bytes with a timout of 5 seconds to variable RESP.
read -N62 -t5 RESP < /dev/ttyUSB0
# Print out how many bytes we received
echo ${#RESP}
# Send back the data, -n for no trailing new line
echo -n $RESP > /dev/ttyUSB0
done
There are a few problems I have with this script:
Not all bytes are received consistently. I've been testing now for some time an only seen the full number of bytes once.
It outputs only 0xFF values on the UART bus, this is observed through the logic analyser.
What am I missing here in order to receiving the correct data and send it back correctly?
Through the suggestions made by #sawdust I was able to get a working script.
In the end I stopped using the read command. I was unable to get it to work in raw mode. I could not use termios as this in to available on my Busybox system. I got a working setup using dd:
#!/bin/bash
stty -F /dev/ttyUSB0 raw
stty -F /dev/ttyUSB0 1500000
dd if=/dev/ttyUSB0 count=62 of=/dev/ttyUSB0

Inappropriate ioctl for device when trying to SSH

I'm trying to SSH few servers and trying to get sudo -l output of each server.
Below is the script I'm executing
#!/bin/bash
serverlist="/tmp/servers"
while IFS=, read -r server netgroup username user
do
ssh -tt -q root#$server sudo -U $username -l < /dev/null
done < "$serverlist"
I have found that -tt option in this script as the cause of this error. Any thought on this?
Also i have noted that I don't see this error when i execute below command just for 1 server.
ssh -tt -q root#myserver sudo -U cham01 -l
Below is the complete error message I'am getting:
tcgetattr: Inappropriate ioctl for device
tcgetattr: Inappropriate ioctl for device normally means that some program attempted to do a terminal control operation but its standard I/O streams weren't connected to a terminal. (I know this because tcgetattr is the name of a C library function that does terminal control operations.)
Now, the whole point of the -tt option to ssh is to guarantee that the program run on the remote host is connected to a terminal, and stty printing out speed 38400 baud; line = 0; -brkint -imaxbel demonstrates that it was. This is what I get when I run these commands with my servers:
$ ssh myserver stty < /dev/null
stty: 'standard input': Inappropriate ioctl for device
$ ssh -tt myserver stty < /dev/null
speed 38400 baud; line = 0;
-brkint -imaxbel
Connection to myserver closed.
But what you are getting is
$ ssh -tt yourserver stty < /dev/null
tcsetattr: Inappropriate ioctl for device
speed 38400 baud; line = 0;
-brkint -imaxbel
The tcsetattr error is not coming from stty. First something tried to do something terminal-related and failed, and then stty ran successfully.
This suggests a bug in your shell startup scripts, which are doing something that is inappropriate when run "non-interactively", causing you to get this error even though you are running commands connected to a terminal. I can't help you any further, but perhaps this old answer about a similar problem offers some clues.
export GPG_TTY=$(tty)
From: https://github.com/keybase/keybase-issues/issues/2798#issue-205008630
:)
In this answer I will not propose a solution (I don't know where the error comes from) but, instead, I am going to suggest a powerful instrument to find it!
To understand where the problem comes from you can use the command strace:
strace ssh -tt -q root#myserver sudo -U cham01 -l < /dev/null
Sure you will realize which one is the system call that causes the error. The shell will prompt all the system calls and, at some point close to the end,you will see something like:
....
ioctl(3, SNDCTL_TMR_START or TCSETS, {B0 -opost -isig -icanon -echo ...}) = -1 ENOTTY (Inappropriate ioctl for device)
....
Here, you can find examples on how to use it.
Suggestion: before the ioctl(...) system call, there should be an open(...) system call for the same device. Go in the header file of the device and try to have a look on the different command you can pass it. The problem should be a not recognized command (due maybe to an old version of the device driver used). This is just a suggestion.

how to duplicate /dev/ttyUSB0 stream?

Is there a way to duplicate a serial device /dev/ttyUSB0 on Linux (Ubuntu 14.04) ?
I made a program which read the data from the RS232 port and I would like to execute two instances of my program with different options in parallel. However, I cannot open the /dev/ttyUSB0 twice from my program (writen in C).
Ideally, I would like the ttyUSB0 driver to create two identical devices. (/dev/ttyUSB0 and /dev/ttyUSB1). The driver would make sure that ttyUSB1 is an exact copy of ttyUSB0.
Is there a way to perform this from the command line (bash) or should I make my own special serial port driver ?
You can use tee to output anything written to /dev/ttyUSB0 to two named pipes.
mkfifo copy1 copy2
tee copy1 copy2 < /dev/ttyUSB0 > /dev/null &
program1 < copy1
program2 < copy2
(Strictly speaking, you only need one named pipe; the other program could read from an anonymous pipe connected directly to tee. This symmetrical treatment is a little cleaner, though.)

Using gnu screen to access serial port

I am trying to access a device which is attached to a USB-serial port. The settings are 57600 baud, 8 bit, 1 stop bit, no parity. The device outputs a status line every second and accepts typed commands.
I would like to use GNU screen to initiate 2 way communications, so I am using this command:
screen /dev/ttyS2 57600,cs8
However I just get a blank screen, nothing received from the device.
The communication is fine using teraterm, and I can also do this
stty -F /dev/ttyS2 57600 cs8
cat /dev/ttyS2
to see the status output from the device.
I've tried various combinations of ixon, ixoff, crtscts, and clocal but nothing makes any difference.
How can I determine what the correct command should be?
I am using Cygwin on Windows 10.
I faced the same issue with gnu-screen, I started using plink.exe instead from the PuTTY suite. It's not optimal, but it does the job. In my case serial is just for recovery, not for everyday usage.
Start PuTTY, create a profile with your serial connection.
Name and save the connection.
From cygwin, run: '/cygdrive/c/Program\ Files\ (x86)/PuTTY/plink.exe -load SerialProfile'

echo command to serial port in Linux

I want to echo something from my Ubuntu host to some device.
It works well if I use putty or minicom.
However, it doesn't work if I do echo from the shell terminal:
echo "cmd" > /dev/ttyUSB0
From my device, I saw that the first letter of the cmd is received correctly while the second one received is ASCII bigger than 200. I also have tried to use the "stty" command to adjust the serial communication settings but didn't help. Does anythone know why?
Thanks,
First You need to set the tty device settings and then you need to transmit the data whatever you want
stty -F /dev/ttyUSB0 9600 -parity cs8 -cstopb
OR
stty -speed 9600 < /dev/ttyUSB0
Now Send data:
echo "cmd" > /dev/ttyUSB0

Resources