LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-17-2006, 11:48 AM   #1
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Rep: Reputation: 15
Serial port - Problem with binary frames


Hi all. This is my first post in this forum.

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?

Here you have my settings:

newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR | ICRNL;
newtio.c_oflag &= ~OPOST;
newtio.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 1;

Any idea?

Thank you very much.
 
Old 01-17-2006, 01:34 PM   #2
rob_of_owensboro
LQ Newbie
 
Registered: Jan 2006
Location: Owensboro KY
Distribution: RHEL, Fedora, GENTOO
Posts: 6

Rep: Reputation: 0
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.
 
Old 01-18-2006, 04:09 AM   #3
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Thanks Rob for your reply.

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:

Float = Byte1 + Byte2 * 256 + Byte3 * 256^2 + Byte4 * 256^3

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.

Thank you very much.
 
Old 01-18-2006, 04:35 AM   #4
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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.
 
Old 01-18-2006, 06:21 AM   #5
estratos
Member
 
Registered: Jan 2006
Distribution: Ubuntu
Posts: 85

Original Poster
Rep: Reputation: 15
Wim,

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.

Thanks Wim and Rob.
 
Old 01-18-2006, 07:20 AM   #6
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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

google on serial sniffer linux
 
  


Reply

Tags
programming, rs232, serial port, tty


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem to write to serial port os2 Programming 7 05-10-2013 04:33 PM
Serial port problem floydes Programming 1 10-14-2004 11:50 AM
serial port problem... linux4john Linux - Software 1 10-30-2003 10:02 PM
Serial port problem ruchika Linux - Networking 0 08-15-2003 04:18 PM
Problem with Serial port mritunjay Linux - Networking 1 04-22-2002 01:59 PM


All times are GMT -5. The time now is 06:48 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration