3 Incheckningar 2c2a906703 ... 2ac6220b6e

Upphovsman SHA1 Meddelande Datum
  龙三郎 2ac6220b6e Merge branch 'master' of http://192.168.100.32:3000/wulianwei/NIM_DSP6748 1 månad sedan
  龙三郎 5cd8aaccb7 Merge branch 'master' of http://192.168.100.32:3000/wulianwei/NIM_DSP6748 1 månad sedan
  龙三郎 7e1c233b0d 1 3 månader sedan
6 ändrade filer med 564 tillägg och 61 borttagningar
  1. 361 0
      code/hlx/tr_protocol.c
  2. 69 0
      code/hlx/tr_protocol.h
  3. 18 58
      code/hlx/tr_queue.c
  4. 0 3
      code/hlx/tr_queue.h
  5. 100 0
      code/hlx/tr_tools_util.c
  6. 16 0
      code/hlx/tr_tools_util.h

+ 361 - 0
code/hlx/tr_protocol.c

@@ -0,0 +1,361 @@
+/*
+ * tr_protocol.c
+ *
+ *  Created on: 2025年10月13日
+ *      Author: Administrator
+ */
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include "tr_protocol.h"
+#include "tr_tools_util.h"
+
+
+static void insert(uint8_t* dest, uint16_t dest_start, uint8_t* source, uint16_t source_start, uint16_t len)
+{
+    uint16_t i = 0;
+    while (i < len)
+    {
+        dest[dest_start + i] = source[source_start + i];
+        i++;
+    }
+}
+
+
+// 拼包的方法
+void ProtocolInit(trProtocol* protocol) {
+    protocol->version = 1;
+    protocol->length = 8;
+    protocol->currentOptinNumber = 0;
+    protocol->optionsNum = 0;
+    memset(protocol->message, 0, sizeof(protocol->message));
+    protocol->message[0] = 'T';
+    protocol->message[1] = 'R';
+    protocol->message[2] = protocol->version;
+}
+// 设置目的地址
+void ProtocolSetDestAddress(trProtocol* protocol, uint8_t destAddress) {
+    protocol->destAddress = destAddress;
+    protocol->message[3] = destAddress;
+}
+// 设置源地址
+void ProtocolSetSourceAddress(trProtocol* protocol, uint8_t sourceAddress) {
+    protocol->sourceAddress = sourceAddress;
+    protocol->message[4] = sourceAddress;
+}
+// 设置类型
+void ProtocolSetType(trProtocol* protocol, uint8_t type) {
+    protocol->type = type;
+    protocol->message[5] = type;
+}
+// 设置option
+void ProtocolSetOption(trProtocol* protocol, uint16_t optionNumber, uint16_t optionLength, uint8_t* optionValue) {
+    uint16_t pos_start = protocol->length, opt_length = 1;
+    int delta = optionNumber - protocol->currentOptinNumber;
+
+    if (delta < 0) {
+        return;
+    }
+    else if (delta < 13) {
+        protocol->message[pos_start] |= (delta & 0xFF) << 4;
+    }
+    else if (delta < 255 + 13) {
+        protocol->message[pos_start] |= (0x0D & 0xFF) << 4;
+        uint8_t ext = delta - 0x0D;
+        protocol->message[pos_start + opt_length++] = ext;
+    }
+    else if (delta < 65535 + 255 + 14) {
+        protocol->message[pos_start] |= (0x0E & 0xFF) << 4;
+        uint16_t ext = (delta - 0x0E - 0xFF);
+        protocol->message[pos_start + opt_length++] = (ext >> 8) & 0xFF;
+        protocol->message[pos_start + opt_length++] = ext & 0xFF;
+    }
+    else {
+        return;
+    }
+    // length
+    if (optionLength < 1) {
+        return;
+    }
+    else if (optionLength < 13) {
+        protocol->message[pos_start] |= (optionLength & 0xFF);
+    }
+    else if (optionLength < 255 + 13) {
+        protocol->message[pos_start] |= (0x0D & 0xFF);
+        uint8_t ext = optionLength - 0x0D;
+        protocol->message[pos_start + opt_length++] = ext;
+    }
+    else if (optionLength < 65535 + 255 + 14) {
+        protocol->message[pos_start] |= (0x0E & 0xFF);
+        uint16_t ext = (optionLength - 0x0E - 0xFF);
+        protocol->message[pos_start + opt_length++] = (ext >> 8) & 0xFF;
+        protocol->message[pos_start + opt_length++] = ext & 0xFF;
+    }
+    else {
+        return;
+    }
+
+    insert(protocol->message, pos_start + opt_length, optionValue, 0, optionLength);
+    opt_length += optionLength;
+    protocol->length += opt_length;
+    protocol->currentOptinNumber = optionNumber;
+}
+// 设置字符串类型的option
+void ProtocolSetOptionString(trProtocol* protocol, uint16_t optionNumber, char* optionValue) {
+    uint16_t len = strlen(optionValue);
+    ProtocolSetOption(protocol, optionNumber, len, (uint8_t*)optionValue);
+}
+// 设置short类型的option
+void ProtocolSetOptionShort(trProtocol* protocol, uint16_t optionNumber, uint16_t optionValue) {
+    uint8_t value[2];
+    value[0] = (optionValue >> 8) & 0xFF;
+    value[1] = optionValue & 0xFF;
+    ProtocolSetOption(protocol, optionNumber, sizeof(value), value);
+}
+// 设置payload
+void ProtocolSetPayload(trProtocol* protocol, uint16_t payloadLength, uint8_t* payload) {
+    uint16_t pos = protocol->length;
+    protocol->message[pos] = 0xFF;
+    insert(protocol->message, pos + 1, payload, 0, payloadLength);
+    protocol->length += payloadLength + 1;
+}
+// 设置字符串类型的payload
+void ProtocolSetPayloadString(trProtocol* protocol, const char* payloadString) {
+    uint16_t len = strlen(payloadString);
+    ProtocolSetPayload(protocol, len, (uint8_t*)payloadString);
+}
+// 打包
+void ProtocolPackage(trProtocol* protocol) {
+    protocol->message[6] = protocol->length >> 8 & 0xFF;
+    protocol->message[7] = protocol->length & 0xFF;
+    uint16_t crc = CalCRC16(protocol->message, protocol->length);
+    protocol->message[protocol->length++] = crc >> 8 & 0xFF;
+    protocol->message[protocol->length++] = crc & 0xFF;
+}
+
+
+// 报文解析
+uint8_t ProtocolCheckCRC(uint8_t* message, uint16_t messageLength) {
+    uint16_t crc = CalCRC16(message, messageLength - 2);
+    if ((crc >> 8 & 0xFF) != message[messageLength - 2]) {
+        return 0;
+    }
+    if ((crc & 0xFF) != message[messageLength - 1]) {
+        return 0;
+    }
+    return 1;
+}
+// 获取版本号
+uint8_t ProtocolGetVersion(uint8_t* message, uint16_t messageLength) {
+    if (!ProtocolCheckCRC(message, messageLength)) {
+        return 0;
+    }
+    return message[2];
+}
+// 获取报文类型
+uint8_t ProtocolGetType(uint8_t* message, uint16_t messageLength) {
+    if (!ProtocolCheckCRC(message, messageLength)) {
+        return 0;
+    }
+    return message[5];
+}
+// 获取目的地址
+uint8_t ProtocolGetDestAddress(uint8_t* message, uint16_t messageLength) {
+    if (!ProtocolCheckCRC(message, messageLength)) {
+        return 0;
+    }
+    return message[3];
+}
+// 获取源地址
+uint8_t ProtocolGetSourceAddress(uint8_t* message, uint16_t messageLength) {
+    if (!ProtocolCheckCRC(message, messageLength)) {
+        return 0;
+    }
+    return message[4];
+}
+
+// option结构体
+typedef struct
+{
+    uint8_t valid; // 是否合法
+    uint16_t number; // 编号
+    uint16_t length; // 长度
+    uint16_t pos; // 位置
+    uint8_t delta_ext; // 编号扩展,字节
+    uint8_t length_ext; // 长度扩展,字节
+}
+trOptionStruct;
+
+static trOptionStruct _GetOption(uint8_t* message, uint16_t pos, uint16_t number)
+{
+    trOptionStruct option_struct = {1, number, message[pos] & 0x0F, pos, 0, 0};
+    uint8_t delta = (message[pos] & 0xF0) >> 4;
+    if (delta < 0x0D)
+    {
+        option_struct.delta_ext = 0;
+        option_struct.number += delta;
+    }
+    else if (delta == 0x0D)
+    {
+        option_struct.delta_ext = 1;
+        uint8_t num = message[pos + 1];
+        option_struct.number += (0x0D + num);
+    }
+    else if (delta == 0x0E)
+    {
+        option_struct.delta_ext = 2;
+        uint16_t num = 0;
+        num = (num | message[pos + 1]) << 8;
+        num = num | message[pos + 2];
+        option_struct.number += (0x0E + 0xFF + num);
+    }
+    else
+    {
+        option_struct.valid = 0;
+        return option_struct;
+    }
+
+    if (option_struct.length < 0x0D)
+    {
+        option_struct.length_ext = 0;
+    }
+    else if (option_struct.length == 0x0D)
+    {
+        option_struct.length_ext = 1;
+        uint8_t num = message[pos + option_struct.delta_ext + 1];
+        option_struct.length = (0x0D + num);
+    }
+    else if (option_struct.length == 0x0E)
+    {
+        option_struct.length_ext = 2;
+        uint16_t num = 0;
+        num = (num | message[pos + option_struct.delta_ext + 1]) << 8;
+        num = num | message[pos + option_struct.delta_ext + 2];
+        option_struct.length = (0x0E + 0xFF + num);
+    }
+    else
+    {
+        option_struct.valid = 0;
+        return option_struct;
+    }
+    return option_struct;
+}
+
+// 获取option
+static trOptionStruct getOption(uint8_t* message, uint16_t messageLength, uint16_t number)
+{
+    uint16_t pos = 8;
+    uint16_t current_number = 0;
+    trOptionStruct option_struct = {0};
+
+    while (pos < messageLength)
+    {
+        option_struct = _GetOption(message, pos, current_number);
+        if (option_struct.valid == 0 || option_struct.number == number)
+        {
+            break;
+        }
+        else
+        {
+            current_number = option_struct.number;
+            pos = option_struct.pos + option_struct.delta_ext + option_struct.length_ext + option_struct.length + 1; // 下一个循环的位置起点
+        }
+    }
+    return option_struct;
+}
+
+// 获取option和长度
+uint16_t ProtocolGetOption(uint8_t* message, uint16_t messageLength, uint16_t optionNumber, uint8_t* optionValue) {
+
+    trOptionStruct option_struct = getOption(message, messageLength, optionNumber);
+    if (option_struct.valid)
+    {
+        uint8_t i;
+        for (i = 0; i < option_struct.length; i++)
+        {
+            optionValue[i] = message[option_struct.pos + option_struct.delta_ext + option_struct.length_ext + 1 + i];
+        }
+        return option_struct.length;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+// 获取option
+uint8_t ProtocolGetOptionShort(uint8_t* message, uint16_t messageLength, uint16_t optionNumber, uint16_t * optionValue) {
+    uint8_t value[8];
+    uint16_t optionLength = ProtocolGetOption(message, messageLength, optionNumber, value);
+    if (optionLength != 2) {
+        return 0;
+    }
+    *optionValue = 0;
+    *optionValue |= value[0] << 8;
+    *optionValue |= value[1];
+    return 1;
+}
+
+typedef struct
+{
+    uint8_t valid;
+    uint16_t pos;
+    uint16_t length;
+}
+trPayloadStruct;
+// 获取payload
+static trPayloadStruct Get_Payload(uint8_t* coap_message, uint16_t coap_length)
+{
+    uint16_t pos = 8;
+    uint16_t current_number = 0;
+    trOptionStruct option_struct;
+    trPayloadStruct payload_struct = {1, 0, 0};
+
+    while (pos < coap_length)
+    {
+        if (coap_message[pos] == 0xFF)
+        {
+            payload_struct.valid = 1;
+            payload_struct.pos = pos + 1;
+            payload_struct.length = coap_length - pos - 1;
+            break;
+        }
+        option_struct = _GetOption(coap_message, pos, current_number);
+        if (option_struct.valid == 0)
+        {
+            payload_struct.valid = 0;
+            payload_struct.length = 0;
+            payload_struct.pos = 0;
+            break;
+        }
+        else
+        {
+            current_number = option_struct.number;
+            pos = option_struct.pos + option_struct.delta_ext + option_struct.length_ext + option_struct.length + 1; // 下一个循环的位置起点
+        }
+    }
+    return payload_struct;
+}
+// 获取payload和长度
+uint16_t ProtocolGetPayload(uint8_t* message, uint16_t messageLength, uint8_t* payload) {
+    if (!ProtocolCheckCRC(message, messageLength)) {
+        return 0;
+    }
+    trPayloadStruct payload_struct = Get_Payload(message, messageLength - 2);
+    if (payload_struct.valid)
+    {
+        uint8_t i;
+        for (i = 0; i < payload_struct.length; i++)
+        {
+            payload[i] = message[payload_struct.pos + i];
+        }
+        return payload_struct.length;
+    }
+    else
+    {
+        return 0;
+    }
+}
+
+
+

+ 69 - 0
code/hlx/tr_protocol.h

@@ -0,0 +1,69 @@
+/*
+ * tr_protocol.h
+ *
+ *  Created on: 2025年10月13日
+ *      Author: Administrator
+ */
+
+#ifndef CODE_HLX_TR_PROTOCOL_H_
+#define CODE_HLX_TR_PROTOCOL_H_
+
+#include <stdio.h>
+#include <stdint.h>
+
+typedef struct
+{
+    uint8_t version; // 版本
+    uint8_t destAddress; // 目的地址
+    uint8_t sourceAddress; // 源地址
+    uint8_t type; // 类型
+    uint16_t length; // 长度
+    uint16_t optionsNum; // option个数
+    uint16_t currentOptinNumber; // 当前option的编号
+    uint8_t message[2048]; // 报文
+}
+trProtocol;
+
+// 拼包的方法
+void ProtocolInit(trProtocol* protocol);
+// 设置目的地址
+void ProtocolSetDestAddress(trProtocol* protocol, uint8_t destAddress);
+// 设置源地址
+void ProtocolSetSourceAddress(trProtocol* protocol, uint8_t sourceAddress);
+// 设置类型
+void ProtocolSetType(trProtocol* protocol, uint8_t type);
+// 设置option
+void ProtocolSetOption(trProtocol* protocol, uint16_t optionNumber, uint16_t optionLength, uint8_t* optionValue);
+// 设置字符串类型的option
+void ProtocolSetOptionString(trProtocol* protocol, uint16_t optionNumber, char* optionValue);
+// 设置short类型的option
+void ProtocolSetOptionShort(trProtocol* protocol, uint16_t optionNumber, uint16_t optionValue);
+// 设置payload
+void ProtocolSetPayload(trProtocol* protocol, uint16_t payloadLength, uint8_t* payload);
+// 设置字符串类型的payload
+void ProtocolSetPayloadString(trProtocol* protocol, const char* payloadString);
+// 打包
+void ProtocolPackage(trProtocol* protocol);
+
+
+// 报文解析
+// CRC校验
+uint8_t ProtocolCheckCRC(uint8_t* message, uint16_t messageLength);
+// 获取版本号
+uint8_t ProtocolGetVersion(uint8_t* message, uint16_t messageLength);
+// 获取报文类型
+uint8_t ProtocolGetType(uint8_t* message, uint16_t messageLength);
+// 获取目的地址
+uint8_t ProtocolGetDestAddress(uint8_t* message, uint16_t messageLength);
+// 获取源地址
+uint8_t ProtocolGetSourceAddress(uint8_t* message, uint16_t messageLength);
+// 获取option
+uint16_t ProtocolGetOption(uint8_t* message, uint16_t messageLength, uint16_t optionNumber, uint8_t* optionValue);
+// 获取option
+uint8_t ProtocolGetOptionShort(uint8_t* message, uint16_t messageLength, uint16_t optionNumber, uint16_t* optionValue);
+// 获取payload
+uint16_t ProtocolGetPayload(uint8_t* message, uint16_t messageLength, uint8_t* payload);
+
+
+
+#endif /* CODE_HLX_TR_PROTOCOL_H_ */

+ 18 - 58
code/hlx/tr_queue.c

@@ -6,6 +6,7 @@
  */
 #include <tr_queue.h>
 #include <stdio.h>
+#include "tr_protocol.h"
 
 
 // 初始化队列
@@ -113,30 +114,6 @@ static bool queryQueue(Queue *q, unsigned short start, unsigned short len, unsig
 
 
 
-// 数据转发协议相关
-// 将源数据打包成协议数据
-bool packageSourceData(unsigned char *s, unsigned short sl, unsigned char *d, unsigned short *dl, unsigned char da, unsigned char sa, unsigned char type)
-{
-    unsigned short length = sl + 7;
-    *(d) = 'T';
-    *(d+1) = 'R';
-    *(d+2) = 0x01;
-    *(d+3) = (da & 0x0f) | ((sa << 4) & 0xf0);
-    *(d+4) = (length >> 8) & 0xff;
-    *(d+5) = length & 0xff;
-    *(d+6) = type;
-    memcpy((d+7), s, sl);
-    *dl = length;
-    return true;
-}
-// 将数据加上协议格式,并放入队列。
-static unsigned char buffer[BUFFER_SIZE_2K];
-static unsigned short bufferLength = 0;
-bool enqueueBatchWithProtocol(Queue *q, unsigned char *s, unsigned short sl, unsigned char da, unsigned char sa, unsigned char type)
-{
-    packageSourceData(s, sl, buffer, &bufferLength, da, sa, type);
-    return enqueueBatch(q, buffer, bufferLength);
-}
 // 从队列中取出一个数据包。
 bool dequeueBatchWithProtocol(Queue *q, unsigned char *d, unsigned short *dl)
 {
@@ -157,7 +134,7 @@ bool dequeueBatchWithProtocol(Queue *q, unsigned char *d, unsigned short *dl)
         }
     }
     unsigned char plength[2] = {0};
-    if(!queryQueue(q, 4, 2, plength))
+    if(!queryQueue(q, 6, 2, plength))
     {
         return false;
     }
@@ -174,75 +151,58 @@ bool dequeueBatchWithProtocol(Queue *q, unsigned char *d, unsigned short *dl)
 
 
 // 协议解析相关
-// 获取协议版本
-static bool verifyProtocol(unsigned char *p, unsigned short pl)
-{
-    if(pl < 7)
-    {
-        return false;
-    }
-    unsigned short length = 0;
-    length |= (*(p + 4) << 8) & 0xff00;
-    length |= *(p + 5) & 0x00ff;
-    if(length != pl)
-    {
-        return false;
-    }
-    return true;
-}
 bool getVerFromProtocol(unsigned char *p, unsigned short pl, unsigned char *ver)
 {
-    if(!verifyProtocol(p, pl))
+    uint8_t version = ProtocolGetVersion(p, pl);
+    if(!version)
     {
         return false;
     }
-    *ver = *(p + 2);
+    *ver = version;
     return true;
 }
 
 // 获取目的地址
 bool getDaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *da)
 {
-    if(!verifyProtocol(p, pl))
+    uint8_t destAddress = ProtocolGetDestAddress(p, pl);
+    if(!destAddress)
     {
         return false;
     }
-    *da = *(p + 3) & 0x0f;
+    *da = destAddress;
     return true;
 }
 
 // 获取数据类型
 bool getTypeFromProtocol(unsigned char *p, unsigned short pl, unsigned char *type)
 {
-    if(!verifyProtocol(p, pl))
+    uint8_t _type = ProtocolGetType(p, pl);
+    if(!_type)
     {
         return false;
     }
-    *type = *(p + 6);
+    *type = _type;
     return true;
 }
 
 // 获取来源地址
 bool getSaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *sa)
 {
-    if(!verifyProtocol(p, pl))
+    uint8_t sourceAddress = ProtocolGetSourceAddress(p, pl);
+    if(!sourceAddress)
     {
         return false;
     }
-    *sa = (*(p + 3) >> 4) & 0x0f;
+    *sa = sourceAddress;
     return true;
 }
 
 // 获取源数据
 bool getPayloadFromProtocol(unsigned char *p, unsigned short pl, unsigned char *s, unsigned short *sl)
 {
-    if(!verifyProtocol(p, pl))
-    {
-        return false;
-    }
-    unsigned short length = pl - 7;
-    *sl = length;
-    memcpy(s, (p + 7), length);
+    uint16_t payloadLength = ProtocolGetPayload(p, pl, s);
+    *sl = payloadLength;
     if(*sl == 0)
     {
         return false;
@@ -319,9 +279,9 @@ bool enqueueFromUartBatch(unsigned char *s, unsigned short sl)
     return enqueueBatch(&queueList[QUEUE_INDEX_UART], s, sl);
 }
 
-// 从本模块发出的数据入队列,源数据。带协议格式
+// 从本模块发出的数据入队列,源数据。带协议格式
 bool enqueueFromHere(unsigned char *s, unsigned short sl, unsigned char da, unsigned char type)
 {
-    return enqueueBatchWithProtocol(&queueList[QUEUE_INDEX_HEAR], s, sl, da, THIS_ADDRESS, type);
+    return enqueueBatch(&queueList[QUEUE_INDEX_HEAR], s, sl);
 }
 

+ 0 - 3
code/hlx/tr_queue.h

@@ -61,8 +61,6 @@ bool dequeue(Queue *q, unsigned char *element); // 
 bool dequeueBatch(Queue *q, unsigned short len, unsigned char *element); // 批量出队列
 
 // 协议解析相关
-// 将源数据打包成协议数据
-bool packageSourceData(unsigned char *s, unsigned short sl, unsigned char *d, unsigned short *dl, unsigned char da, unsigned char sa, unsigned char type);
 bool getVerFromProtocol(unsigned char *p, unsigned short pl, unsigned char *ver); // 获取协议版本
 bool getDaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *da); // 获取目的地址
 bool getSaFromProtocol(unsigned char *p, unsigned short pl, unsigned char *sa); // 获取来源地址
