Home > Back-end >  Excuse me each bosses, variable length data domain protocol frame parsing problem
Excuse me each bosses, variable length data domain protocol frame parsing problem

Time:03-19

Suppose there is such a protocol consists of the following:

1. 2 bytes of data frames a start bit AA DD
2. The data frame length 2 bytes 00 XX
3. Data domain variable length per 4 bytes for a group of such as 0 0 0 65, 0 f E E represents temperature 0 f c
4. Check digit 2 bytes

At present is to parse the data domain out

Typedef struct
{
Uint8_t frame_head [2].
Uint8_t frame_length [2].
uint8_t frame_datafield [256].
Uint8_t frame_checkbit [2].
} myselfprotocal;

First of all, a serial port 1 frame head operator does not comply with the

If (g_tBspUart1. The flag==1 & amp; & G_tBspUart1. PRxBuf [0]==AA & amp; & G_tBspUart1. PRxBuf [1]==DD)

Memcpy (& amp; Myselfprotocal_t, & amp; G_tBspUart1 pRxBuf, g_tBspUart1. RxLen);

4 bytes data domain as a set, assuming that there are 5 groups, but can you tell me how to resolve it? Such as 0 0 f e 00 00 0 c 65 03 02 00 0 a 23 such format

Thank you for your attention

CodePudding user response:

Do you have the data frame length, so is, in fact, you ask?

CodePudding user response:

| start | Command | Data CRC | |
| 0 xaa | 0 XDD | part of the need to parse | |
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Means how to frame_datafield [] element in the array according to four a set of a set of extracted respectively

CodePudding user response:

Uint8_t * frame=... ;//take the lead in full frame first address
Uint8_t (* data) [4]=(uint8_t * [4]) (frame + 4);
Short int len=(* (short) int * (frame + 2) - (4)/4.//assume that the frame length including calibration head does not include the tail
int i;
for(i=0; i{
Do_someting_on data [I];
}

CodePudding user response:

Thank you for your ideas

CodePudding user response:

2 bytes of data frame length must pay attention to the high and low byte sequence, typically using socket function conversion to each other:
Htons (), ntohs ()

CodePudding user response:

reference forever74 reply: 3/f
uint8_t * frame=... ;//take the lead in full frame first address
Uint8_t (* data) [4]=(uint8_t * [4]) (frame + 4);
Short int len=(* (short) int * (frame + 2) - (4)/4.//assume that the frame length including calibration head does not include the tail
int i;
for(i=0; i{
Do_someting_on data [I];
}


Hello, I contact the programming time is not long, can you tell me how to understand this code? Thank you
(uint8_t * [4]) (frame + 4);

Is (uint8_t * [4]) such expressions have not seen the

And (* (short int *) (frame + 2) - (4)

CodePudding user response:

Transform pointer type is,
Frame is a pointer, tube 1 byte, frame + 4,
It cast to uint8_t * [4] type is a pointer variable data type, is to make it into a tube 4 bytes of pointer type, used to data pointer assignment,

Below that, a solution of reference is the goal, but not consider the byte order, if the byte order is wrong, can't write,

CodePudding user response:

Said was waiting for you to handle full frame while receiving treatment is of high efficiency,

CodePudding user response:

reference 5 floor bluesen reply:
2 bytes of data frame length must pay attention to the high and low byte sequence, typically using socket function conversion to each other:
Htons (), ntohs ()


Although this problem does not involve to distinguish the big end little end still very thank you for let me know these two functions is quite practical thank you!

CodePudding user response:

refer to 7th floor forever74 response:
transform pointer type, is
Frame is a pointer, tube 1 byte, frame + 4,
It cast to uint8_t * [4] type is a pointer variable data type, is to make it into a tube 4 bytes of pointer type, used to data pointer assignment,

Below that, a solution of reference is the goal, but not to consider the byte order, if the byte order is wrong, can't write,


Thank you very much for spending time on my study to digest
  • Related