51

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?

migrated from stackoverflow.com Jul 5 '12 at 14:30

This question came from our site for professional and enthusiast programmers.

56

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
  • or socat stdio /dev/ttyS0 – pstanton Nov 19 '17 at 8:28
  • 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 1 '18 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 Mar 1 '18 at 8: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 6:12

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.