使用C語言自定義序列化與反序列化,快速實現自定義協議,二進制傳輸,類似protobuf

前言:有時我在寫二進制傳輸協議時,當不使用標準的協議傳輸時每次項目都要想協議的格式,下面的代碼實現了一個標準的二進制協議語法,加密使用異或加密,對除載荷外的字段進行了加密,當然如果你的硬件比較好也可以全部加密。當然你也可以用protobuf,這裏使用c語言實現,其他語言可以實現
協議格式
基本數據結構

//編程網站查看
// http://www.dooccn.com/c/#id/c85fa97f81596701975d3ae7b9346725
//位數一定要和平臺相同
typedef unsigned int  u32;
typedef unsigned short u16;
typedef unsigned char  u8;


#include "string.h"
#include "stdio.h"
#include "stdlib.h"

#define LOCK1                   'D'
#define LOCK2                   'E'
#define R_CODE                  0XF1//異或密碼
#define RE_SEND_BUFFER          22

//協議1字節對齊
typedef struct{
    u8  MsgHead[2];//頭字節
    u8  MsgLength;//有效數據長度
    u8  MsgClass;//消息類
    u16 MsgCrc16;//校驗
    u8  MsgPayLoad[RE_SEND_BUFFER];//消息載荷
}MsgProtocol_t;

//格式化
typedef struct
{
    void (*FmtU8)(u8 d);
    void (*FmtU16)(u16 d);
    void (*FmtU32)(u32 d);
    void (*FmtStr)(char* str);
    void (*FmtHex)(u8* Dt,u16 len);
    void (*FmtHead)(void);
    void (*Fmtend)(void);
    void (*FmtCmd)(u16 cmd);
    void (*FmtCrc16)(void);
    u8*  (*GetBuffer)(void);
    u16  (*GetLength)(void);
    
}ProtocolFormat_t;

MsgProtocol_t transferProtocol;
ProtocolFormat_t* Vest_TxRxProtocol = NULL;
static u32 SendProtocolLength = 0;

//CRC16高
const u8 CRC_Array_H[] = 
{
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 
    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 
    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 
    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 
    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 
};
//CRC16低
const u8 CRC_Array_L[] = 
{
    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 
    0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 
    0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 
    0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 
    0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 
    0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 
    0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 
    0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 
    0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 
    0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 
    0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 
    0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 
    0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 
    0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 
    0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 
    0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 
    0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 
    0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 
    0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 
    0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 
    0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 
    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 
    0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 
    0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 
    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 
    0x43, 0x83, 0x41, 0x81, 0x80, 0x40
};
u16 Message_count_CRC_2(u8 CrcLenth,u8 *CRC_Data)
{
    u8  Crc_H = 0xFF;
    u8  Crc_L = 0xFF;
    u8  Index;
    while (CrcLenth--)
    {  
      Index = Crc_H ^ *CRC_Data++;
        Crc_H = Crc_L ^  CRC_Array_H[Index]; 
        Crc_L = CRC_Array_L[Index]; 
    }
    return((Crc_H << 8) | Crc_L); 
}

