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() {

void loop() {

    if(Serial.available() > 0){

Here i send 1 and i get the response from arduino: CuteCom example

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



1 Answer 1


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)

# 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
  • if 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
    Commented Feb 22, 2020 at 16:30
  • after adding sleep 0.2s after the echo command, data returned. I guess it was reading before data could be written to /dev/ttyUSB0.
    – Smetronic
    Commented Feb 22, 2020 at 20:37
  • One more thing, I also connected external CP2102 USB to UART to Arduino after that I was getting data. The Arduino board is using CH340 USB to UART.
    – Smetronic
    Commented Feb 22, 2020 at 20:42
  • yeah, now I can call this script from other languages for serial communication. Thanks
    – Smetronic
    Commented Feb 23, 2020 at 19:21
  • 1
    Hi, 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.
    – Smetronic
    Commented Feb 25, 2020 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .