diff --git a/inc/dlt645.h b/inc/dlt645.h index d628c78..beae797 100644 --- a/inc/dlt645.h +++ b/inc/dlt645.h @@ -3,8 +3,8 @@ #include -#define DL645_MAX_READ_LEN 200 //读数据的最大数据长度 -#define DL645_MAX_WRITE_LEN 50 //写数据的最大数据长度 +#define DLT645_MAX_READ_LEN 200 //读数据的最大数据长度 +#define DLT645_MAX_WRITE_LEN 50 //写数据的最大数据长度 // port setting #define DLT645_DEFAULT_RESPONSE_TIMEOUT 500 //500ms diff --git a/inc/dlt645_1997_private.h b/inc/dlt645_1997_private.h index fe6d148..d02a3ea 100644 --- a/inc/dlt645_1997_private.h +++ b/inc/dlt645_1997_private.h @@ -1,7 +1,7 @@ #ifndef __DLT645_1997_PRIVATE_H #define __DLT645_1997_PRIVATE_H -#define DL645_1997_RD_CMD_LEN 14 //读取数据命令的长度 +#define DLT645_1997_RD_CMD_LEN 14 //读取数据命令的长度 //功能码 #define C_1997_CODE_BRC 0x08 //广播校时 diff --git a/inc/dlt645_2007_private.h b/inc/dlt645_2007_private.h index 9211c13..1c9a590 100644 --- a/inc/dlt645_2007_private.h +++ b/inc/dlt645_2007_private.h @@ -3,7 +3,7 @@ #include "dlt645_private.h" -#define DL645_2007_RD_CMD_LEN (DL645_PREMBLE_LEN + 16) //读取数据命令的长度 +#define DLT645_2007_RD_CMD_LEN (DLT645_PREMBLE_LEN + 16) //读取数据命令的长度 //功能码 #define C_2007_CODE_BRC 0x08 //广播校时 diff --git a/inc/dlt645_private.h b/inc/dlt645_private.h index 805308a..8110dbb 100644 --- a/inc/dlt645_private.h +++ b/inc/dlt645_private.h @@ -9,23 +9,23 @@ #define DLT645_LOG(...) #endif -#define DL645_START_CODE 0x68 -#define DL645_STOP_CODE 0x16 +#define DLT645_START_CODE 0x68 +#define DLT645_STOP_CODE 0x16 -#define DL645_GADDR_CODE 0xAA //万能地址码 -#define DL645_PREMBLE_CODE 0xFE //前导码 +#define DLT645_GADDR_CODE 0xAA //万能地址码 +#define DLT645_PREMBLE_CODE 0xFE //前导码 -#define DL645_PREMBLE_LEN 0 //前导码长度 -#define DL645_ADDR_LEN 6 //设备地址长度 +#define DLT645_PREMBLE_LEN 0 //前导码长度 +#define DLT645_ADDR_LEN 6 //设备地址长度 -#define DL645_START_POS (DL645_PREMBLE_LEN + 0) -#define DL645_ADDR_POS (DL645_PREMBLE_LEN + 1) //设备地址 -#define DL645_CONTROL_POS (DL645_PREMBLE_LEN + 8) //控制码位置 -#define DL645_LEN_POS (DL645_PREMBLE_LEN + 9) //长度位置 -#define DL645_DATA_POS (DL645_PREMBLE_LEN + 10) //数据位置 +#define DLT645_START_POS (DLT645_PREMBLE_LEN + 0) +#define DLT645_ADDR_POS (DLT645_PREMBLE_LEN + 1) //设备地址 +#define DLT645_CONTROL_POS (DLT645_PREMBLE_LEN + 8) //控制码位置 +#define DLT645_LEN_POS (DLT645_PREMBLE_LEN + 9) //长度位置 +#define DLT645_DATA_POS (DLT645_PREMBLE_LEN + 10) //数据位置 -#define DL645_WR_LEN 50 //写入数据命令的长度 -#define DL645_RESP_LEN 60 //读取数据命令的长度 +#define DLT645_WR_LEN 50 //写入数据命令的长度 +#define DLT645_RESP_LEN 60 //读取数据命令的长度 #define C_TD_MASK 0x80 //主从标志位 #define C_TD_POS 7 //主从标志位比特位 diff --git a/readme.md b/readme.md index e8636d8..789b521 100644 --- a/readme.md +++ b/readme.md @@ -453,7 +453,7 @@ MSH_CMD_EXPORT(dlt645_test, dlt645 test); 1. 目前插件不支持自动识别前导码,不会自动拼包或者过滤处理; 2. 如果串口有数据输出和输入但是打印读取失败,考虑是前导码的问题,不同厂商的仪表会有长度不一的前导码。 - 可以用PC工具发送68开头的数据:68 AA AA AA AA AA AA 68 11 04 33 34 34 35 B1 16 - - 返回的报文68开头前的FE个数就是前导码的长度,然后配置DL645_PREMBLE_LEN即可。 + - 返回的报文68开头前的FE个数就是前导码的长度,然后配置DLT645_PREMBLE_LEN即可。 ## 支持 diff --git a/src/dlt645.c b/src/dlt645.c index 588df52..f5d8114 100644 --- a/src/dlt645.c +++ b/src/dlt645.c @@ -55,10 +55,10 @@ int dlt645_receive_msg(dlt645_t *ctx, uint8_t *msg, uint16_t len, uint32_t code, */ int dlt645_send_msg(dlt645_t *ctx, uint8_t *msg, int len) { - msg[DL645_START_POS] = DL645_START_CODE; - msg[DL645_START_POS + DL645_ADDR_LEN + 1] = DL645_START_CODE; - msg[len - 1] = DL645_STOP_CODE; - msg[len - 2] = _crc(msg + DL645_PREMBLE_LEN, len - DL645_PREMBLE_LEN - 2); + msg[DLT645_START_POS] = DLT645_START_CODE; + msg[DLT645_START_POS + DLT645_ADDR_LEN + 1] = DLT645_START_CODE; + msg[len - 1] = DLT645_STOP_CODE; + msg[len - 2] = _crc(msg + DLT645_PREMBLE_LEN, len - DLT645_PREMBLE_LEN - 2); return ctx->write(ctx, msg, len); } @@ -81,7 +81,7 @@ void dlt645_set_addr(dlt645_t *ctx, uint8_t *addr) { addr_temp[5 - i] = addr[i]; } - memcpy(ctx->addr, addr_temp, DL645_ADDR_LEN); + memcpy(ctx->addr, addr_temp, DLT645_ADDR_LEN); } /** diff --git a/src/dlt645_1997.c b/src/dlt645_1997.c index be58bdc..bcbe64f 100644 --- a/src/dlt645_1997.c +++ b/src/dlt645_1997.c @@ -29,7 +29,7 @@ int dlt645_1997_recv_check(uint8_t *msg, int len, uint8_t* addr, uint32_t code) { return -1; } - if (msg[DL645_CONTROL_POS] == 0x84) + if (msg[DLT645_CONTROL_POS] == 0x84) return 0; uint8_t *code_buf = (uint8_t *)&code; @@ -39,7 +39,7 @@ int dlt645_1997_recv_check(uint8_t *msg, int len, uint8_t* addr, uint32_t code) code_buf[i] += 0x33; } - if (*((uint16_t *)(msg + DL645_DATA_POS)) != code) + if (*((uint16_t *)(msg + DLT645_DATA_POS)) != code) return -1; return 0; @@ -110,32 +110,32 @@ int dlt645_1997_read_data(dlt645_t *ctx, uint32_t code, uint8_t *read_data) { - uint8_t send_buf[DL645_1997_RD_CMD_LEN]; - uint8_t read_buf[DL645_RESP_LEN]; + uint8_t send_buf[DLT645_1997_RD_CMD_LEN]; + uint8_t read_buf[DLT645_RESP_LEN]; memset(read_buf, 0, sizeof(read_buf)); memset(send_buf, 0, sizeof(send_buf)); - memcpy(send_buf + 1, ctx->addr, DL645_ADDR_LEN); - send_buf[DL645_CONTROL_POS] = C_1997_CODE_RD; - send_buf[DL645_LEN_POS] = 2; + memcpy(send_buf + 1, ctx->addr, DLT645_ADDR_LEN); + send_buf[DLT645_CONTROL_POS] = C_1997_CODE_RD; + send_buf[DLT645_LEN_POS] = 2; uint8_t send_code[2] = {0}; send_code[0] = (code & 0xff) + 0x33; send_code[1] = ((code >> 8) & 0xff) + 0x33; - memcpy(send_buf + DL645_DATA_POS, send_code, 2); + memcpy(send_buf + DLT645_DATA_POS, send_code, 2); - if (dlt645_send_msg(ctx, send_buf, DL645_1997_RD_CMD_LEN) < 0) + if (dlt645_send_msg(ctx, send_buf, DLT645_1997_RD_CMD_LEN) < 0) { DLT645_LOG("send data error!\n"); return -1; } - if (dlt645_receive_msg(ctx, read_buf, DL645_RESP_LEN, code, DLT645_1997) < 0) + if (dlt645_receive_msg(ctx, read_buf, DLT645_RESP_LEN, code, DLT645_1997) < 0) { DLT645_LOG("receive msg error!\n"); return -1; } - return dlt645_1997_parsing_data(code, read_buf + DL645_DATA_POS + 2, read_buf[DL645_LEN_POS] - 2, read_data); + return dlt645_1997_parsing_data(code, read_buf + DLT645_DATA_POS + 2, read_buf[DLT645_LEN_POS] - 2, read_data); } diff --git a/src/dlt645_2007.c b/src/dlt645_2007.c index ea9ca8b..4bb4d0a 100644 --- a/src/dlt645_2007.c +++ b/src/dlt645_2007.c @@ -29,7 +29,7 @@ int dlt645_2007_recv_check(uint8_t *msg, int len, uint8_t * addr, uint32_t code) { return -1; } - if (msg[DL645_CONTROL_POS] == 0x94) + if (msg[DLT645_CONTROL_POS] == 0x94) return 0; uint8_t *code_buf = (uint8_t *)&code; @@ -39,7 +39,7 @@ int dlt645_2007_recv_check(uint8_t *msg, int len, uint8_t * addr, uint32_t code) code_buf[i] += 0x33; } - if (*((uint32_t *)(msg + DL645_DATA_POS)) != code) + if (*((uint32_t *)(msg + DLT645_DATA_POS)) != code) return -1; return 0; @@ -164,17 +164,17 @@ int dlt645_2007_read_data(dlt645_t *ctx, uint32_t code, uint8_t *read_data) { - uint8_t send_buf[DL645_2007_RD_CMD_LEN]; - uint8_t read_buf[DL645_RESP_LEN]; + uint8_t send_buf[DLT645_2007_RD_CMD_LEN]; + uint8_t read_buf[DLT645_RESP_LEN]; memset(read_buf, 0, sizeof(read_buf)); memset(send_buf, 0, sizeof(send_buf)); - memset(send_buf, DL645_PREMBLE_CODE, DL645_PREMBLE_LEN); - memcpy(send_buf + DL645_ADDR_POS, ctx->addr, DL645_ADDR_LEN); + memset(send_buf, DLT645_PREMBLE_CODE, DLT645_PREMBLE_LEN); + memcpy(send_buf + DLT645_ADDR_POS, ctx->addr, DLT645_ADDR_LEN); - send_buf[DL645_CONTROL_POS] = C_2007_CODE_RD; - send_buf[DL645_LEN_POS] = 4; + send_buf[DLT645_CONTROL_POS] = C_2007_CODE_RD; + send_buf[DLT645_LEN_POS] = 4; uint8_t send_code[4] = {0}; send_code[0] = (code & 0xff) + 0x33; @@ -182,21 +182,21 @@ int dlt645_2007_read_data(dlt645_t *ctx, send_code[2] = ((code >> 16) & 0xff) + 0x33; send_code[3] = ((code >> 24) & 0xff) + 0x33; - memcpy(send_buf + DL645_DATA_POS, send_code, 4); + memcpy(send_buf + DLT645_DATA_POS, send_code, 4); - if (dlt645_send_msg(ctx, send_buf, DL645_2007_RD_CMD_LEN) < 0) + if (dlt645_send_msg(ctx, send_buf, DLT645_2007_RD_CMD_LEN) < 0) { DLT645_LOG("send data error!\n"); return -1; } - if (dlt645_receive_msg(ctx, read_buf, DL645_RESP_LEN, code, DLT645_2007) < 0) + if (dlt645_receive_msg(ctx, read_buf, DLT645_RESP_LEN, code, DLT645_2007) < 0) { DLT645_LOG("receive msg error!\n"); return -1; } - return dlt645_2007_parsing_data(code, read_buf + DL645_DATA_POS + 4, read_buf[DL645_LEN_POS] - 4, read_data); + return dlt645_2007_parsing_data(code, read_buf + DLT645_DATA_POS + 4, read_buf[DLT645_LEN_POS] - 4, read_data); } /** @@ -216,16 +216,16 @@ int dlt645_write_data(dlt645_t *ctx, uint8_t *write_data, uint8_t write_len) { - uint8_t send_buf[DL645_WR_LEN]; - uint8_t read_buf[DL645_RESP_LEN]; + uint8_t send_buf[DLT645_WR_LEN]; + uint8_t read_buf[DLT645_RESP_LEN]; memset(read_buf, 0, sizeof(read_buf)); memset(send_buf, 0, sizeof(send_buf)); - memcpy(send_buf + 1, ctx->addr, DL645_ADDR_LEN); + memcpy(send_buf + 1, ctx->addr, DLT645_ADDR_LEN); - send_buf[DL645_CONTROL_POS] = C_2007_CODE_WR; - send_buf[DL645_LEN_POS] = 12 + write_len; + send_buf[DLT645_CONTROL_POS] = C_2007_CODE_WR; + send_buf[DLT645_LEN_POS] = 12 + write_len; uint8_t send_code[4] = {0}; send_code[0] = (code & 0xff) + 0x33; @@ -238,15 +238,15 @@ int dlt645_write_data(dlt645_t *ctx, write_data[i] += 0x33; } - memcpy(send_buf + DL645_DATA_POS, send_code, 4); - memcpy(send_buf + DL645_DATA_POS + 12, write_data, write_len); + memcpy(send_buf + DLT645_DATA_POS, send_code, 4); + memcpy(send_buf + DLT645_DATA_POS + 12, write_data, write_len); if (dlt645_send_msg(ctx, send_buf, 24 + write_len) < 0) { DLT645_LOG("send data error!\n"); return -1; } - if (dlt645_receive_msg(ctx, read_buf, DL645_RESP_LEN, code, DLT645_2007) < 0) + if (dlt645_receive_msg(ctx, read_buf, DLT645_RESP_LEN, code, DLT645_2007) < 0) { DLT645_LOG("receive msg error!\n"); return -1; diff --git a/src/dlt645_data.c b/src/dlt645_data.c index 50f731b..92442f4 100644 --- a/src/dlt645_data.c +++ b/src/dlt645_data.c @@ -56,39 +56,39 @@ int dlt645_common_check(uint8_t *msg, int len, uint8_t *addr) return -1; } //数据帧标志校验 - if (msg[DL645_START_POS] != DL645_START_CODE || - msg[DL645_START_POS + DL645_ADDR_LEN + 1] != DL645_START_CODE || - msg[len - 1] != DL645_STOP_CODE) + if (msg[DLT645_START_POS] != DLT645_START_CODE || + msg[DLT645_START_POS + DLT645_ADDR_LEN + 1] != DLT645_START_CODE || + msg[len - 1] != DLT645_STOP_CODE) { DLT645_LOG("check code error!\n"); return -1; } //CRC校验 - uint8_t crc = _crc(msg + DL645_PREMBLE_LEN, len - DL645_PREMBLE_LEN - 2); + uint8_t crc = _crc(msg + DLT645_PREMBLE_LEN, len - DLT645_PREMBLE_LEN - 2); if (crc != msg[len - 2]) { DLT645_LOG("check crc error!\n"); return -1; } //控制码主从校验 - if ((msg[DL645_CONTROL_POS] & C_TD_MASK) == (C_TD_MASTER << C_TD_POS)) + if ((msg[DLT645_CONTROL_POS] & C_TD_MASK) == (C_TD_MASTER << C_TD_POS)) { DLT645_LOG("check control direction error!\n"); return -1; } //控制码应答校验 - if ((msg[DL645_CONTROL_POS] & C_ACK_MASK) == (C_ACK_ERR << C_ACK_POS)) + if ((msg[DLT645_CONTROL_POS] & C_ACK_MASK) == (C_ACK_ERR << C_ACK_POS)) { DLT645_LOG("check ACK error!\n"); return msg[len - 3]; } //从站地址校验 - if (memcmp(msg + DL645_ADDR_POS, addr, 6) != 0) + if (memcmp(msg + DLT645_ADDR_POS, addr, 6) != 0) { // 万能地址无需校验 for(int i = 0; i < 6; i++) { - if(addr[i] != DL645_GADDR_CODE) + if(addr[i] != DLT645_GADDR_CODE) { return -1; }