Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I have a device connected to the Serial Port and i wish to extract it's readings into a C Program. I have written a program to read and display the Serial Port Output but it gives out Garbage. Is there a need to process the output before displayin in order to get the required values in Hex? If so, how do i go about it? Thanks for your help in advance.

There are numerous reasons why the serial port might be giving you garbage.
Do you have the baud rate set correctly, are the stop bits and parity bits correct. How are you accessing the serial port, polling or interrupts.
The serial port reads pure binary numbers. If you are expecting Hex you are going to have to convert binary into Hex.
Stuart

Continuing from your previous post.
What is the device that you are trying to read? It must have some documentation of the communications protocol and data format. Most serial ports are designed to transfer whole words (normally 8 bit, but other values are possible). As previously mentioned you will have to convert your input to get HEX.
It would be easier to debug with a known working terminal program. There are some that have a diagnostic mode that displays the raw data stream. Note that some devices have non-standard protocols (Especiallly those running at low baud rates). Make sure hardware handshaking is correct if used. It's easier to test if your device can be set to continually output. Then vary your terminal settings until you get decipherable output.

I'm working with an Inertial Measurement Unit (RS232 Interface). All i've done so far is an open followed by a read on the serial port. Here's a sample output:
Read Status is 100
@Ð !!!!¬þ 1!!¬¥ü !%!±ü !!'±ü !!'±ü !!!±ü !%±ü !%'¬þ
Read Status is 55
'±ü !!%±ü !±ü !% ±ü !!';
±ü !!!±ü !%±ü !%'¬þ
Read Status is 21
@Ð !!%±ü %54 !±ü !% ±ü !!';
±ü !!!±ü !%±ü !%'¬þIf possible, could anyone send me an example of wat i shud ideally be looking for or an example of how to read the serial port using C Programming? Sorry about the trouble and thanks a lot for your patience and help.

If you were to look at the numbers and not their ASCII equivalent then it might make a little more sense. What you have is like looking at a programme file with a text editor - is doesn't mean much. In amongst the data are some characters that are outside the normal ASCII range so it is not ASCII that is being transmitted.
There is a lot of repetition there which if you could establish a pattern will give you some idea of what is happening.
Stuart

The repition is acceptable because the INS is stationary. So i presume those symbols correspond to the output of the INS wen it is stable. If i were to move it about, then the readings would change. I still cant find a way to interpret those readings. If i just execute a read on the serial port, should it not be giving out values in binary unless there is a prob with the serial port itself, which there doesnt seem to be! I cant find a solution to this anywhere on the web or in a book. Please Help!

It is giving readings in binary. As wizard-fred pointed out, you should have/get the documentation telling you the format of each frame/string the device spits out. Otherwise you'll have to guess or deduce it from experiment.

I did a guick google on IMU's. Little data on military stuff, plenty on commercial stuff especially those using Analog Devices Chip. Serial Devices are common using the higher baud rates of 19.2k to 460k. Data Stream Is A Set Of Packets (ID, Header, Status, Data, Trailer). One vendor uses 25 bytes for 6 channels. Two successive bytes for 16 bit data from ADC. Big-Endian.
Obviously you are very short on documentation. There are SDK's available from many vendors. (C, Delphi, Etc.)
I still think your communications is off.
In the example you should be getting a repeating pattern of 25 bytes with no motion.

Thanks a lot for all your help guys. I will check my communications again and pray it works. i will update you on my results. Once again, i really appreciate your help.

I seem to have fixed the problem and am getting output as below. However, the read status, which is the number of bytes read in keeps changing even when the instrument is stationary? Is this normal or have i made a mistake in programming again? the buffer i have defined is an integer buffer and i am also concerned because of th negative values below. please advice. thanks
Read Status is 44
-826734246
-1632040547
1498044829
1503484330
1234913708
1504467290
-826734230
-1631990627
1504336285
1503484314
176310700
1233865132
1505515866
7212122
-1631990776
1498044829
1502435674
1234913724
1504467274
-826734230
-1632040547
1504336285
1503484346
1234913692
1504467290
0

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |