Various forms of protocol writing

Various forms of protocol writing

When receiving, the high bit is received first. Receive the first row first, then the second row.

Big endian mode (most significant bit first)

Data format
insert image description here
Method 1:

#include <stdio.h>

typedef struct
{
     int byte1:8;
     int byte2:8;
}bit8_bi16;

typedef union
{
    int bit16;
    bit8_bi16 data;
}merge;

int main()
{

     int a[2] = {0xab,0xcd};//从串口按顺序接收
    //进行数据处理
    //大端模式的读出
    merge d;
    d.data.byte1 = a[1];   //顺序跟接收顺序相反
    d.data.byte2 = a[0];
    printf("大端模式的读出:%x\n",d.data);
    return 0;
}

Method Two:

int main()
{

     int a[2] = {0xab,0xcd};//从串口按顺序接收
     int c = (a[0]<<8)|(a[1]);
     printf("大端模式的读出:%x\n",c);
    return 0;
}

The result is as follows:
insert image description here

Guess you like

Origin blog.csdn.net/qq_45159887/article/details/129740208