可以用任工的通信协议进行解析
This commit is contained in:
86
Core/Inc/Communication_Protocol.h
Normal file
86
Core/Inc/Communication_Protocol.h
Normal file
@ -0,0 +1,86 @@
|
||||
//
|
||||
// Created by zhu on 2024/1/31.
|
||||
//
|
||||
|
||||
#ifndef H750_CJSON_COMMUNICATION_PROTOCOL_H
|
||||
#define H750_CJSON_COMMUNICATION_PROTOCOL_H
|
||||
|
||||
|
||||
#ifndef COMMUNICATION_PROTOCOL_H
|
||||
#define COMMUNICATION_PROTOCOL_H
|
||||
|
||||
|
||||
#include <stdint.h>
|
||||
#include <Communication_struct.h>
|
||||
// DEFINE The protocol Here
|
||||
//Forexample
|
||||
// #define MYCOMAN 0x02 //命令全大写
|
||||
// to_do : define your protocol here
|
||||
/*-------------------------------------------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*-------------------------------------------------------------------------------------------------------------*/
|
||||
/*
|
||||
在此之下开始定义一些函数
|
||||
|
||||
|
||||
|
||||
|
||||
*/
|
||||
#define ERROR_NOT_ENOUGH_DATA -200
|
||||
#define ERROR_HEADER -300
|
||||
#define ERROR_COMMAND -400
|
||||
#define ERROR_INPUT -500
|
||||
#define ERROR_CRC -600
|
||||
|
||||
|
||||
// 成功返回打包后的数据长度
|
||||
// -1: Error
|
||||
// 成功返回打包后的数据长度
|
||||
int32_t IRIS_Protocol_Pack(uint8_t Command,uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData);
|
||||
|
||||
// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令 BufferOut 是输出
|
||||
// 下位机使用的打包函数 Command 是输出
|
||||
// 成功返回解包后的数据长度
|
||||
// 0: 该命令返回无参数
|
||||
// 错误返回ERRor
|
||||
// 成功返回解包后的数据长度
|
||||
int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut);
|
||||
|
||||
// 解包函数 PackData 是接收到的数据 LenthofIn 是数据长度 Command 是命令输入 BufferOut 是输出 上位机使用
|
||||
// 成功返回解包后的数据长度
|
||||
// 0: 该命令返回无参数
|
||||
// 错误返回ERRor
|
||||
// 成功返回解包后的数据长度
|
||||
int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut);
|
||||
|
||||
// 定义裁切命令
|
||||
// 成功返回裁切后的数据长度
|
||||
// -1: Error
|
||||
int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn );
|
||||
|
||||
// 检查数据是否有效
|
||||
// 有效返回值1
|
||||
// 错误返回ERRor
|
||||
int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn );
|
||||
|
||||
|
||||
// 返回CRC校验值
|
||||
uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif //H750_CJSON_COMMUNICATION_PROTOCOL_H
|
46
Core/Inc/Communication_struct.h
Normal file
46
Core/Inc/Communication_struct.h
Normal file
@ -0,0 +1,46 @@
|
||||
//
|
||||
// Created by zhu on 2024/1/31.
|
||||
//
|
||||
|
||||
#ifndef H750_CJSON_COMMUNICATION_STRUCT_H
|
||||
#define H750_CJSON_COMMUNICATION_STRUCT_H
|
||||
|
||||
#ifndef COMMUNICATION_STRUCT_H
|
||||
#define COMMUNICATION_STRUCT_H
|
||||
|
||||
// Define your communication structures here
|
||||
// For example:
|
||||
/*
|
||||
如果发送和接受的数据结构一样,那么就定义一个结构体
|
||||
struct MyComan_struct //对应的命令结构体 小驼峰命名法+struct
|
||||
{
|
||||
uint8_t Command;
|
||||
uint16_t LenthofIn;
|
||||
};
|
||||
|
||||
|
||||
|
||||
如果发送和接受的数据结构不一样,那么就分开定义
|
||||
struct MyComan_Send_struct //对应的命令结构体 小驼峰命名法+Send_struct
|
||||
{
|
||||
uint8_t Command;
|
||||
uint16_t LenthofIn;
|
||||
|
||||
};
|
||||
|
||||
struct MyComan_Recv_struct //对应的命令结构体 小驼峰命名法+Recv_struct
|
||||
{
|
||||
uint8_t Command;
|
||||
uint16_t LenthofOut;
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#endif // COMMUNICATION_STRUCT_H
|
||||
|
||||
|
||||
#endif //H750_CJSON_COMMUNICATION_STRUCT_H
|
186
Core/Src/Communication_Protocol.c
Normal file
186
Core/Src/Communication_Protocol.c
Normal file
@ -0,0 +1,186 @@
|
||||
#include "Communication_Protocol.h"
|
||||
#include "string.h"
|
||||
|
||||
|
||||
int32_t IRIS_Protocol_Pack(uint8_t Command,uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData)
|
||||
{
|
||||
if( PackData == NULL || (LenthofIn!=0 && BufferIn == NULL))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
PackData[0] = 0x55;
|
||||
PackData[1] = 0xAA;
|
||||
PackData[2] = Command;
|
||||
PackData[3] = LenthofIn & 0xFF;
|
||||
PackData[4] = (LenthofIn >> 8) & 0xFF;
|
||||
if(LenthofIn!=0)
|
||||
{
|
||||
memcpy(&PackData[5],BufferIn,LenthofIn);
|
||||
}
|
||||
uint16_t CRC = IRIS_calcCRC(PackData, LenthofIn+5);
|
||||
PackData[LenthofIn+5] = CRC & 0xFF;
|
||||
PackData[LenthofIn+6] = (CRC >> 8) & 0xFF;
|
||||
return LenthofIn+7;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
int32_t IRIS_STM32_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t *Command, uint8_t *BufferOut)
|
||||
{
|
||||
|
||||
if( PackData == NULL || BufferOut == NULL)
|
||||
{
|
||||
return ERROR_INPUT;
|
||||
}
|
||||
if(PackData[0] != 0x55 || PackData[1] != 0xAA)
|
||||
{
|
||||
return ERROR_HEADER;
|
||||
}
|
||||
|
||||
uint16_t LenthofOut = PackData[3] + (PackData[4] << 8);
|
||||
if(LenthofOut > LenthofIn - 7)
|
||||
{
|
||||
return ERROR_NOT_ENOUGH_DATA;
|
||||
}
|
||||
uint16_t CRC = IRIS_calcCRC(PackData, LenthofOut+5);
|
||||
if(CRC != (PackData[LenthofOut+5] + (PackData[LenthofOut+6] << 8)))
|
||||
{
|
||||
return ERROR_CRC;
|
||||
}
|
||||
if(LenthofOut == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
*Command = PackData[2] ;
|
||||
|
||||
memcpy(BufferOut,&PackData[5],LenthofOut);
|
||||
return LenthofOut;
|
||||
}
|
||||
|
||||
|
||||
int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut)
|
||||
{
|
||||
if( PackData == NULL || BufferOut == NULL)
|
||||
{
|
||||
return ERROR_INPUT;
|
||||
}
|
||||
if(PackData[0] != 0x55 || PackData[1] != 0xAA)
|
||||
{
|
||||
return ERROR_HEADER;
|
||||
}
|
||||
if(PackData[2] != Command)
|
||||
{
|
||||
return ERROR_COMMAND;
|
||||
}
|
||||
uint16_t LenthofOut = *(uint16_t *)(PackData+3)-2;
|
||||
if(LenthofOut > LenthofIn - 7)
|
||||
{
|
||||
return ERROR_NOT_ENOUGH_DATA;
|
||||
}
|
||||
uint16_t CRC = IRIS_calcCRC(PackData, LenthofOut+5);
|
||||
uint16_t CRC2=*(uint16_t *)(PackData+LenthofOut+5);
|
||||
if(CRC != CRC2)
|
||||
{
|
||||
return ERROR_CRC;
|
||||
}
|
||||
if(LenthofOut == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
memcpy(BufferOut,&PackData[5],LenthofOut);
|
||||
return LenthofOut;
|
||||
|
||||
}
|
||||
|
||||
|
||||
int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn )
|
||||
{
|
||||
if( PackData == NULL )
|
||||
{
|
||||
return ERROR_INPUT;
|
||||
}
|
||||
uint16_t i = 0;
|
||||
for(i = 0; i < LenthofIn; i++)
|
||||
{
|
||||
if(PackData[i] == 0x55 && PackData[i+1] == 0xAA)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(i == LenthofIn)
|
||||
{
|
||||
//清空数据
|
||||
memset(PackData,0,LenthofIn);
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint16_t LenthofOut = LenthofIn - i;
|
||||
memcpy(PackData,&PackData[i],LenthofOut);
|
||||
return LenthofOut;
|
||||
}
|
||||
|
||||
int32_t IRIS_Check_Data_Valid(uint8_t *PackData, uint16_t LenthofIn)
|
||||
{
|
||||
if( PackData == NULL)
|
||||
{
|
||||
return ERROR_INPUT;
|
||||
}
|
||||
if (LenthofIn < 7)
|
||||
{
|
||||
return ERROR_NOT_ENOUGH_DATA;
|
||||
/* code */
|
||||
}
|
||||
|
||||
if(PackData[0] != 0x55 || PackData[1] != 0xAA)
|
||||
{
|
||||
return ERROR_HEADER;
|
||||
}
|
||||
uint16_t LenthofOut = PackData[3] + (PackData[4] << 8);
|
||||
if(LenthofOut > LenthofIn - 7)
|
||||
{
|
||||
return ERROR_NOT_ENOUGH_DATA;
|
||||
}
|
||||
uint16_t CRC = IRIS_calcCRC(PackData, LenthofOut+5);
|
||||
if(CRC != (PackData[LenthofOut+5] + (PackData[LenthofOut+6] << 8)))
|
||||
{
|
||||
return ERROR_CRC;
|
||||
}
|
||||
return 1;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize)
|
||||
{
|
||||
const uint8_t *pBytesArray = (const uint8_t*)pBuffer;
|
||||
uint16_t poly = 0x8408;
|
||||
uint16_t crc = 0;
|
||||
uint8_t carry;
|
||||
uint8_t i_bits;
|
||||
uint16_t j;
|
||||
for (j =0; j < bufferSize; j++)
|
||||
{
|
||||
crc = crc ^ pBytesArray[j];
|
||||
for (i_bits = 0; i_bits < 8; i_bits++)
|
||||
{
|
||||
carry = crc & 1;
|
||||
crc = crc / 2;
|
||||
if (carry)
|
||||
{
|
||||
crc = crc^poly;
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
@ -8,8 +8,9 @@
|
||||
#include "stdlib.h"
|
||||
#include "cJSON_Test.h"
|
||||
#include "SerialDataProcess.h"
|
||||
|
||||
#include "Communication_Protocol.h"
|
||||
extern bool Printf_Flag ;
|
||||
uint8_t cjson_buf[200];
|
||||
void SerialDataProcess()
|
||||
{
|
||||
uint8_t len;
|
||||
@ -17,9 +18,15 @@ void SerialDataProcess()
|
||||
{
|
||||
len = g_usart_rx_sta & 0x3FFF; /* <20><>ȡ<EFBFBD><C8A1>Ч<EFBFBD><D0A7><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD> */
|
||||
g_usart_rx_buf[len] = '\0'; /* ĩβ<C4A9><CEB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
// printf("<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD>JSON<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
|
||||
// printf(" %s\r\n",g_usart_rx_buf);
|
||||
cjson(g_usart_rx_buf);
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD>JSON<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: ");
|
||||
printf(" %s\r\n",g_usart_rx_buf);
|
||||
|
||||
//IRIS_Cut_Befor_Header(g_usart_rx_buf,len);
|
||||
IRIS_Protocol_Unpack(g_usart_rx_buf, len, 0x00, cjson_buf);
|
||||
printf("<EFBFBD>и<EFBFBD>֮<EFBFBD><EFBFBD><EFBFBD><EFBFBD> %s\r\n",cjson_buf);
|
||||
cjson(cjson_buf);
|
||||
//
|
||||
|
||||
g_usart_rx_sta = 0;
|
||||
memset(g_usart_rx_buf, 0, sizeof(g_usart_rx_buf));
|
||||
}else
|
||||
|
@ -117,12 +117,18 @@ int main(void)
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
|
||||
SerialDataProcess();
|
||||
if(Printf_Flag== true){
|
||||
Judge();
|
||||
Printf_Flag = false;
|
||||
}
|
||||
|
||||
|
||||
//uint8_t p[2];
|
||||
//p[0] = 0x55;
|
||||
//p[1] = 0xaa;
|
||||
// HAL_UART_Transmit(&huart3, p, 2, 10);
|
||||
// paramstruct ->age = 20;
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user