1

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: 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

Thanks

0

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
| improve this answer | |
  • 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 Feb 22 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 Feb 22 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 Feb 22 at 20:42
  • yeah, now I can call this script from other languages for serial communication. Thanks – Smetronic Feb 23 at 19:21
  • If 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. – icarus Feb 23 at 20:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.