Compare commits

...

11 Commits

6 changed files with 292 additions and 154 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
/.vscode/
.idea

View File

@ -11,8 +11,8 @@
- 0x01: 命令为字符串 即Data要转换为字符串
- 0x00: 命令为json数据应用json解析Data
- 0x02-0xff:自定义二进制通讯数据Data的解析应改按照Communication_Protocol中约定进行
- **长度**Data+Check的总字节数
- **Check**:长度+Data的 CRC校验 算法在Communication_Protocol提供了如果Data过长光谱数据可以不计算但是得提供此时提供内容为0xEE 0xEE
- **长度**Data的长度
- **Check**:Data的 CRC校验 算法在Communication_Protocol提供了如果Data过长光谱数据可以不计算但是得提供此时提供内容为0xEE 0xEE
- **均采用小端方式存储**
@ -21,21 +21,21 @@
json定义比较灵活 但是根对象必须提供如下:
- CN: 命令名称
- command: 命令名称
- PA:命令参数 //发送时必须 返回可选
- PA:命令参数 //可选
返回时应提供相应状态
- STOK
- STOK //可选
发送示例:
```json
{
"CN":"SetCamerTime",
"command":"SetCamerTime",
"PA":{
"time1":1000,
"time2":1000
@ -49,7 +49,7 @@ json定义比较灵活 但是根对象必须提供如下:
```json
{
"CN":"SetCamerTime",
"command":"SetCamerTime",
"Other":"自己定义",
"ST":"OK"
}
@ -69,7 +69,7 @@ json定义比较灵活 但是根对象必须提供如下:
#include <stdint.h>
#include <Communication_struct.h>
// 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

View File

@ -1,119 +0,0 @@
#include "Communication_Protocol.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_Protocol_Unpack(uint8_t *PackData, uint16_t LenthofIn, uint8_t Command, uint8_t *BufferOut)
{
if( PackData == NULL || BufferOut == NULL)
{
return -100;
}
if(PackData[0] != 0x55 || PackData[1] != 0xAA)
{
return -200;
}
if(PackData[2] != Command)
{
return -300;
}
uint16_t LenthofOut = PackData[3] + (PackData[4] << 8);
if(LenthofOut > LenthofIn - 7)
{
return -400;
}
uint16_t CRC = IRIS_calcCRC(PackData, LenthofOut+5);
if(CRC != (PackData[LenthofOut+5] + (PackData[LenthofOut+6] << 8)))
{
return -1;
}
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 -1;
}
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;
}
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;
}

View File

@ -4,15 +4,32 @@
#include <stdint.h>
#include "IRIS_Method.h"
#include <Communication_struct.h>
// DEFINE The protocol Here
//Forexample
// #define MYCOMAN 0x02 //命令全大写
// todo : define your protocol here
/*-------------------------------------------------------------------------------------------------------------*/
#define GET_BASIC_INFO 0x50
#define SET_INTEGRAL_TIME 0x51
#define SET_AUTO_EXPOSURE 0x52
#define GET_INTEGRAL_TIME_AND_DATA_PROCESS 0x53
#define GET_TEMPRETURE 0x54
#define SET_SHUTTER_OPEN 0x56
#define SET_SHUTTER_CLOSE 0x55
#define GET_BANDNUMBER 0x57
#define SET_WEAVE_COEFF 0x58
#define GET_WEAVE_COEFF 0x59
#define SET_DATA_PROCESS_METHOD 0x60
#define GET_DATA_FROM_SENSOR 0x61
#define NONE_DATA 0x00
@ -24,29 +41,11 @@
/*-------------------------------------------------------------------------------------------------------------*/
/*
在此之下开始定义一些函数
*/
// 成功返回打包后的数据长度
// -1: Error
// 成功返回打包后的数据长度
int32_t IRIS_Protocol_Pack(uint8_t Command,uint16_t LenthofIn, uint8_t *BufferIn, uint8_t *PackData);
// 成功返回解包后的数据长度
// 0: 该命令返回无参数
// -1: checksum error
// -100: parameter error
// -200: header error
// -300: command error
// -400: length 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 );
// 返回CRC校验值
uint16_t IRIS_calcCRC(const void *pBuffer, uint16_t bufferSize);
#endif

175
src/IRIS_Method.cpp Normal file
View File

@ -0,0 +1,175 @@
/**
******************************************************************************
* @file : IRIS_Method.c
* @author : xin
* @brief : None
* @attention : None
* @date : 2024/2/1
******************************************************************************
*/
//
// Created by xin on 2024/2/1.
//
#include "IRIS_Method.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;
uint16_t datalenth = LenthofIn;
PackData[3] = (datalenth >> 8) & 0xFF;
PackData[4] = datalenth & 0xFF;
if (LenthofIn != 0) {
memcpy(&PackData[5], BufferIn, LenthofIn);
}
uint16_t crcbytelenth = LenthofIn;
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, crcbytelenth);
PackData[LenthofIn + 5] = (CRC_CRC >> 8) & 0xFF;
PackData[LenthofIn + 6] = CRC_CRC & 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[4] + (PackData[3] << 8); //减去CRC的两个字节
if (LenthofOut > LenthofIn - 7) {
return ERROR_NOT_ENOUGH_DATA;
}
if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) {
} else {
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, LenthofOut);
if (CRC_CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 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 = PackData[4] + (PackData[3] << 8);
if (LenthofOut > LenthofIn - 7) {
return ERROR_NOT_ENOUGH_DATA;
}
if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) {
} else {
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, LenthofOut);
if (CRC_CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 8))) {
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;
}
if (LenthofIn <2) {
return LenthofIn;
}
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[4] + (PackData[3] << 8);
if (LenthofOut > LenthofIn - 7) {
return ERROR_NOT_ENOUGH_DATA;
}
if (PackData[LenthofOut + 6] == 0xEE && PackData[LenthofOut + 5] == 0xEE) {
} else {
uint16_t CRC_CRC = IRIS_calcCRC(PackData + 5, LenthofOut);
if (CRC_CRC != (PackData[LenthofOut + 6] + (PackData[LenthofOut + 5] << 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;
}

60
src/IRIS_Method.h Normal file
View File

@ -0,0 +1,60 @@
/**
******************************************************************************
* @file : IRIS_Method.h
* @author : xin
* @brief : None
* @attention : None
* @date : 2024/2/1
******************************************************************************
*/
//
// Created by xin on 2024/2/1.
//
#ifndef IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H
#define IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H
#define ERROR_NOT_ENOUGH_DATA -200
#define ERROR_HEADER -300
#define ERROR_COMMAND -400
#define ERROR_INPUT -500
#define ERROR_CRC -600
#include <stdint.h>
//#include<Arduino.h>
// 成功返回打包后的数据长度
// -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 //IRIS_COMMUNICATION_PROTOCOL_IRIS_METHOD_H