78

I have a serial port device that I would like to test using linux command line.

I am able to use stty and echo for sending commands to serial port, but when device responds I have no way of reading what is coming from serial port. I am using

stty -F /dev/ttyS0 speed 9600 cs8 -cstopb -parenb && echo -n ^R^B > /dev/ttyS0

to send a command to the device. Device operates and sends a response back in 300 ms's. How do I print that response to the console using command line?

2 Answers 2

82

Same as with output. Example:

cat /dev/ttyS0

Or:

cat < /dev/ttyS0

The first example is an app that opens the serial port and relays what it reads from it to its stdout (your console). The second is the shell directing the serial port traffic to any app that you like; this particular app then just relays its stdin to its stdout.

To get better visibility into the traffic, you may prefer a hex dump:

od -x < /dev/ttyS0
6
  • 1
    or socat stdio /dev/ttyS0
    – pstanton
    Commented Nov 19, 2017 at 8:28
  • 1
    I sent a command to a device I am working with. The command is : echo "1GAINS" | sudo tee /dev/ttyUSB0 .... and in response I am getting an infinite stream repeating the same message. Any ideas? The device itself should echo back the command I send once, but here it's like I'm getting weird feedback.
    – user391339
    Commented Mar 1, 2018 at 8:14
  • I verified that the feedback is not "real" using a serial analyzer. The device only echos the command back once, but using the above commands I get a crazy endless repetition on the terminal output.
    – user391339
    Commented Mar 1, 2018 at 8:25
  • Does it work with binary data ?
    – ransh
    Commented May 14, 2018 at 18:53
  • 1
    cat rarely works, in my experience; it just returns with nothing. but screen /dev/ttyACM0 115200 works pretty reliably Commented Dec 29, 2023 at 3:24
25

I am monitoring output for arduino uno like:

screen /dev/ttyUSB0 9600
3
  • 7
    This worked best for me to see all the data being written to the USB tty port. cat would immediately exit where screen runs continuously until killed via CTRL-A k command. Commented Nov 9, 2020 at 21:09
  • 11
    Exit: Ctrl-A, k, y Commented Feb 17, 2022 at 22:06
  • This worked on Raspberry Pi as well after installing screen Commented Aug 13, 2023 at 22:33

You must log in to answer this question.

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