From 5c6fe7b3119450232b3d31a8387c32d3719c990d Mon Sep 17 00:00:00 2001 From: xin Date: Thu, 25 Jan 2024 13:10:47 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E4=B8=8B=E4=BD=8D=E6=9C=BAST?= =?UTF-8?q?M32=20unpack?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++ .idea/IRIS_Communication_Protocol.iml | 8 ++ .idea/deployment.xml | 105 ++++++++++++++++++++++++++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ Readme.md | 34 +++++++-- src/Communication_Protocol.c | 81 +++++++++++++++++--- src/Communication_Protocol.h | 32 ++++++-- 8 files changed, 262 insertions(+), 20 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/IRIS_Communication_Protocol.iml create mode 100644 .idea/deployment.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/IRIS_Communication_Protocol.iml b/.idea/IRIS_Communication_Protocol.iml new file mode 100644 index 0000000..bc2cd87 --- /dev/null +++ b/.idea/IRIS_Communication_Protocol.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/deployment.xml b/.idea/deployment.xml new file mode 100644 index 0000000..02235a3 --- /dev/null +++ b/.idea/deployment.xml @@ -0,0 +1,105 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..8d63296 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/Readme.md b/Readme.md index c3f149e..c0ffeeb 100644 --- a/Readme.md +++ b/Readme.md @@ -69,7 +69,7 @@ json定义比较灵活 但是根对象必须提供如下: #include #include // DEFINE The protocol Here - //Forexample +//Forexample // #define MYCOMAN 0x02 //命令全大写 // todo : define your protocol here /*-------------------------------------------------------------------------------------------------------------*/ @@ -87,19 +87,35 @@ json定义比较灵活 但是根对象必须提供如下: /*-------------------------------------------------------------------------------------------------------------*/ /* 在此之下开始定义一些函数 + + + + */ +#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: 该命令返回无参数 -// -1: checksum error -// -100: parameter error -// -200: header error -// -300: command error -// -400: length error +// 错误返回ERRor // 成功返回解包后的数据长度 int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut); @@ -108,6 +124,12 @@ int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Comm // -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 diff --git a/src/Communication_Protocol.c b/src/Communication_Protocol.c index fd694a0..7f583e8 100644 --- a/src/Communication_Protocol.c +++ b/src/Communication_Protocol.c @@ -19,37 +19,70 @@ int32_t IRIS_Protocol_Pack(uint8_t Command,uint16_t LenthofIn, uint8_t *BufferIn 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 -100; + return ERROR_INPUT; } if(PackData[0] != 0x55 || PackData[1] != 0xAA) { - return -200; + return ERROR_HEADER } if(PackData[2] != Command) { - return -300; + return ERROR_COMMAND; } uint16_t LenthofOut = PackData[3] + (PackData[4] << 8); if(LenthofOut > LenthofIn - 7) { - return -400; + return ERROR_NOT_ENOUGH_DATA; } uint16_t CRC = IRIS_calcCRC(PackData, LenthofOut+5); if(CRC != (PackData[LenthofOut+5] + (PackData[LenthofOut+6] << 8))) { - return -1; + return ERROR_CRC; } if(LenthofOut == 0) { @@ -65,7 +98,7 @@ int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn ) { if( PackData == NULL ) { - return -1; + return ERROR_INPUT } uint16_t i = 0; for(i = 0; i < LenthofIn; i++) @@ -87,6 +120,36 @@ int32_t IRIS_Cut_Befor_Header(uint8_t *PackData, uint16_t LenthofIn ) 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; + +} + diff --git a/src/Communication_Protocol.h b/src/Communication_Protocol.h index 2274547..23426ba 100644 --- a/src/Communication_Protocol.h +++ b/src/Communication_Protocol.h @@ -24,19 +24,35 @@ /*-------------------------------------------------------------------------------------------------------------*/ /* 在此之下开始定义一些函数 + + + + */ +#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: 该命令返回无参数 -// -1: checksum error -// -100: parameter error -// -200: header error -// -300: command error -// -400: length error +// 错误返回ERRor // 成功返回解包后的数据长度 int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut); @@ -45,6 +61,12 @@ int32_t IRIS_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Comm // -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