473,385 Members | 1,912 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Parse a data frame in C?

4
I am writing a program that reads the data from the serial port on Linux.
The data are sent by another device in the following frame format:

---------------------------------------------------------------------
|start | Command | Data | CRC | End |
|0x02| 0x41 | (0-127 octets) | | 0x03|
--------------------------------------------------------------------

The Data field contains 127 octets as shown and octet 1,2 contains one type of data; octet 3,4 contains another data. I need to get these data

I know how to write and read data to and from a serial port in Linux, but it is just to write and read a simple string (like "ABD")


My issue is that I do not know how to parse the data frame formatted as above so that I can:

- get the data in octet 1,2 in the Data field
- get the data in octet 3,4 in the Data field
- get the value in CRC field to check the consistency of the data

Here the sample snip code that read and write a simple string from and to a serial port in Linux:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int writeport(int fd, char *chars) {
  3.     int len = strlen(chars);
  4.     chars[len] = 0x0d; // stick a <CR> after the command
  5.     chars[len+1] = 0x00; // terminate the string properly
  6.     int n = write(fd, chars, strlen(chars));
  7.     if (n < 0) {
  8.         fputs("write failed!\n", stderr);
  9.         return 0;
  10.     }
  11.     return 1;                                                                                                           
  12. }
  13.  
  14. int readport(int fd, char *result) {
  15.     int iIn = read(fd, result, 254);
  16.     result[iIn-1] = 0x00;
  17.     if (iIn < 0) {
  18.         if (errno == EAGAIN) {
  19.             printf("SERIAL EAGAIN ERROR\n");
  20.             return 0;
  21.         } else {
  22.             printf("SERIAL read error %d %s\n", errno, strerror(errno));
  23.             return 0;
  24.         }
  25.     }                    
  26.     return 1;
  27. }
  28.  
Does anyone please have some ideas?
Thanks all.
Mar 23 '10 #1
9 8335
weaknessforcats
9,208 Expert Mod 8TB
Treat everything as unsigned char.

What do you mean by octet? Is that 8-bits or 8 bytes?

If you can populate your array you have your data.

The CRC can't be calculated unless you use the same formula that was used to create the value in your data.
Mar 23 '10 #2
ipkiss
4
Thanks a lot for you answer,

"What do you mean by octet? Is that 8-bits or 8 bytes?" -> one octet means 1 byte -> 8bits

"If you can populate your array you have your data" -> could you please elaborate on this please. I could not figure out how to get data in such a way because I either could not know how to send data in such a way.

Regarding CRC: I got the CRC algorithm of that frame format. As long as I can get the CRC value of that frame, there is no issue.

Again, I just do not know how to send and receive the data in such a way in C.

Thanks a lot.
Mar 23 '10 #3
donbock
2,426 Expert 2GB
Start with an example. Pretend for a moment that the data field contains only four octets (two data items). Make up a couple of data items. Now what sequence of octets should be sent?
Mar 24 '10 #4
weaknessforcats
9,208 Expert Mod 8TB
|start | Command | Data | CRC | End |
|0x02| 0x41 | (0-127 octets) | | 0x03|

The 0x02 is the STX, or start-of-text.

You read the stream byte-by-byte until you find an STX.


Then you can read into your array until you reach the 0x03. The 0x03 is the ETX or end-of text.

Your array should contain the command+data octets+CRC.

This device does not use ITB (intermediate-text-block) to separate multiple command+data records passed as a block betweent the STX and ETX?
Mar 24 '10 #5
ipkiss
4
Thanks all,

I am trying you guys suggestion and let's you know the outcome as soon as possible
Mar 25 '10 #6
Banfa
9,065 Expert Mod 8TB
A couple of points

weaknessforcats I'm mildly surprised you have never heard the term octet before. It is in very common usage in the communication industry meaning 1 unsigned 8 bit byte.

ipkiss that looks similar to many communication protocols, from various bits of kit I have worked with over the years. Here is a couple of important questions:

Can start byte (STX(0x02)) and end byte (ETX(0x03)) values appear in the data? Or is the data only printable characters?

If they do appear in the data are they escaped in some way so that you know they are not actually the start and end of a message?

