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