Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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 the cat 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.

share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
    
Hi Jim, How can I prevent this? –  Ricardo Cristian Ramirez Sep 23 '12 at 13:10
1  
You 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. –  Jim Paris Sep 24 '12 at 16:09
    
Thanks 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 –  Ricardo Cristian Ramirez Sep 25 '12 at 21:11
add comment

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.

share|improve this answer
    
Hi P.T., Actually I am not sure what corrupts the data but if I do not use cat everything works fine. If I use cat, it prints the correct data but the application on the other side does not respond, somehow cat 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 Sep 23 '12 at 13:08
1  
Ah, 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. –  P.T. Sep 23 '12 at 17:04
    
Hi 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. –  Ricardo Cristian Ramirez Sep 23 '12 at 17:24
    
PT: that's what my answer assumed he meant –  Jim Paris Sep 24 '12 at 16:10
add comment

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 press F to read some data then Ctrl+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.

share|improve this answer
    
Hi Gilles, hexdump -C and od -t x1 produce elegant outputs. I used both of them thank you. –  Ricardo Cristian Ramirez Sep 23 '12 at 13:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.