How to send and receive data from serial port using command line?
The usual issue for this is that when the device is closed then it gets reset back to some default configuration, so any changes you make are lost. Holding an open file descriptor avoids this.
Something like this (untested)
#!/bin/bash
# Keep the ttyUSB0 device open on fd 3
exec 3<>/dev/ttyUSB0
stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb
echo "1" >&3 # send data
cat <&3 # read the data
Related videos on Youtube
![Smetronic](https://lh4.googleusercontent.com/-mFBmdpcGCDM/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3reAk-xx7KNRyaC-Ohpvn2ESdi9s_A/photo.jpg?sz=256)
Smetronic
Updated on September 18, 2022Comments
-
Smetronic almost 2 years
In the past, I have used c++ and python to communicate with serial ports in a Linux and Windows environment. In Linux, I have also used programs like picocom, minicom, and cutecom for serial communication but now I want to read and write to the serial port using simple Linux commands which requires no installation of external programs. I would be using this method in raspberry pi to communicate with my Arduino board. In the below example I'm using stty for setting serial port options and I use echo and cat command to send and read data from the serial port but at the end, I'm not seeing any output, I have read other posts in this site related to this but nothing seems to work for me. I'm able to communicate with Arduino using cutecom but with below commands, I don't see any response.
Linux (Ubuntu):
$ stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb $ echo "1" > /dev/ttyUSB0 //send data $ cat /dev/ttyUSB0
Arduino Mega code for your referance:
#include <Arduino.h> void setup() { Serial.begin(115200); } void loop() { if(Serial.available() > 0){ Serial.println("[123,55,7777]"); } }
Here i send 1 and i get the response from arduino:
This should be pretty simple I send 1 or any character to Arduino and it should return [123,55,7777] in the command line. Any kind of help and guidance is appreciated.
Below is the code that I have tried but doesn't return any data.
stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb #CONFIGURE SERIAL PORT exec 3</dev/ttyUSB0 #REDIRECT SERIAL OUTPUT TO FD 3 cat <&3 > /tmp/ttyDump.dat & #REDIRECT SERIAL OUTPUT TO FILE PID=$! #SAVE PID TO KILL CAT echo -e -n "\x01" > /dev/ttyUSB0 #SEND COMMAND HEX 0x01 TO SERIAL PORT sleep 0.2s #WAIT FOR RESPONSE kill $PID #KILL CAT PROCESS wait $PID 2>/dev/null #SUPRESS "Terminated" output exec 3<&- #FREE FD 3 cat /tmp/ttyDump.dat #DUMP CAPTURED DATA
Thanks
-
sawdust over 2 yearsYour termios configuration is incomplete. See stackoverflow.com/questions/71065389/…
-
-
Smetronic over 4 yearsif I set the serial port using stty will the changes would be kept in memory if I don't call any program on that serial port. I have tested your code it doesn't return anything.
-
Smetronic over 4 yearsafter adding sleep 0.2s after the echo command, data returned. I guess it was reading before data could be written to /dev/ttyUSB0.
-
icarus over 4 yearsIf you think the answer I gave is correct then you can accept it so others in the future know it is valid. Otherwise you can create your own answer and accept it. The point of Q&A sites like this is to get answers for the future.
-
Smetronic over 4 yearsHi, after running the serial bash script for a few minutes, error "stty: standard input: Inappropriate ioctl for device" starts to come up. the same error also appears in CuteCom when opening the serial port.
-
Greg H over 2 yearsClosing the file descriptor when you're done can be accomplished with: 3<&-