@@ -71,7 +69,6 @@ bool getPayloadFromProtocol(unsigned char *p, unsigned short pl, unsigned char *
 
 
 // 数据转发协议相关
-bool enqueueBatchWithProtocol(Queue *q, unsigned char *s, unsigned short sl, unsigned char da, unsigned char sa, unsigned char type); // 将数据加上协议格式,并放入队列。
 bool dequeueBatchWithProtocol(Queue *q, unsigned char *d, unsigned short *dl); // 从队列中取出一个数据包。
 
 // 转发业务相关

+ 100 - 0
code/hlx/tr_tools_util.c

@@ -0,0 +1,100 @@
+/*
+ * tr_tools_util.c
+ *
+ *  Created on: 2025年10月16日
+ *      Author: Administrator
+ */
+#include <stdio.h>
+#include <stdint.h>
+#include "tr_tools_util.h"
+
+/* CRC高位字节值表*/
+const uint8_t auchCRCHi[] = {
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40,
+    0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1,
+    0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41,
+    0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0,
+    0x80, 0x41, 0x00, 0xC1, 0x81, 0x40
+} ;
+
+
+
+/* CRC低位字节值表*/
+const uint8_t auchCRCLo[] = {
+    0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06,
+    0x07, 0xC7, 0x05, 0xC5, 0xC4, 0x04, 0xCC, 0x0C, 0x0D, 0xCD,
+    0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
+    0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A,
+    0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4,
+    0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3,
+    0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3,
+    0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4,
+    0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A,
+    0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29,
+    0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED,
+    0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26,
+    0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60,
+    0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67,
+    0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F,
+    0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68,
+    0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E,
+    0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5,
+    0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71,
+    0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92,
+    0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C,
+    0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B,
+    0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B,
+    0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C,
+    0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42,
+    0x43, 0x83, 0x41, 0x81, 0x80, 0x40
+};
+
+
+
+/******************************************************************************
+*函数名:     CalCRC16                                                          *
+*功能描述:  Modbus的CRC16校验                                                        *
+*输入参数:  无                                                                *
+*输出参数:  无                                                                                                                       *
+*返回值:     校验结果                                                                                      *
+******************************************************************************/
+
+uint16_t CalCRC16(uint8_t *puchMsg, uint16_t usDataLen)
+{
+   uint8_t uchCRCHi = 0xFF ;                      /* 高CRC字节初始化 */
+   uint8_t uchCRCLo = 0xFF ;                      /* 低CRC 字节初始化 */
+   uint8_t uIndex ;                              /* CRC循环中的索引 */
+   while (usDataLen--)                          /* 传输消息缓冲区 */
+   {
+      uIndex = uchCRCHi ^ *puchMsg++ ;            /* 计算CRC */
+      uchCRCHi = uchCRCLo ^ auchCRCHi[uIndex];
+      uchCRCLo = auchCRCLo[uIndex];
+   }
+   //return ((uint16)uchCRCHi << 8 | (uint16)uchCRCLo);
+   return ((uint16_t) uchCRCLo<< 8 | (uint16_t)uchCRCHi);
+}
+
+
+
+

+ 16 - 0
code/hlx/tr_tools_util.h

@@ -0,0 +1,16 @@
+/*
+ * tr_tool_util.h
+ *
+ *  Created on: 2025Äê10ÔÂ16ÈÕ
+ *      Author: Administrator
+ */
+
+#ifndef CODE_HLX_TR_TOOLS_UTIL_H_
+#define CODE_HLX_TR_TOOLS_UTIL_H_
+
+
+uint16_t CalCRC16(uint8_t* puchMsg, uint16_t usDataLen);
+
+
+
+#endif /* CODE_HLX_TR_TOOLS_UTIL_H_ */