Parse and read data frame in C?

后端 未结 2 948
梦如初夏
梦如初夏 2021-01-14 16:40

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

|start | Comm         


        
相关标签:
2条回答
  • 2021-01-14 17:28

    result is an array of chars, which are 1 octet wide.

    to read octet n use:

    char octet_n = result[n];
    

    So to do what you want you need:

    // skip the start and command fields
    char *data_field = result + 2; 
    
    int octet_1_2 = data_field[1] | (data_field[2] << 8);
    int octet_3_4 = data_field[3] | (data_field[4] << 8);
    
    // crc is at byte 128 + 2 = 130
    int crc = result[130];
    

    Edit: An explanation for this line:

    int octet_1_2 = data_field[1] | (data_field[2] << 8);
    

    You want to read two consecutive octets into one 16-bit word:

                1
           bits 5        8 7       0
                --------------------
    octet_1_2 = | octet 2 | octet 1|
                --------------------
    

    So you want to take bits 7:0 of octet 1 and put them in bits 7:0 of octet_1_2:

    octet_1_2 = data_field[1];
    

    Then you want to take bits 7:0 of octet 2 and put them in bits 15:8 of octet_1_2. You do this by shifting octet 2 8 bits to the left, and OR'ing the result into octet_1_2:

    octet_1_2 |= data_field[2] << 8;
    

    These two lines can be merged into one as I did above.

    0 讨论(0)
  • 2021-01-14 17:28

    The best thing to read formatted data in C is to read a structure. Given the frame format you have I would do the following.

    
    typedef struct special_data
    {
        char first_data[2];
        char second data[2];
    } special_data_t;
    
    union data_u
    {
        special_data_t my_special_data;
        char           whole_data[128];
    };
    
    typedef struct data_frame
    {
        unsigned char start;
        unsigned char cmd;
        union data_u  data;
        unsigned char crc;
        unsigned char end;
    } data_frame_t;
    
    void func_read(int fd)
    {
        data_frame_t my_data;
    
        if (read(fd, &my_data, sizeof(my_data)) != -1)
        {
            // Do something
        }
        return;
    }
    

    This way you may access the data you need through the structure fields. The first structure and the union are just helpers to access the bytes you need in the data field of the frame.

    0 讨论(0)
提交回复
热议问题