So I have a standard RS232 serial port that is looped back to itself by simply running a wire from Tx to Rx. I'm testing loopback by running echo
and cat
in two separate terminals:
cat /dev/ttyS1
echo "hi" > /dev/ttyS1
My issue is with the output. I would expect to see one "hi" come back on the terminal running cat but instead I get this:
hi
[2 newlines]
hi
[4 newlines]
hi
[8 newlines]
hi
[16 newlines]
hi
[32 newlines]
hi
…and so on until I ctrl+c cat
.
After interrupting cat, if I run it again it will not output "hi"s until I run echo a second time.
Is this normal? Any idea why I'm seeing this behavior?
Edit: By newline, I mean ASCII 0x0A
. There are no carriage returns in this output.
Best Answer
Thanks to the second comment by Bruce, I was able to figure out the problem on my own.
After running
stty -a -F /dev/ttyS1
, there were 3 options I found to contribute to the problem: "echo", "onlcr", and "icrnl".Since this serial port is looped back to itself, here is what happened after running
echo "hi" > /dev/ttyS1
:echo
command appends a newline to the end of the message by default, so "hi" + LF is sent out to /dev/ttyS1And so on...
In order to fix this problem, I ran the following command:
Disabling "echo" prevents an infinite loop of messages and disabling "onlcr" prevents the serial device from converting LF to CRLF on output. Now
cat
receives one "hi" (with a single newline!) for each time I runecho
.CR = carriage return (ASCII 0x0D); LF = line feed or newline (ASCII 0x0A)