ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm developing a program in C that comunicates with a RS232 device. I'm using the POSIX method in order to access the ttyS0 device, as described in the Serial Programming HOW-TO.
The external device uses a binary protocol, so it's been hard to debug. The problem is the following:
My programm receives data from the RS232 device but some of the received characters are interpreted by Linux as negative hex values. For instance, I receive:
01 (OK)
0a (OK)
15 (OK)
...
ffffffc0 (not a correct value for the received character. My programm takes this as -64 (float))
00 (OK)
...
And here my questions:
1 - I don't know any good serial monitor or sniffer for Linux. What do you recommend me? Remember that I'm working with binary frames. I really need this tool.
2 - Could my problem have something to do with the termio configuration?
I have written many program of this type. My first question is what does your frame look like and what are you talking to. Different architectures store integer and floating point numbers in different formats. For integers I often have to swap bytes around get the correct values. For floating point numbers, conversions can be far more complex depending on how the two architectures represent these numbers in memory.
Point of clarification - ffffffc0 (not a correct value for the received character. My programm takes this as -64 (float))
ffffffco is four bytes or characters, you use the singular form of character making it dificult to interpret you question.
The serial device is an energy metering one. It comunicates over RS232 sending and receiving binary frames. Integers and floats are represented by 4 bytes:
Integer values vary depending of the data to display. For example, dates and time are directly represented by the hex value of the byte (MM/DD HH:MM = HEX1/HEX2 HEX3:HEX4).
In general everything works OK. I can read coherent values and calculate its correspondant integer/float. The problem appears in some specific bytes.
Indeed, ffffffc0 should be a single byte and not four bytes. My program takes this byte and interpretes it in a wrong way. Capturing bytes into an unsigned char string transforms this byte into "c0", but again this is not a correct value. But before continuing with this question, I need a good serial monitor in order to spy the RS232 frames. I'll post my results then.
I think that your problem is solved if you make the buffer (where you store the received bytes) of type 'unsigned char'. I guess that it's currently of type 'char'.
C0 hex is greater than 127 and for that reason interpreted as a negative number. Using 'unsigned char' solves this issue.
I've declared the string as unsigned char. It takes now positive values although I still have some reserves about the coherence of the information. Anyway, next time I'll remember to capture strings in an unsigned char format.
Your talking about frames. C0 hex might be the framing character. A message from the device might be embedded between those characters. Have seen it in TCPIP comms
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.