cat corrupts serial port data
Solution 1
It's not corrupted. What's happening is that the cat
command is getting some of the bytes, and your application is getting some of them. So when you run cat
, any bytes read by it are missed by the app, and both cat
and the app will see (different) partial streams that appear corrupted.
Solution 2
Are you sure the data isn't corrupted by your terminal (or wherever cat is displaying)? cat
is unlikely to corrupt your data.
Try using od
(octal dump) to dump the data coming from the serial port, so you can see exactly what is coming across (without relying on it being printable). Use od -c
if you're expecting ASCII data.
If you're still seeing corruption, maybe your serial port isn't quite configured correctly? Try setserial
and stty
to see if they can configure things better.
Solution 3
cat
doesn't modify the data. There might be old Unix systems where it truncates lines that contain null bytes, but not Linux, and I think not any modern unix-like system.
On the other hand, if you try to display binary data directly on your terminal, the terminal will interpret control characters as commands to control the display. That's what control characters are for. If you want to see a printable representation of the raw data, you have several solutions:
- Run
cat -A
, which will print a readable but ambiguous representation of control characters (e.g.^A
could be the byte 0x01 or the two-byte sequence 0x5e 0x41). - Run
hexdump -C
,od -t x1
or some other hexadecimal dump program (or an octal dump if you prefer). - Run
less /dev/ttyS0
and pressF
to read some data thenCtrl+C
to browse it. Inside less, type-r
to toggle between raw display of control characters and a printable representation.
Note that reading /dev/ttyS0
shows what your serial port receives, not what is sent through it. If you want that, spy on the application that's writing, e.g. with strace
or a debugger.
Related videos on Youtube
Ricardo Cristian Ramirez
Updated on September 18, 2022Comments
-
Ricardo Cristian Ramirez almost 2 years
I have an application which writes some bytes to a serial port. When I do
cat /dev/ttyS0
to see what is being transferred, I find that the data is corrupted by thecat
command. Is there any other way to see what is being sent on the serial port?Does anyone know why
cat
changes the data?Edit: There is another application on the other side and I want to intercept the data in order to check its content but the application must continue to work.
-
Ricardo Cristian Ramirez almost 12 yearsHi P.T., Actually I am not sure what corrupts the data but if I do not use
cat
everything works fine. If I usecat
, it prints the correct data but the application on the other side does not respond, somehowcat
eat the data. BTW, stty out as follows:-parenb -parodd cs8 hupcl -cstopb cread clocal crtscts -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -iutf8 -opost -olcuc -ocrnl onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0 -isig -icanon -iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt echoctl echoke
-
Ricardo Cristian Ramirez almost 12 yearsHi Jim, How can I prevent this?
-
Ricardo Cristian Ramirez almost 12 yearsHi Gilles,
hexdump -C
andod -t x1
produce elegant outputs. I used both of them thank you. -
Michael Nelson almost 12 yearsAh, please update your question to indicate that you've also got an app reading from the serial port. That explains the corruption as the bytes on the serial port are "consumed" when read, so only one app can see them.
-
Ricardo Cristian Ramirez almost 12 yearsHi again, I have edited the question. If only one application can see the data, is there a suitable way to redirect the data to the application after doing
cat
? Thanks. -
cheshirecatalyst almost 12 yearsYou can't change that behavior, so you'll need to design around it. For example, write a program that reads from the serial port and feeds data to your application, optionally displaying it. Or have your application write its own debugging output. For infrequent debugging, you might be able to get away with
strace
to see what your application is reading. -
cheshirecatalyst almost 12 yearsPT: that's what my answer assumed he meant
-
Ricardo Cristian Ramirez almost 12 yearsThanks Jim. I do not have the source of receiving application. I am just using its data top of it. So writing a multiplexer program will be more useful for my case. I have not worked with serial ports so far and I thought
cat
intercepts and displays the flow, but I was wrong. Also, I found a similar topic in stackoverflow -
xcorat over 7 yearsCan't you do a
tee
and redirect to a dummy file to be read from your app?