2022-11-03 20:38:55 +08:00
|
|
|
|
/**
|
|
|
|
|
* @file seasky_protocol.c
|
|
|
|
|
* @author Liu Wei
|
|
|
|
|
* @author modified by Neozng
|
|
|
|
|
* @brief 湖南大学RoBoMatster串口通信协议
|
|
|
|
|
* @version 0.1
|
|
|
|
|
* @date 2022-11-03
|
2022-11-29 20:00:46 +08:00
|
|
|
|
*
|
2022-11-03 20:38:55 +08:00
|
|
|
|
* @copyright Copyright (c) 2022
|
2022-11-29 20:00:46 +08:00
|
|
|
|
*
|
2022-11-03 20:38:55 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "seasky_protocol.h"
|
|
|
|
|
#include "crc8.h"
|
|
|
|
|
#include "crc16.h"
|
2022-11-29 16:33:34 +08:00
|
|
|
|
#include "memory.h"
|
2022-11-03 20:38:55 +08:00
|
|
|
|
|
2023-03-22 16:37:12 +08:00
|
|
|
|
/*获取CRC8校验码*/
|
|
|
|
|
uint8_t Get_CRC8_Check(uint8_t *pchMessage,uint16_t dwLength)
|
|
|
|
|
{
|
|
|
|
|
return crc_8(pchMessage,dwLength);
|
|
|
|
|
}
|
2022-11-03 20:38:55 +08:00
|
|
|
|
/*检验CRC8数据段*/
|
2022-11-29 16:33:34 +08:00
|
|
|
|
static uint8_t CRC8_Check_Sum(uint8_t *pchMessage, uint16_t dwLength)
|
2022-11-03 20:38:55 +08:00
|
|
|
|
{
|
|
|
|
|
uint8_t ucExpected = 0;
|
|
|
|
|
if ((pchMessage == 0) || (dwLength <= 2))
|
|
|
|
|
return 0;
|
2022-11-29 16:33:34 +08:00
|
|
|
|
ucExpected = crc_8(pchMessage, dwLength - 1);
|
2022-11-03 20:38:55 +08:00
|
|
|
|
return (ucExpected == pchMessage[dwLength - 1]);
|
|
|
|
|
}
|
2022-11-29 16:33:34 +08:00
|
|
|
|
|
2023-03-22 16:37:12 +08:00
|
|
|
|
/*获取CRC16校验码*/
|
|
|
|
|
uint16_t Get_CRC16_Check(uint8_t *pchMessage,uint32_t dwLength)
|
|
|
|
|
{
|
|
|
|
|
return crc_16(pchMessage,dwLength);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-03 20:38:55 +08:00
|
|
|
|
/*检验CRC16数据段*/
|
2022-11-29 16:33:34 +08:00
|
|
|
|
static uint16_t CRC16_Check_Sum(uint8_t *pchMessage, uint32_t dwLength)
|
2022-11-03 20:38:55 +08:00
|
|
|
|
{
|
|
|
|
|
uint16_t wExpected = 0;
|
|
|
|
|
if ((pchMessage == 0) || (dwLength <= 2))
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-11-29 16:33:34 +08:00
|
|
|
|
wExpected = crc_16(pchMessage, dwLength - 2);
|
2022-11-03 20:38:55 +08:00
|
|
|
|
return (((wExpected & 0xff) == pchMessage[dwLength - 2]) && (((wExpected >> 8) & 0xff) == pchMessage[dwLength - 1]));
|
|
|
|
|
}
|
2022-11-29 16:33:34 +08:00
|
|
|
|
|
2022-11-03 20:38:55 +08:00
|
|
|
|
/*检验数据帧头*/
|
2022-11-29 20:00:46 +08:00
|
|
|
|
static uint8_t protocol_heade_Check(protocol_rm_struct *pro, uint8_t *rx_buf)
|
2022-11-03 20:38:55 +08:00
|
|
|
|
{
|
|
|
|
|
if (rx_buf[0] == PROTOCOL_CMD_ID)
|
|
|
|
|
{
|
|
|
|
|
pro->header.sof = rx_buf[0];
|
|
|
|
|
if (CRC8_Check_Sum(&rx_buf[0], 4))
|
|
|
|
|
{
|
|
|
|
|
pro->header.data_length = (rx_buf[2] << 8) | rx_buf[1];
|
|
|
|
|
pro->header.crc_check = rx_buf[3];
|
|
|
|
|
pro->cmd_id = (rx_buf[5] << 8) | rx_buf[4];
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
此函数根据待发送的数据更新数据帧格式以及内容,实现数据的打包操作
|
|
|
|
|
后续调用通信接口的发送函数发送tx_buf中的对应数据
|
|
|
|
|
*/
|
2022-11-29 20:00:46 +08:00
|
|
|
|
void get_protocol_send_data(uint16_t send_id, // 信号id
|
2022-11-03 20:38:55 +08:00
|
|
|
|
uint16_t flags_register, // 16位寄存器
|
2022-11-29 20:00:46 +08:00
|
|
|
|
float *tx_data, // 待发送的float数据
|
2022-11-03 20:38:55 +08:00
|
|
|
|
uint8_t float_length, // float的数据长度
|
2022-11-29 20:00:46 +08:00
|
|
|
|
uint8_t *tx_buf, // 待发送的数据帧
|
|
|
|
|
uint16_t *tx_buf_len) // 待发送的数据帧长度
|
2022-11-03 20:38:55 +08:00
|
|
|
|
{
|
|
|
|
|
static uint16_t crc16;
|
|
|
|
|
static uint16_t data_len;
|
|
|
|
|
|
|
|
|
|
data_len = float_length * 4 + 2;
|
|
|
|
|
/*帧头部分*/
|
|
|
|
|
tx_buf[0] = PROTOCOL_CMD_ID;
|
2022-11-29 20:00:46 +08:00
|
|
|
|
tx_buf[1] = data_len & 0xff; // 低位在前
|
|
|
|
|
tx_buf[2] = (data_len >> 8) & 0xff; // 低位在前
|
|
|
|
|
tx_buf[3] = crc_8(&tx_buf[0], 3); // 获取CRC8校验位
|
2022-11-03 20:38:55 +08:00
|
|
|
|
|
|
|
|
|
/*数据的信号id*/
|
|
|
|
|
tx_buf[4] = send_id & 0xff;
|
|
|
|
|
tx_buf[5] = (send_id >> 8) & 0xff;
|
|
|
|
|
|
|
|
|
|
/*建立16位寄存器*/
|
|
|
|
|
tx_buf[6] = flags_register & 0xff;
|
|
|
|
|
tx_buf[7] = (flags_register >> 8) & 0xff;
|
|
|
|
|
|
|
|
|
|
/*float数据段*/
|
|
|
|
|
for (int i = 0; i < 4 * float_length; i++)
|
|
|
|
|
{
|
|
|
|
|
tx_buf[i + 8] = ((uint8_t *)(&tx_data[i / 4]))[i % 4];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*整包校验*/
|
2022-11-29 20:00:46 +08:00
|
|
|
|
crc16 = crc_16(&tx_buf[0], data_len + 6);
|
2022-11-03 20:38:55 +08:00
|
|
|
|
tx_buf[data_len + 6] = crc16 & 0xff;
|
|
|
|
|
tx_buf[data_len + 7] = (crc16 >> 8) & 0xff;
|
|
|
|
|
|
|
|
|
|
*tx_buf_len = data_len + 8;
|
|
|
|
|
}
|
|
|
|
|
/*
|
|
|
|
|
此函数用于处理接收数据,
|
|
|
|
|
返回数据内容的id
|
|
|
|
|
*/
|
2022-11-29 20:00:46 +08:00
|
|
|
|
uint16_t get_protocol_info(uint8_t *rx_buf, // 接收到的原始数据
|
|
|
|
|
uint16_t *flags_register, // 接收数据的16位寄存器地址
|
|
|
|
|
uint8_t *rx_data) // 接收的float数据存储地址
|
2022-11-03 20:38:55 +08:00
|
|
|
|
{
|
2022-11-29 23:11:37 +08:00
|
|
|
|
// 放在静态区,避免反复申请栈上空间
|
2022-11-03 20:38:55 +08:00
|
|
|
|
static protocol_rm_struct pro;
|
|
|
|
|
static uint16_t date_length;
|
2022-11-29 23:11:37 +08:00
|
|
|
|
|
2022-11-03 20:38:55 +08:00
|
|
|
|
if (protocol_heade_Check(&pro, rx_buf))
|
|
|
|
|
{
|
|
|
|
|
date_length = OFFSET_BYTE + pro.header.data_length;
|
2022-11-29 16:33:34 +08:00
|
|
|
|
if (CRC16_Check_Sum(&rx_buf[0], date_length))
|
2022-11-03 20:38:55 +08:00
|
|
|
|
{
|
|
|
|
|
*flags_register = (rx_buf[7] << 8) | rx_buf[6];
|
2023-03-18 14:50:50 +08:00
|
|
|
|
memcpy(rx_data, rx_buf + 8, pro.header.data_length - 2);
|
2022-11-03 20:38:55 +08:00
|
|
|
|
return pro.cmd_id;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-09 20:24:22 +08:00
|
|
|
|
return 0;
|
2022-11-03 20:38:55 +08:00
|
|
|
|
}
|