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?

Tom Hale
  • 30,455
erin c
  • 893

2 Answers2

80

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
  • 1
    or socat stdio /dev/ttyS0 – pstanton Nov 19 '17 at 08: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 Mar 01 '18 at 08: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 Mar 01 '18 at 08:25
  • Does it work with binary data ? – ransh May 14 '18 at 18:53
  • @ransh - See the hex dump part of the answer. You'll ultimately need a human or a tool who understands or who can reverse engineer that particular binary data. – Jirka Hanika May 15 '18 at 06:12
  • 1
    cat rarely works, in my experience; it just returns with nothing. but screen /dev/ttyACM0 115200 works pretty reliably – Marc Compere Dec 29 '23 at 03:24
24

I am monitoring output for arduino uno like:

screen /dev/ttyUSB0 9600
gjerich
  • 241