//8
static void ThisFmtU8(u8 d)
{
    transferProtocol.MsgPayLoad[SendProtocolLength++] = d;
}
//16
static void ThisFmtU16(u16 d)
{
    memcpy(&transferProtocol.MsgPayLoad[SendProtocolLength],&d,2);
    SendProtocolLength += 2;
}
//32
static void ThisFmtU32(u32 d)
{
    memcpy(&transferProtocol.MsgPayLoad[SendProtocolLength],&d,4);
    SendProtocolLength += 4;
}
//str
static void ThisFmtStr(char* str)
{
    memcpy(&transferProtocol.MsgPayLoad[SendProtocolLength],str,strlen(str));
    SendProtocolLength += strlen(str);
}
//cmd
static void ThisFmtCmd(u16 cmd)
{
    u16 tmp_cmd = cmd ^ R_CODE;
    transferProtocol.MsgClass = tmp_cmd;
}
//heard
static void ThisFmtHead(void)
{
    SendProtocolLength = 0;
    //頭
    transferProtocol.MsgHead[0] = LOCK1 ^ R_CODE;
    transferProtocol.MsgHead[1] = LOCK2 ^ R_CODE;
}
//crc16
static void ThisFmtCrc16(void)
{
    transferProtocol.MsgCrc16 = Message_count_CRC_2(SendProtocolLength,transferProtocol.MsgPayLoad)^R_CODE;
}
//end
static void ThisFmtend(void)
{
    //總數居長度
    transferProtocol.MsgLength = (SendProtocolLength + (sizeof(MsgProtocol_t) -RE_SEND_BUFFER)) ^R_CODE;
}
//get buffer
static  u8* ThisGetBuffer(void)
{
    return (u8*)&transferProtocol;
}
//hex
static void ThisFmtHex(u8* Dt,u16 Len)
{
    memcpy(&transferProtocol.MsgPayLoad[SendProtocolLength],Dt,Len);
    SendProtocolLength += Len;
}
//得到數據包總長度
static u16 ThisGetLength(void)
{
    return SendProtocolLength + (sizeof(MsgProtocol_t) -RE_SEND_BUFFER);
}
//get object
ProtocolFormat_t* ProtocolFormatNew(void)
{
	//如果使用的C99編譯器的話可以直接初始化賦值
	//這裏使用的是靜態代表每次獲取實例的對象是一樣的
	//要想不同可以使用malloc分配內存
    static ProtocolFormat_t SendProtocol;
    SendProtocol.FmtU8              = ThisFmtU8;
    SendProtocol.FmtU16             = ThisFmtU16;
    SendProtocol.FmtU32             = ThisFmtU32;
    SendProtocol.FmtHex             = ThisFmtHex,
    SendProtocol.FmtStr             = ThisFmtStr;
    SendProtocol.FmtHead            = ThisFmtHead;
    SendProtocol.Fmtend             = ThisFmtend;
    SendProtocol.FmtCmd             = ThisFmtCmd;
    SendProtocol.FmtCrc16           = ThisFmtCrc16;
    SendProtocol.GetBuffer          = ThisGetBuffer;
    SendProtocol.GetLength          = ThisGetLength;
    return &SendProtocol;
}

//接收端對協議分析還原
MsgProtocol_t* Protocol_Analysis(void* d)
{
    MsgProtocol_t *p = (MsgProtocol_t*)d;
    p->MsgHead[0]    = p->MsgHead[0] ^ R_CODE;
    p->MsgHead[1]    = p->MsgHead[1] ^ R_CODE;
    p->MsgClass      = p->MsgClass ^ R_CODE;
    p->MsgLength     = p->MsgLength ^ R_CODE;
    p->MsgCrc16      = p->MsgCrc16 ^ R_CODE;
    return p;
}

//測試buf
static u8 DisplayBuffer[128];

//序列化
static void TestCTL1(void)
{
    u8 i = 0;
    Vest_TxRxProtocol->FmtHead();
    Vest_TxRxProtocol->FmtCmd(0x61);
    Vest_TxRxProtocol->FmtU8(0x01);
    Vest_TxRxProtocol->FmtCrc16();
    Vest_TxRxProtocol->Fmtend();
    memcpy(DisplayBuffer,Vest_TxRxProtocol->GetBuffer(),Vest_TxRxProtocol->GetLength());
    for(i=0;i < Vest_TxRxProtocol->GetLength();i++)
    printf("%02x ",DisplayBuffer[i]);
    printf("\n");
}
//反序列化
//反序列化測試
static u8 DecodeTestBuffer[7] = {0xb5,0xb4,0xf6,0x90,0x71,0x7e,0x01};
static void DecodeTestHandler(void* p,u16 length)
{
    u8 *Data = (u8*)p;
    u16 indexof = 0;
    u8 i =0;
    if(length > 6)
    {
        while( (length-indexof) >= 7)//解包
        {
            char head[2];
            head[0] = Data[indexof] ^ R_CODE;
            head[1] = Data[indexof+1] ^ R_CODE;
            if(head[0] == LOCK1 && head[1] == LOCK2)
            {
                MsgProtocol_t *p = Protocol_Analysis(&Data[indexof]);
                printf("cmd=0x%02x,crc16=0x%02x,len=0x%02x\n",p->MsgClass,p->MsgCrc16,p->MsgLength);
                printf("Data: ");
                for(i=0;i<p->MsgLength-6;i++)
                {
                    printf("%02X ",p->MsgPayLoad[i]);
                }
                indexof += p->MsgLength;//下一包數據
            }
            else{
                indexof++;
            }
        }
    }
}

int main(void)
{
    Vest_TxRxProtocol = ProtocolFormatNew();
    TestCTL1();
    DecodeTestHandler(DecodeTestBuffer,7);
    
}

//C語言在線測試網站
//http://www.dooccn.com/c/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章