适配下位机STM32 unpack
This commit is contained in:
@ -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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user