Just looking for STX may not be sufficient to find the start of a message but it is hard to know with such an incomplete description of the communication protocol.
Mar 25 '10 #7
weaknessforcats
9,208 Expert Mod 8TB
weaknessforcats I'm mildly surprised you have never heard the term octet before. It is in very common usage in the communication industry meaning 1 unsigned 8 bit byte.
When I did communications it was 1975 when it was called asynchronous communications. We use the tem octal but never octet. To us 8 bits was a byte.



Can start byte (STX(0x02)) and end byte (ETX(0x03)) values appear in the data? Or is the data only printable characters?
If they do appear in the data are they escaped in some way so that you know they are not actually the start and end of a message?
The data is ASCII characters only. The STX/ITB/ETX was called an envelope. The data was inside the envelope. The com driver used the STX/OTB/EXT to fill the receive buffer with the data. The envelope characters were discarded by the driver.

This type of envelope was use by an IBM protocol called BiSync. I haven't seen this used in many many years. Have a peek: http://telecom.tbi.net/bisync.htm
Mar 25 '10 #8
ipkiss
4
Hello guys,

I am testing the sending and receiving programs and got the issue:

In order to test with serial port, I used the socat (https://help.ubuntu.com/community/VirtualSerialPort ) to create a pair serial ports on Linux and test my program with these port.

The first time the program sends the data and the program receives data is ok. However, if I read again or even re-write the new data into the serial port, the return data is always null until I stop the virtual serial port and start it again, then the write and read data is ok, but still, only one time.

The sending and receiving code as I posted above, nothing changes.

Does anyone have any ideas?

Thanks a lot.
Mar 26 '10 #9
Heming
1
Hello ipkiss,

I have to write a program to read and write data from/to RS-232 on Ubuntu.
My data frame is exactly the same that ipkiss wrote:

start | Command | Data | CRC | End |
|0x02| 0x41 | (0-X bytes) | | 0x03|

Ipkis, could you tell me how you solved your problems?

If I use a canonical mode (http://www.easysw.com/~mike/serial/serial.html) I can read one line until "/n" but I would like to read until ETX (0x03).
Do you know how can I change this?
Thanks a lot
Oct 5 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Kathryn | last post by:
I hope you can help as this is driving me crazy! I have an asp which has 3 frames on it as follows: <FRAMESET rows="50%,*,10%"> <FRAME src="rundetailstop.asp" name="rundetailstop"...
4
by: Matteo | last post by:
Hy everybody. I'm not a html writer, but a sysadmin who's trying to help a user able to compile an online form with IE but not with Mozilla (Moz1.6, Ns7.1, Firefox 0.8+) due to a javascript date...
8
by: Jerry | last post by:
I have an off-the-shelf app that uses an Access database as its backend. One of the tables contains a field with an "OLE Object" datatype. I'm writing some reports against this database, and I...
5
by: Peter | last post by:
I have 3 ASP.NET forms written in C#, I also have 3 frames (left, top and bottom) one for each form. My question is if I have data on one form in the bottom frame how do I access this data when I...
3
by: Evan | last post by:
I have a web page with 2 frames. The left frame is running menu.aspx and the right frame is running images.aspx. When a selection is made in menu.aspx I call a method in images.aspx and pass a...
6
by: Ben Finney | last post by:
Howdy all, Summary: I'm looking for idioms in unit tests for factoring out repetitive iteration over test data. I explain my current practice, and why it's unsatisfactory. When following...
14
by: Rob Meade | last post by:
Hi all, I'm working on a project where there are just under 1300 course files, these are HTML files - my problem is that I need to do more with the content of these pages - and the thought of...
2
by: Katie | last post by:
I have created a report which sorts data by a time frame. However, if there is no data for a particular time frame then that row does not show. I need it to show with a blank for the value, e.g....
29
by: gs | last post by:
let say I have to deal with various date format and I am give format string from one of the following dd/mm/yyyy mm/dd/yyyy dd/mmm/yyyy mmm/dd/yyyy dd/mm/yy mm/dd/yy dd/mmm/yy mmm/dd/yy
2
by: Samuel R. Neff | last post by:
I'm using a quasi open-source project and am running into an exception in double.Parse which is effectively this: double.Parse(double.MinValue.ToString()) System.OverflowException: Value was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.