3

I would like to use cat /dev/ttyUSB0 or something similar to monitor serial communication happening through my USB-serial adapter.

I am successfully able to send serial commands from linux to a serial device through the TX wire using echo "blahblah" | sudo tee /dev/ttyUSB0, however I wanted to see the output returned by the serial device. The expected reply is typically an echoed back message, but sometimes it a longer message.

When I open cat /dev/ttyUSB0 in a separate terminal window, all is quiet until I execute echo "blahblah" | sudo tee /dev/ttyUSB0, at which point I get "blahblah" (terminated with '\r\n' bits) repeated forever in the terminal window running cat. Unplugging the RX wire halts it, and plugging it back in does not restart it.

I am sure that this repetition is an artifact, as I have used a serial logic analyzer connected to the RX output coming from the serial device, and only see the single echoed command, not an infinite sequence of echoes.

FYI, I get the same behavior using echo "blahblah" > /dev/ttyUSB0 to send the initial serial data.

user391339
  • 449
  • 1
  • 6
  • 16
  • 1
    If you're just trying to see the response from the serial device you can try using something like `screen` or `minicom` to manage the serial port – imbuedHope Mar 01 '18 at 15:15
  • Ok thank you I will try that. Another option is to use a second usb-serial adapter but hopefully it won't come to that. – user391339 Mar 01 '18 at 15:18
  • 1
    Note, you are getting repeats because you probably have the tty in echo mode, where each char read will be echoed back out. Look at the echo part of `stty -a -F /dev/ttyUSB0` and then set it off with `stty -echo ...`. Normally you need to do even more than this, hence using `screen` is a better idea. – meuh Mar 01 '18 at 15:36
  • @meuh will try that next. So far screen worked to stop the repeat behavior, but it prevents the serial device from getting the message. Closing the screen terminal allows the message through. – user391339 Mar 01 '18 at 15:39
  • Are you typing the message into the screen window? If so try ending the line with control-j or control-d intsead of enter. – meuh Mar 01 '18 at 15:46
  • @meuh No, I was echoing/piping the message in a separate terminal window. It looks like setting stty -F /dev/ttyUSB0 -echo solved my problem and now I am able to see the response using socat in a separate terminal window. Thanks! – user391339 Mar 01 '18 at 15:51

1 Answers1

1

I know this is a million years old but I had this exact issue today. Basically, if I attached to the serial device with "screen" and then detach, I could echo into the device and get the output with cat without line spamming. Screen was passing settings to the tty, but I wanted a way of doing this programmatically. Shout out to "meuh" for pointing me in the right direction. In my case, using `stty', I issued the following beforehand:

stty -echo -opost -F /dev/ttyUSB0

The '-echo' stops the newline spam and '-opost' stopped a single newline that I didn't need in my case. I could then proceed with my testing in which I ran the following in one terminal window:

# cat /dev/ttyUSB0

I then ran the following in another terminal window:

# echo "This is a test - $(date)" > /dev/ttyUSB0

I received the expected output in the first terminal window:

# cat /dev/ttyUSB0 
This is a test - Mon 21 Feb 2022 11:27:40 AEDT

Hope this helps!

Kooby
  • 11
  • 1