├── docs └── _assets │ └── wechat_support.png ├── port ├── dlt645_port.h ├── dlt645_rs485_port.c └── dlt645_port.c ├── inc ├── dlt645_1997.h ├── dlt645_2007.h ├── dlt645.h ├── dlt645_1997_private.h ├── dlt645_private.h └── dlt645_2007_private.h ├── SConscript ├── sample └── dlt645_sample.c ├── src ├── dlt645.c ├── dlt645_1997.c ├── dlt645_2007.c └── dlt645_data.c ├── .github └── workflows │ └── action.yml ├── readme.md └── LICENSE.txt /docs/_assets/wechat_support.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WKJay/DLT645/HEAD/docs/_assets/wechat_support.png -------------------------------------------------------------------------------- /port/dlt645_port.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLT645_PORT_H 2 | #define __DLT645_PORT_H 3 | #include "dlt645.h" 4 | 5 | //对外提供环境声明 6 | extern dlt645_t dlt645; 7 | //645采集硬件层初始化 8 | int dlt645_port_init(void); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /inc/dlt645_1997.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLT645_1997_H 2 | #define __DLT645_1997_H 3 | 4 | #include "dlt645.h" 5 | 6 | //基于 DLT645 2007 协议读取数据 7 | int dlt645_1997_read_data(dlt645_t *ctx, uint32_t code, uint8_t *read_data); 8 | //基于 DLT645 2007 协议校验数据 9 | int dlt645_1997_recv_check(uint8_t *msg, int len, uint8_t *addr, uint32_t code); 10 | 11 | #endif /* __DLT645_1997_H */ 12 | -------------------------------------------------------------------------------- /inc/dlt645_2007.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLT645_2007_H 2 | #define __DLT645_2007_H 3 | 4 | #include "dlt645.h" 5 | 6 | //基于 DLT645 2007 协议读取数据 7 | int dlt645_2007_read_data(dlt645_t *ctx, uint32_t code, uint8_t *read_data); 8 | //基于 DLT645 2007 协议校验数据 9 | int dlt645_2007_recv_check(uint8_t *msg, int len, uint8_t *addr, uint32_t code); 10 | 11 | #endif /* __DLT645_2007_H */ 12 | -------------------------------------------------------------------------------- /SConscript: -------------------------------------------------------------------------------- 1 | Import('RTT_ROOT') 2 | Import('rtconfig') 3 | from building import * 4 | 5 | cwd = GetCurrentDir() 6 | src = Glob("src/*.c") 7 | 8 | # add dlt645 sample 9 | if GetDepend('DLT645_USING_SAMPLE'): 10 | src = src + ['sample/dlt645_sample.c'] 11 | if (GetDepend('DLT645_USING_CUSTOM_PORT') == False): 12 | src = src + ['port/dlt645_port.c'] 13 | 14 | CPPPATH = [cwd + '/inc'] 15 | CPPPATH += [cwd + '/port'] 16 | 17 | group = DefineGroup('dlt645', src, depend = ['PKG_USING_DLT645'], CPPPATH = CPPPATH) 18 | 19 | Return('group') 20 | -------------------------------------------------------------------------------- /inc/dlt645.h: -------------------------------------------------------------------------------- 1 | #ifndef _DLT645_H 2 | #define _DLT645_H 3 | 4 | #include 5 | 6 | #define DLT645_MAX_READ_LEN 200 //读数据的最大数据长度 7 | #define DLT645_MAX_WRITE_LEN 50 //写数据的最大数据长度 8 | 9 | // port setting 10 | #define DLT645_DEFAULT_RESPONSE_TIMEOUT 500 //500ms 11 | #define MAX_DEVICE_NAME_LEN 10 //最大设备名长度 12 | 13 | #define dlt_malloc rt_malloc 14 | #define dlt_free rt_free 15 | 16 | //DLT645 环境结构体 17 | typedef struct dlt645 18 | { 19 | uint8_t addr[6]; //从机地址 20 | uint8_t debug; //调试标志 21 | int (*write)(struct dlt645 *ctx, uint8_t *buf, uint16_t len); //底层写函数 22 | int (*read) (struct dlt645 *ctx, uint8_t *msg, uint16_t len); //底层读函数 23 | void *port_data; //移植层拓展接口 24 | } dlt645_t; 25 | 26 | typedef enum 27 | { 28 | DLT645_2007 = 1, 29 | DLT645_1997 30 | } dlt645_protocal; 31 | 32 | //设置从机地址 33 | extern void dlt645_set_addr(dlt645_t *ctx, uint8_t *addr); 34 | //设置调试开关 35 | extern int dlt645_set_debug(dlt645_t *ctx, int flag); 36 | //数据采集 37 | extern int dlt645_read_data(dlt645_t *ctx, uint32_t code, uint8_t *read_data, dlt645_protocal protocal); 38 | //十进制转BCD码(32位) 39 | extern uint32_t dec_to_bcd(uint32_t val); 40 | //字符串转BCD形式 41 | extern int str_to_bcd(char *str, uint8_t *bcd_store_address, uint16_t bcd_len); 42 | 43 | #endif 44 | -------------------------------------------------------------------------------- /inc/dlt645_1997_private.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLT645_1997_PRIVATE_H 2 | #define __DLT645_1997_PRIVATE_H 3 | 4 | #define DLT645_1997_RD_CMD_LEN 14 //读取数据命令的长度 5 | 6 | //功能码 7 | #define C_1997_CODE_BRC 0x08 //广播校时 8 | #define C_1997_CODE_RD 0x01 //读数据 9 | #define C_1997_CODE_RDM 0x02 //读后续数据 10 | #define C_1997_CODE_WR 0x04 //写数据 11 | #define C_1997_CODE_WRA 0x0A //写设备地址 12 | #define C_1997_CODE_BR 0x0C //更改通信速率 13 | #define C_1997_CODE_PD 0x0F //修改密码 14 | #define C_1997_CODE_XL 0x10 //最大需量清零 15 | #define C_1997_CEODE_RR 0x03 //重读数据 16 | 17 | 18 | #define ERR_1997_RATE 0x40 //费率数超 19 | #define ERR_1997_DAY 0x20 //日时段数超 20 | #define ERR_1997_YEAR 0x10 //年时区数超 21 | #define ERR_1997_BR 0x08 //通信速率不能更改 22 | #define ERR_1997_PD 0x04 //密码错误/未授权 23 | #define ERR_1997_DATA 0x02 //无请求数据 24 | #define ERR_1997_OTHER 0x01 //其他错误 25 | 26 | // DLT645 1997 数据标识 27 | #define DIC_B611 0xB611 //A相电压 28 | #define DIC_B612 0xB612 //B相电压 29 | #define DIC_B613 0xB613 //C相电压 30 | #define DIC_B691 0xB691 //AB线电压 31 | #define DIC_B692 0xB692 //BC线电压 32 | #define DIC_B693 0xB693 //CA线电压 33 | 34 | #define DIC_B621 0xB621 //A相电流 35 | #define DIC_B622 0xB622 //B相电流 36 | #define DIC_B623 0xB623 //C相电流 37 | 38 | #define DIC_B630 0xB630 //总有功功率 39 | #define DIC_B631 0xB631 //A相有功功率 40 | #define DIC_B632 0xB632 //B相有功功率 41 | #define DIC_B633 0xB633 //C相有功功率 42 | 43 | #define DIC_B640 0xB640 //总无功功率 44 | #define DIC_B641 0xB641 //A相无功功率 45 | #define DIC_B642 0xB642 //B相无功功率 46 | #define DIC_B643 0xB643 //C相无功功率 47 | 48 | #define DIC_B660 0xB660 //总视在功率 49 | #define DIC_B661 0xB661 //A相视在功率 50 | #define DIC_B662 0xB662 //B相视在功率 51 | #define DIC_B663 0xB663 //C相视在功率 52 | 53 | #endif /* __DLT645_1997_PRIVATE_H */ 54 | -------------------------------------------------------------------------------- /sample/dlt645_sample.c: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Copyright (c) 2019 3 | All rights reserved. 4 | File name: sample.c 5 | Description: DLT645 软件包使用样例 6 | History: 7 | 1. Version: 8 | Date: 2019-09-23 9 | Author: wangjunjie 10 | Modify: 11 | *************************************************/ 12 | #include "dlt645.h" 13 | #include "rtthread.h" 14 | #include "stdio.h" 15 | #include "dlt645_port.h" 16 | 17 | // dlt645 采集测试标识符 (A相电压) 18 | #define DLT645_2007_READ_TEST_CODE 0x02010100 19 | #define DLT645_1997_READ_TEST_CODE 0xB611 20 | uint8_t test_addr[6] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}; 21 | 22 | /** 23 | * Name: dlt645_read_test 24 | * Brief: dlt645协议采集测试程序 25 | * Input: None 26 | * Output: None 27 | */ 28 | static void dlt645_read_test(void) { 29 | uint8_t read_buf[4]; 30 | rt_memset(read_buf, 0, 4); 31 | 32 | // 设置从机地址 33 | dlt645_set_addr(&dlt645, test_addr); 34 | 35 | // 设置debug模式 36 | dlt645_set_debug(&dlt645, 0); 37 | 38 | // if(dlt645_read_data(&dlt645,DLT645_1997_READ_TEST_CODE,read_buf,DLT645_1997) > 0) //1997采集测试 39 | if (dlt645_read_data(&dlt645, DLT645_2007_READ_TEST_CODE, read_buf, DLT645_2007) > 0) // 2007采集测试 40 | { 41 | printf("读取成功,A相电压值为: %.2f \r\n", *(float *)read_buf); 42 | } else { 43 | rt_kprintf("读取失败\r\n"); 44 | } 45 | } 46 | 47 | /** 48 | * Name: dlt645_entry 49 | * Brief: dlt645协议测试线程 50 | * Input: None 51 | * Output: None 52 | */ 53 | void dlt645_entry(void *param) { 54 | // dlt645 硬件层初始化 55 | dlt645_port_init(); 56 | while (1) { 57 | // 采集测试 58 | dlt645_read_test(); 59 | rt_thread_mdelay(1000); 60 | } 61 | } 62 | 63 | int dlt645_test(void) { 64 | rt_thread_t tid; 65 | tid = rt_thread_create("dlt645", dlt645_entry, RT_NULL, 4096, 8, 20); 66 | if (tid != RT_NULL) rt_thread_startup(tid); 67 | return 0; 68 | } 69 | MSH_CMD_EXPORT(dlt645_test, dlt645 test); 70 | -------------------------------------------------------------------------------- /inc/dlt645_private.h: -------------------------------------------------------------------------------- 1 | #ifndef _DLT645_PRIVATE_H 2 | #define _DLT645_PRIVATE_H 3 | 4 | #include "dlt645.h" 5 | 6 | #ifdef DLT645_DEBUG 7 | #define DLT645_LOG kprintf 8 | #else 9 | #define DLT645_LOG(...) 10 | #endif 11 | 12 | #define DLT645_START_CODE 0x68 13 | #define DLT645_STOP_CODE 0x16 14 | 15 | #define DLT645_GADDR_CODE 0xAA //万能地址码 16 | #define DLT645_PREMBLE_CODE 0xFE //前导码 17 | 18 | #define DLT645_PREMBLE_LEN 0 //前导码长度 19 | #define DLT645_ADDR_LEN 6 //设备地址长度 20 | 21 | #define DLT645_START_POS (DLT645_PREMBLE_LEN + 0) 22 | #define DLT645_ADDR_POS (DLT645_PREMBLE_LEN + 1) //设备地址 23 | #define DLT645_CONTROL_POS (DLT645_PREMBLE_LEN + 8) //控制码位置 24 | #define DLT645_LEN_POS (DLT645_PREMBLE_LEN + 9) //长度位置 25 | #define DLT645_DATA_POS (DLT645_PREMBLE_LEN + 10) //数据位置 26 | 27 | #define DLT645_WR_LEN 50 //写入数据命令的长度 28 | #define DLT645_RESP_LEN 60 //读取数据命令的长度 29 | 30 | #define C_TD_MASK 0x80 //主从标志位 31 | #define C_TD_POS 7 //主从标志位比特位 32 | #define C_TD_MASTER 0 //主站发出的命令帧 33 | #define C_TD_SLAVE 1 //从站发出的应答帧 34 | 35 | #define C_ACK_MASK 0x40 //从站是否正确应答标志位 36 | #define C_ACK_POS 6 //从站应答标志位比特位 37 | #define C_ACK_OK 0 //从站应答正确 38 | #define C_ACK_ERR 1 //从站应答错误 39 | 40 | #define C_FU_MASK 0x20 //是否有后续帧标志位 41 | #define C_FU_POS 5 //后续帧标志位比特位 42 | #define C_FU_NONE 0 //无后续帧 43 | #define C_FU_EXT 1 //有后续帧 44 | 45 | #define C_CODE_MASK 0x1F //功能码标志位 46 | 47 | 48 | 49 | //645 公共校验 50 | extern int dlt645_common_check(uint8_t *msg, int len, uint8_t *addr); 51 | //645 和校验 52 | extern int _crc(uint8_t *msg, int len); 53 | //645 调用底层接口接收数据 54 | extern int dlt645_receive_msg(dlt645_t *ctx, uint8_t *msg, uint16_t len, uint32_t code, dlt645_protocal protocal); 55 | //645 调用底层接口发送 56 | extern int dlt645_send_msg(dlt645_t *ctx, uint8_t *msg, int len); 57 | //将接收到的dlt645数据包中的数据转化为整数 58 | extern int data_package_translate_to_int(uint8_t *read_data, uint16_t len); 59 | //根据数据格式将645协议读取的数据转换为真实数据并存储 60 | extern int dlt645_data_parse_by_format_to_float(uint8_t *read_data, uint16_t read_len, const char *data_format, uint8_t *store_address); 61 | #endif 62 | -------------------------------------------------------------------------------- /src/dlt645.c: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Copyright (c) 2019 3 | All rights reserved. 4 | File name: dlt645.c 5 | Description: 6 | History: 7 | 1. Version: 8 | Date: 2019-09-20 9 | Author: wangjunjie 10 | Modify: 11 | *************************************************/ 12 | #include "dlt645_private.h" 13 | #include "dlt645_1997.h" 14 | #include "dlt645_2007.h" 15 | #include "string.h" 16 | 17 | /** 18 | * Name: dlt645_receive_msg 19 | * Brief: 645协议调用底层接收数据 20 | * Input: 21 | * @ctx: 645环境句柄 22 | * @msg: 数据包存储地址 23 | * @len: 最大接收长度 24 | * @addr: 从站地址 25 | * @code: 数据标识 26 | * @protocal: 645协议类型 27 | * Output: 接收成功:0,接收失败:-1 28 | */ 29 | int dlt645_receive_msg(dlt645_t *ctx, uint8_t *msg, uint16_t len, uint32_t code, dlt645_protocal protocal) 30 | { 31 | int msg_len = ctx->read(ctx, msg, len); 32 | 33 | if (protocal == DLT645_1997) 34 | { 35 | return dlt645_1997_recv_check(msg, msg_len, ctx->addr, code); 36 | } 37 | else if (protocal == DLT645_2007) 38 | { 39 | return dlt645_2007_recv_check(msg, msg_len, ctx->addr, code); 40 | } 41 | else 42 | { 43 | return -1; 44 | } 45 | } 46 | 47 | /** 48 | * Name: dlt645_send_msg 49 | * Brief: 645协议调用底层发送数据 50 | * Input: 51 | * @ctx: 645环境句柄 52 | * @msg: 发送的数据首地址 53 | * @len: 发送长度 54 | * Output: 实际发送的字节数,错误返回-1 55 | */ 56 | int dlt645_send_msg(dlt645_t *ctx, uint8_t *msg, int len) 57 | { 58 | msg[DLT645_START_POS] = DLT645_START_CODE; 59 | msg[DLT645_START_POS + DLT645_ADDR_LEN + 1] = DLT645_START_CODE; 60 | msg[len - 1] = DLT645_STOP_CODE; 61 | msg[len - 2] = _crc(msg + DLT645_PREMBLE_LEN, len - DLT645_PREMBLE_LEN - 2); 62 | 63 | return ctx->write(ctx, msg, len); 64 | } 65 | 66 | /** 67 | * Name: dlt645_set_addr 68 | * Brief: 设置从站地址 69 | * Input: 70 | * @ctx: 645环境句柄 71 | * @addr: 从站地址 72 | * Output: None 73 | */ 74 | void dlt645_set_addr(dlt645_t *ctx, uint8_t *addr) 75 | { 76 | uint8_t addr_temp[6]; 77 | memset(addr_temp, 0, 6); 78 | 79 | //转换字节序 80 | for (int i = 0; i < 6; i++) 81 | { 82 | addr_temp[5 - i] = addr[i]; 83 | } 84 | memcpy(ctx->addr, addr_temp, DLT645_ADDR_LEN); 85 | } 86 | 87 | /** 88 | * Name: dlt645_set_debug 89 | * Brief: 设置调试模式 90 | * Input: 91 | * @ctx: 645环境句柄 92 | * @flag: 调试标志 93 | * Output: None 94 | */ 95 | int dlt645_set_debug(dlt645_t *ctx, int flag) 96 | { 97 | ctx->debug = flag; 98 | return 0; 99 | } 100 | 101 | /** 102 | * Name: dlt645_read_data(用户调用) 103 | * Brief: 根据645协议类型读取数据 104 | * Input: 105 | * @ctx: 645环境句柄 106 | * @addr: 从站地址 107 | * @code: 标识符 108 | * @read_data: 读取数据存储地址 109 | * @protocal: 协议类型 110 | * Output: 成功返回数据长度,失败返回-1 111 | */ 112 | int dlt645_read_data(dlt645_t *ctx, 113 | uint32_t code, 114 | uint8_t *read_data, 115 | dlt645_protocal protocal) 116 | { 117 | int rs = -1; 118 | switch (protocal) 119 | { 120 | case DLT645_1997: 121 | rs = dlt645_1997_read_data(ctx, code, read_data); 122 | break; 123 | case DLT645_2007: 124 | rs = dlt645_2007_read_data(ctx, code, read_data); 125 | break; 126 | default: 127 | DLT645_LOG("unrecognized protocal!\r\n"); 128 | break; 129 | } 130 | return rs; 131 | } 132 | -------------------------------------------------------------------------------- /port/dlt645_rs485_port.c: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Copyright (c) 2023 3 | All rights reserved. 4 | File name: dlt645_rs485_port.c 5 | Description: DLT645用RS485通信的移植&使用例程文件 6 | History: 7 | 1. Version: 8 | Date: 2023-08-11 9 | Author: LiangZohar 10 | Modify: 11 | Env: RT-Thread v5.0.1 12 | Depend on: CONFIG_PKG_USING_RS485=y 13 | Usage: 打开并更新RT-Thread的RS485软件包 14 | *************************************************/ 15 | 16 | #include 17 | #include 18 | #define DBG_TAG "dlt645_485" 19 | #define DBG_LVL DBG_LOG 20 | #include 21 | 22 | #include "dlt645_port.h" 23 | #include "dlt645.h" 24 | #include "rs485.h" 25 | 26 | 27 | #ifndef DLT645_SERIAL_NAME 28 | #define DLT645_SERIAL_NAME "uart5" //serial device name 29 | #endif 30 | 31 | #ifndef DLT645_SERIAL_BAUDRATE 32 | #define DLT645_SERIAL_BAUDRATE 2400 33 | #endif 34 | 35 | #ifndef DLT645_SERIAL_DATA_BITS 36 | #define DLT645_SERIAL_DATA_BITS 8 //0 -- data bits 37 | #endif 38 | 39 | #ifndef DLT645_SERIAL_STOP_BITS 40 | #define DLT645_SERIAL_STOP_BITS 1 //0 -- stop bits 41 | #endif 42 | 43 | #ifndef DLT645_SERIAL_PARITY 44 | #define DLT645_SERIAL_PARITY 2 //0 -- none parity 45 | #endif 46 | 47 | #ifndef DLT645_RS485_DE_PIN 48 | #define DLT645_RS485_DE_PIN 8 //-1 -- nonuse rs485 mode control 49 | #endif 50 | 51 | #ifndef DLT645_RS485_TX_LVL 52 | #define DLT645_RS485_TX_LVL 1 //1 -- rs485 tx level 53 | #endif 54 | 55 | //645结构体注册 56 | dlt645_t dlt645 = {0}; 57 | 58 | /** 59 | * Name: dlt645_hw_read 60 | * Brief: dlt645 硬件层接收数据 61 | * Input: 62 | * @ctx: 645运行环境 63 | * @msg: 接收数据存放地址 64 | * @len: 数据最大接收长度 65 | * Output: 读取数据的长度 66 | */ 67 | int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg ,uint16_t len) 68 | { 69 | int dlen = rs485_recv((rs485_inst_t *)(ctx->port_data), (void *)msg, len); 70 | if(ctx->debug){ 71 | ulog_hexdump("dlt645 recv: ", dlen, msg, dlen); 72 | } 73 | return dlen; 74 | } 75 | 76 | /** 77 | * Name: dlt645_hw_write 78 | * Brief: dlt645 硬件层发送数据 79 | * Input: 80 | * @ctx: 645运行环境 81 | * @buf: 待发送数据 82 | * @len: 发送长度 83 | * Output: 实际发送的字节数,错误返回-1 84 | */ 85 | static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len) 86 | { 87 | if(ctx->debug){ 88 | ulog_hexdump("dlt645 send: ", len, buf, len); 89 | } 90 | return rs485_send((rs485_inst_t *)(ctx->port_data), (void *)buf, len); 91 | } 92 | 93 | /** 94 | * Name: dlt645_port_init 95 | * Brief: 645采集硬件层初始化 96 | * Input: None 97 | * Output: None 98 | */ 99 | int dlt645_port_init(void) 100 | { 101 | rt_memset(&dlt645, 0, sizeof(dlt645_t)); 102 | 103 | dlt645.read = dlt645_hw_read; 104 | dlt645.write = dlt645_hw_write; 105 | dlt645.port_data = (void*)rs485_create( DLT645_SERIAL_NAME, 106 | DLT645_SERIAL_BAUDRATE, 107 | DLT645_SERIAL_PARITY, 108 | DLT645_RS485_DE_PIN, 109 | DLT645_RS485_TX_LVL); 110 | 111 | rs485_set_recv_tmo((rs485_inst_t*)dlt645.port_data, 1000); 112 | if (rs485_connect((rs485_inst_t*)dlt645.port_data) != RT_EOK) 113 | { 114 | rs485_destory((rs485_inst_t*)dlt645.port_data); 115 | LOG_E("rs485 connect fail."); 116 | return RT_ERROR; 117 | } 118 | 119 | LOG_D("dlt645 use rs485 create success."); 120 | 121 | return RT_EOK; 122 | } 123 | 124 | -------------------------------------------------------------------------------- /inc/dlt645_2007_private.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLT645_2007_PRIVATE_H 2 | #define __DLT645_2007_PRIVATE_H 3 | 4 | #include "dlt645_private.h" 5 | 6 | #define DLT645_2007_RD_CMD_LEN (DLT645_PREMBLE_LEN + 16) //读取数据命令的长度 7 | 8 | //功能码 9 | #define C_2007_CODE_BRC 0x08 //广播校时 10 | #define C_2007_CODE_RD 0X11 //读数据 11 | #define C_2007_CODE_RDM 0x12 //读后续数据 12 | #define C_2007_CODE_RDA 0x13 //读通信地址 13 | #define C_2007_CODE_WR 0x14 //写数据 14 | #define C_2007_CODE_WRA 0x15 //写通信地址 15 | #define C_2007_CODE_DJ 0x16 //冻结 16 | #define C_2007_CODE_BR 0x17 //更改通信速率 17 | #define C_2007_CODE_PD 0x18 //修改密码 18 | #define C_2007_CODE_XL 0x19 //最大需量清零 19 | #define C_2007_CODE_DB 0x1a //电表清零 20 | #define C_2007_CODE_MSG 0x1b //事件清零 21 | 22 | 23 | #define ERR_2007_RATE 0x40 //费率数超 24 | #define ERR_2007_DAY 0x20 //日时段数超 25 | #define ERR_2007_YEAR 0x10 //年时区数超 26 | #define ERR_2007_BR 0x08 //通信速率不能更改 27 | #define ERR_2007_PD 0x04 //密码错误/未授权 28 | #define ERR_2007_DATA 0x02 //无请求数据 29 | #define ERR_2007_OTHER 0x01 //其他错误 30 | 31 | /*DLT 645 2007数据标识*/ 32 | #define DIC_0 0x0 //组合有功总电能 33 | #define DIC_100 0x100 //组合有功费率1电能 34 | #define DIC_200 0x200 //组合有功费率2电能 35 | #define DIC_300 0x300 //组合有功费率3电能 36 | #define DIC_400 0x400 //组合有功费率4电能 37 | #define DIC_10000 0x10000 //正向有功总电能 38 | #define DIC_10100 0x10100 //正向有功费率1电能 39 | #define DIC_10200 0x10200 //正向有功费率2电能 40 | #define DIC_10300 0x10300 //正向有功费率3电能 41 | #define DIC_10400 0x10400 //正向有功费率4电能 42 | #define DIC_20000 0x20000 //反向有功总电能 43 | #define DIC_20100 0x20100 //反向有功费率1电能 44 | #define DIC_20200 0x20200 //反向有功费率2电能 45 | #define DIC_20300 0x20300 //反向有功费率3电能 46 | #define DIC_20400 0x20400 //反向有功费率4电能 47 | #define DIC_30000 0x30000 //组合无功1总电能 48 | #define DIC_40000 0x40000 //组合无功2总电能 49 | #define DIC_50000 0x50000 //第一象限无功电能 50 | #define DIC_60000 0x60000 //第二象限无功电能 51 | #define DIC_70000 0x70000 //第三象限无功电能 52 | #define DIC_80000 0x80000 //第四象限无功电能 53 | #define DIC_90000 0x90000 //正向视在总电能 54 | 55 | #define DIC_2010100 0x2010100 //A相电压 56 | #define DIC_2010200 0x2010200 //B相电压 57 | #define DIC_2010300 0x2010300 //C相电压 58 | #define DIC_2020100 0x2020100 //A相电流 59 | #define DIC_2020200 0x2020200 //B相电流 60 | #define DIC_2020300 0x2020300 //C相电流 61 | #define DIC_2030000 0x2030000 //总有功功率 62 | #define DIC_2030100 0x2030100 //A相有功功率 63 | #define DIC_2030200 0x2030200 //B相有功功率 64 | #define DIC_2030300 0x2030300 //C相有功功率 65 | #define DIC_2040000 0x2040000 //总无功功率 66 | #define DIC_2040100 0x2040100 //A相无功功率 67 | #define DIC_2040200 0x2040200 //B相无功功率 68 | #define DIC_2040300 0x2040300 //C相无功功率 69 | #define DIC_2050000 0x2050000 //总视在功率 70 | #define DIC_2050100 0x2050100 //A相视在功率 71 | #define DIC_2050200 0x2050200 //B相视在功率 72 | #define DIC_2050300 0x2050300 //C相视在功率 73 | #define DIC_2060000 0x2060000 //总功率因素 74 | #define DIC_2060100 0x2060100 //A相功率因素 75 | #define DIC_2060200 0x2060200 //B相功率因素 76 | #define DIC_2060300 0x2060300 //C相功率因素 77 | #define DIC_20C0100 0x20C0100 //AB线电压 78 | #define DIC_20C0200 0x20C0200 //BC线电压 79 | #define DIC_20C0300 0x20C0300 //CA线电压 80 | #define DIC_2800002 0x2800002 //频率 81 | #define DIC_4000101 0x4000101 //年月日星期 82 | #define DIC_4000102 0x4000102 //时分秒 83 | #define DIC_5060001 0x5060001 //上一次日冻结时间 84 | #define DIC_5060101 0x5060101 //上一次日冻结正向有功电能 85 | 86 | #define DIC_30C0000 0x30C0000 //过流总次数,总时间 87 | #define DIC_30C0101 0x30C0101 //上一次A相过流记录 88 | 89 | #define DIC_3300100 0x3300100 //电表清零总次数 90 | #define DIC_3300101 0x3300101 //电表清零记录 91 | 92 | #define DIC_4000501 0x4000501 93 | #define DIC_4000502 0x4000502 94 | #define DIC_4000503 0x4000503 95 | #define DIC_4000504 0x4000504 96 | #define DIC_4000505 0x4000505 97 | #define DIC_4000506 0x4000506 98 | #define DIC_4000507 0x4000507 99 | 100 | #define DIC_4000403 0x4000403 //资产管理编码 101 | 102 | #define DIC_4000701 0x4000701 //信号强度 103 | 104 | #define DIC_4000702 0x4000702 //版本号 105 | 106 | #define DIC_7000001 0x7000001 //master_api_key 107 | #define DIC_7000002 0x7000002 //device_id 108 | 109 | #endif /* __DLT645_2007_PRIVATE_H */ 110 | -------------------------------------------------------------------------------- /src/dlt645_1997.c: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Copyright (c) 2019 3 | All rights reserved. 4 | File name: dlt645_1997.c 5 | Description: DLT645 1997版本 6 | History: 7 | 1. Version: 8 | Date: 2019-09-19 9 | Author: wangjunjie 10 | Modify: 11 | *************************************************/ 12 | #include "dlt645_private.h" 13 | #include "dlt645_1997_private.h" 14 | #include "string.h" 15 | 16 | /** 17 | * Name: dlt645_1997_recv_check 18 | * Brief: DLT645-1997 数据校验 19 | * Input: 20 | * @msg: 校验数据包 21 | * @len: 数据包长度 22 | * @addr: 从站地址 23 | * @code: 操作码 24 | * Output: 校验成功:0 ,失败 -1 25 | */ 26 | int dlt645_1997_recv_check(uint8_t *msg, int len, uint8_t* addr, uint32_t code) 27 | { 28 | if (dlt645_common_check(msg, len, addr) < 0) 29 | { 30 | return -1; 31 | } 32 | if (msg[DLT645_CONTROL_POS] == 0x84) 33 | return 0; 34 | 35 | uint8_t *code_buf = (uint8_t *)&code; 36 | 37 | for (uint8_t i = 0; i < 2; i++) 38 | { 39 | code_buf[i] += 0x33; 40 | } 41 | 42 | if (*((uint16_t *)(msg + DLT645_DATA_POS)) != code) 43 | return -1; 44 | 45 | return 0; 46 | } 47 | 48 | /** 49 | * Name: dlt645_1997_parsing_data 50 | * Brief: DLT645-1997 数据包解析 51 | * Input: 52 | * @code: 标识符 53 | * @read_data: 数据包指针 54 | * @len: 数据包长度 55 | * @real_val: 数据存储地址 56 | * Output: 数据包长度 57 | */ 58 | static int dlt645_1997_parsing_data(uint32_t code, uint8_t *read_data, uint16_t len, uint8_t *real_val) 59 | { 60 | switch (code) 61 | { 62 | case DIC_B611: 63 | case DIC_B612: 64 | case DIC_B613: 65 | case DIC_B691: 66 | case DIC_B692: 67 | case DIC_B693: 68 | { 69 | dlt645_data_parse_by_format_to_float(read_data, 2, "XXX", real_val); 70 | break; 71 | } 72 | case DIC_B621: 73 | case DIC_B622: 74 | case DIC_B623: 75 | { 76 | dlt645_data_parse_by_format_to_float(read_data, 2, "XX.XX", real_val); 77 | break; 78 | } 79 | case DIC_B630: 80 | case DIC_B631: 81 | case DIC_B632: 82 | case DIC_B633: 83 | { 84 | dlt645_data_parse_by_format_to_float(read_data, 3, "XX.XXXX", real_val); 85 | break; 86 | } 87 | default: 88 | { 89 | for (uint16_t i = 0; i < len; i++) 90 | { 91 | real_val[i] = ((read_data[i] - 0x33) & 0x0f) + ((read_data[i] - 0x33) >> 4) * 10; 92 | } 93 | break; 94 | } 95 | } 96 | return len; 97 | } 98 | 99 | /** 100 | * Name: dlt645_1997_read_data 101 | * Brief: DLT645-1997 数据读取 102 | * Input: 103 | * @ctx: 645句柄 104 | * @addr: 从站地址 105 | * @code: 数据标识 106 | * @read_data: 数据存储地址 107 | * Output: 成功返回数据长度,失败返回-1 108 | */ 109 | int dlt645_1997_read_data(dlt645_t *ctx, 110 | uint32_t code, 111 | uint8_t *read_data) 112 | { 113 | uint8_t send_buf[DLT645_1997_RD_CMD_LEN]; 114 | uint8_t read_buf[DLT645_RESP_LEN]; 115 | 116 | memset(read_buf, 0, sizeof(read_buf)); 117 | memset(send_buf, 0, sizeof(send_buf)); 118 | 119 | memcpy(send_buf + 1, ctx->addr, DLT645_ADDR_LEN); 120 | send_buf[DLT645_CONTROL_POS] = C_1997_CODE_RD; 121 | send_buf[DLT645_LEN_POS] = 2; 122 | 123 | uint8_t send_code[2] = {0}; 124 | send_code[0] = (code & 0xff) + 0x33; 125 | send_code[1] = ((code >> 8) & 0xff) + 0x33; 126 | memcpy(send_buf + DLT645_DATA_POS, send_code, 2); 127 | 128 | if (dlt645_send_msg(ctx, send_buf, DLT645_1997_RD_CMD_LEN) < 0) 129 | { 130 | DLT645_LOG("send data error!\n"); 131 | return -1; 132 | } 133 | 134 | if (dlt645_receive_msg(ctx, read_buf, DLT645_RESP_LEN, code, DLT645_1997) < 0) 135 | { 136 | DLT645_LOG("receive msg error!\n"); 137 | return -1; 138 | } 139 | 140 | return dlt645_1997_parsing_data(code, read_buf + DLT645_DATA_POS + 2, read_buf[DLT645_LEN_POS] - 2, read_data); 141 | } 142 | -------------------------------------------------------------------------------- /port/dlt645_port.c: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Copyright (c) 2019 3 | All rights reserved. 4 | File name: dlt645_port.c 5 | Description: DLT645 移植&使用例程文件 6 | History: 7 | 1. Version: 8 | Date: 2019-09-19 9 | Author: wangjunjie 10 | Modify: 11 | *************************************************/ 12 | #include "dlt645.h" 13 | #include "rtthread.h" 14 | #include "drv_gpio.h" 15 | 16 | //DLT645采集使用的串口名 17 | #define DLT645_SERIAL_NAME "uart4" 18 | //RS485的收发控制引脚,如果驱动层已有485驱动,不使用填-1 19 | #define DLT645_RS485_DE_PIN -1 //如果用PA15,改为15 20 | 21 | //DL/T 645硬件拓展结构体 22 | typedef struct 23 | { 24 | rt_sem_t dlt645_sem; //用于串口接收的信号量 25 | uint32_t byte_timeout; //字节间的超时时间 26 | } dlt645_port_t; 27 | 28 | static dlt645_port_t dlt645_port = { 29 | .dlt645_sem = RT_NULL, 30 | .byte_timeout = 10, //接收字节间超时时间 31 | }; 32 | //dlt645 采集设备句柄 33 | static rt_device_t dlt645_device = RT_NULL; 34 | //dlt645 采集接收信号量 35 | static struct rt_semaphore dlt645_receive_sem; 36 | //串口配置参数 37 | struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; 38 | //dlt645 环境结构体 39 | dlt645_t dlt645; 40 | 41 | //串口接收数据回调函数 42 | rt_err_t uart_handler(rt_device_t dev, rt_size_t size) 43 | { 44 | //接收到一个数据释放信号量 45 | rt_sem_release(&dlt645_receive_sem); 46 | return RT_EOK; 47 | } 48 | 49 | /** 50 | * Name: dlt645_hw_read 51 | * Brief: dlt645 硬件层接收数据 52 | * Input: 53 | * @ctx: 645运行环境 54 | * @msg: 接收数据存放地址 55 | * @len: 数据最大接收长度 56 | * Output: 读取数据的长度 57 | */ 58 | static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg ,uint16_t len) 59 | { 60 | //实际接收长度 61 | int read_len = 0; 62 | //清缓存变量 63 | uint8_t buf = 0; 64 | 65 | //清空缓存 66 | while(rt_device_read(dlt645_device,0,&buf,1)); 67 | //等待串口接收到数据 68 | if(rt_sem_take(&dlt645_receive_sem, 1000) == -RT_ETIMEOUT) 69 | { 70 | return 0; 71 | } 72 | //每次读取一个字节的数据 73 | while (rt_device_read(dlt645_device, 0, msg + read_len, 1) == 1) 74 | { 75 | if(read_len > len) 76 | { 77 | return 0; 78 | } 79 | else 80 | { 81 | read_len ++; 82 | } 83 | //读取超时标志一帧数据读取完成 84 | if (rt_sem_take(&dlt645_receive_sem, ((dlt645_port_t *)(ctx->port_data))->byte_timeout) == -RT_ETIMEOUT) 85 | { 86 | break; 87 | } 88 | } 89 | return read_len; 90 | } 91 | 92 | /** 93 | * Name: dlt645_hw_write 94 | * Brief: dlt645 硬件层发送数据 95 | * Input: 96 | * @ctx: 645运行环境 97 | * @buf: 待发送数据 98 | * @len: 发送长度 99 | * Output: 实际发送的字节数,错误返回-1 100 | */ 101 | static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len) 102 | { 103 | if(DLT645_RS485_DE_PIN != -1) 104 | { 105 | rt_pin_write(DLT645_RS485_DE_PIN, PIN_HIGH); 106 | } 107 | 108 | //串口发送数据 109 | int ret = rt_device_write(dlt645_device,0,buf,len); 110 | 111 | if(DLT645_RS485_DE_PIN != -1) 112 | { 113 | rt_pin_write(DLT645_RS485_DE_PIN, PIN_LOW); 114 | } 115 | return ret; 116 | } 117 | 118 | 119 | /** 120 | * Name: dlt645_port_init 121 | * Brief: 645采集硬件层初始化 122 | * Input: None 123 | * Output: None 124 | */ 125 | int dlt645_port_init(void) 126 | { 127 | //串口初始化 128 | dlt645_device = rt_device_find(DLT645_SERIAL_NAME); 129 | if (dlt645_device == RT_NULL) 130 | { 131 | rt_kprintf("cannot find device %s \r\n", DLT645_SERIAL_NAME); 132 | return -RT_ERROR; 133 | } 134 | if (rt_device_open(dlt645_device, RT_DEVICE_FLAG_INT_RX) != RT_EOK) 135 | { 136 | rt_kprintf("cannot open device %s \r\n", DLT645_SERIAL_NAME); 137 | return -RT_ERROR; 138 | } 139 | else 140 | { 141 | config.baud_rate = BAUD_RATE_9600; 142 | config.data_bits = DATA_BITS_8; 143 | config.stop_bits = STOP_BITS_1; 144 | config.parity = PARITY_NONE; 145 | /* 打开设备后才可修改串口配置参数 */ 146 | rt_device_control(dlt645_device, RT_DEVICE_CTRL_CONFIG, &config); 147 | rt_kprintf("device %s open success \r\n", DLT645_SERIAL_NAME); 148 | } 149 | 150 | //信号量初始化 151 | if (rt_sem_init(&dlt645_receive_sem, "receive_sem", 0, RT_IPC_FLAG_FIFO) == RT_EOK) 152 | { 153 | dlt645_port.dlt645_sem = &dlt645_receive_sem; 154 | } 155 | else 156 | { 157 | return -RT_ERROR; 158 | } 159 | 160 | //设置串口接收回调函数 161 | rt_device_set_rx_indicate(dlt645_device, uart_handler); 162 | 163 | //485控制引脚初始化 164 | if(DLT645_RS485_DE_PIN != -1) 165 | { 166 | rt_pin_mode(DLT645_RS485_DE_PIN, PIN_MODE_OUTPUT); 167 | } 168 | return RT_EOK; 169 | } 170 | 171 | //645结构体注册 172 | dlt645_t dlt645 = { 173 | {0}, 174 | 0, 175 | dlt645_hw_write, 176 | dlt645_hw_read, 177 | (void *)&dlt645_port}; 178 | -------------------------------------------------------------------------------- /.github/workflows/action.yml: -------------------------------------------------------------------------------- 1 | name: dlt645 2 | # Controls when the action will run. Triggers the workflow on push or pull request 3 | # events but only for the master branch 4 | on: 5 | # Runs at 16:00 UTC (BeiJing 00:00) on the 1st of every month 6 | schedule: 7 | - cron: '0 16 1 * *' 8 | push: 9 | branches: 10 | - master 11 | paths-ignore: 12 | - documentation/** 13 | - '**/README.md' 14 | - '**/README_zh.md' 15 | pull_request: 16 | branches: 17 | - master 18 | paths-ignore: 19 | - documentation/** 20 | - '**/README.md' 21 | - '**/README_zh.md' 22 | 23 | jobs: 24 | build: 25 | runs-on: ubuntu-latest 26 | name: ${{ matrix.legs.RTT_BSP }} 27 | strategy: 28 | fail-fast: false 29 | matrix: 30 | legs: 31 | - RTT_BSP: "gd32" 32 | RTT_TOOL_CHAIN: "sourcery-arm" 33 | SUB_RTT_BSP: 34 | - "gd32/arm/gd32103c-eval" 35 | - "gd32/arm/gd32105c-eval" 36 | - "gd32/arm/gd32105r-start" 37 | - "gd32/arm/gd32107c-eval" 38 | - "gd32/arm/gd32205r-start" 39 | - "gd32/arm/gd32207i-eval" 40 | - "gd32/arm/gd32303e-eval" 41 | - "gd32/arm/gd32305r-start" 42 | - "gd32/arm/gd32307e-start" 43 | - "gd32/arm/gd32407v-start" 44 | - "gd32/arm/gd32450z-eval" 45 | - RTT_BSP: "stm32" 46 | RTT_TOOL_CHAIN: "sourcery-arm" 47 | SUB_RTT_BSP: 48 | - "stm32/stm32f072-st-nucleo" 49 | - "stm32/stm32f091-st-nucleo" 50 | - "stm32/stm32f107-uc-eval" 51 | - "stm32/stm32f207-st-nucleo" 52 | - "stm32/stm32f401-st-nucleo" 53 | - "stm32/stm32f405-smdz-breadfruit" 54 | - "stm32/stm32f407-armfly-v5" 55 | - "stm32/stm32f410-st-nucleo" 56 | - "stm32/stm32f411-atk-nano" 57 | - "stm32/stm32f412-st-nucleo" 58 | - "stm32/stm32f413-st-nucleo" 59 | - "stm32/stm32f427-robomaster-a" 60 | - "stm32/stm32f429-armfly-v6" 61 | - "stm32/stm32f446-st-nucleo" 62 | - "stm32/stm32f469-st-disco" 63 | - "stm32/stm32f746-st-disco" 64 | - "stm32/stm32f767-atk-apollo" 65 | - "stm32/stm32f769-st-disco" 66 | - "stm32/stm32g070-st-nucleo" 67 | - "stm32/stm32g071-st-nucleo" 68 | - "stm32/stm32g431-st-nucleo" 69 | - "stm32/stm32g474-st-nucleo" 70 | - "stm32/stm32h743-armfly-v7" 71 | - "stm32/stm32h747-st-discovery" 72 | - "stm32/stm32h750-artpi" 73 | - "stm32/stm32l4r5-st-nucleo" 74 | - "stm32/stm32l4r9-st-eval" 75 | - "stm32/stm32l010-st-nucleo" 76 | - "stm32/stm32l412-st-nucleo" 77 | - "stm32/stm32l431-BearPi" 78 | - "stm32/stm32l432-st-nucleo" 79 | - "stm32/stm32l433-ali-startkit" 80 | - "stm32/stm32l452-st-nucleo" 81 | - "stm32/stm32l475-atk-pandora" 82 | - "stm32/stm32l476-st-nucleo" 83 | - "stm32/stm32l496-ali-developer" 84 | - "stm32/stm32l552-st-nucleo" 85 | - "stm32/stm32mp157a-st-discovery" 86 | - "stm32/stm32u575-st-nucleo" 87 | - "stm32/stm32wb55-st-nucleo" 88 | steps: 89 | - uses: actions/checkout@v3 90 | with: 91 | repository: RT-Thread/rt-thread 92 | 93 | - uses: actions/checkout@v3 94 | with: 95 | path: mypkgs/dlt645 96 | 97 | - name: Set up Python 98 | uses: actions/setup-python@v3 99 | with: 100 | python-version: 3.8 101 | 102 | - name: Install Tools 103 | shell: bash 104 | run: | 105 | sudo apt-get update 106 | sudo apt-get -qq install gcc-multilib libncurses5-dev scons 107 | sudo python -m pip install --upgrade pip -qq 108 | pip install requests -qq 109 | pip install psutil 110 | git config --global http.postBuffer 524288000 111 | python -c "import tools.menuconfig; tools.menuconfig.touch_env()" 112 | echo "RTT_ROOT=${{ github.workspace }}" >> $GITHUB_ENV 113 | echo "RTT_CC=gcc" >> $GITHUB_ENV 114 | 115 | - name: Install Arm ToolChains 116 | if: ${{ matrix.legs.RTT_TOOL_CHAIN == 'sourcery-arm' && success() }} 117 | shell: bash 118 | run: | 119 | wget -q https://github.com/RT-Thread/toolchains-ci/releases/download/v1.3/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 120 | sudo tar xjf gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2 -C /opt 121 | /opt/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-gcc --version 122 | echo "RTT_EXEC_PATH=/opt/gcc-arm-none-eabi-10-2020-q4-major/bin" >> $GITHUB_ENV 123 | 124 | - name: Bsp Scons Compile 125 | if: ${{ success() }} 126 | shell: bash 127 | env: 128 | RTT_BSP: ${{ matrix.legs.RTT_BSP }} 129 | RTT_TOOL_CHAIN: ${{ matrix.legs.RTT_TOOL_CHAIN }} 130 | SRTT_BSP: ${{ join(matrix.legs.SUB_RTT_BSP, ',') }} 131 | run: | 132 | source ~/.env/env.sh 133 | failed=0 134 | count=0 135 | for bsp in $(echo $SRTT_BSP | tr ',' '\n'); do 136 | count=$((count+1)) 137 | echo "::group::Compiling BSP: ==$count=== $bsp ====" 138 | echo bsp/$bsp 139 | sed -i "s/# CONFIG_PKG_USING_DLT645 is not set/CONFIG_PKG_USING_DLT645=y/g" bsp/$bsp/.config 140 | scons --pyconfig-silent -C bsp/$bsp 141 | sed -i "s/# CONFIG_DLT645_USING_SAMPLE is not set/CONFIG_DLT645_USING_SAMPLE=y/g" bsp/$bsp/.config 142 | scons --pyconfig-silent -C bsp/$bsp 143 | pushd bsp/$bsp && pkgs --update && popd 144 | rm -r bsp/$bsp/packages/dlt645-latest 145 | cp -r mypkgs/dlt645 bsp/$bsp/packages/dlt645 146 | scons -C bsp/$bsp -j$(nproc) --debug=time | tee output.log || \ 147 | { total_time=$(grep "Total command execution time" output.log | awk '{print $5}'); \ 148 | failed=$((failed+1)) ; echo "::endgroup::" ; echo "::error::build $bsp failed" ; \ 149 | echo "- ❌ build $bsp failed in $total_time seconds " >> $GITHUB_STEP_SUMMARY ; } && \ 150 | { total_time=$(grep "Total command execution time" output.log | awk '{print $5}'); \ 151 | echo "- ✅ build $bsp success in $total_time seconds " >> $GITHUB_STEP_SUMMARY ; echo "::endgroup::" ; } 152 | done 153 | exit $failed -------------------------------------------------------------------------------- /src/dlt645_2007.c: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Copyright (c) 2019 3 | All rights reserved. 4 | File name: dlt645_2007.c 5 | Description: DLT645 2007版本 6 | History: 7 | 1. Version: 8 | Date: 2019-09-19 9 | Author: wangjunjie 10 | Modify: 11 | *************************************************/ 12 | #include "dlt645_private.h" 13 | #include "dlt645_2007_private.h" 14 | #include "string.h" 15 | 16 | /** 17 | * Name: dlt645_2007_recv_check 18 | * Brief: DLT645-2007 数据校验 19 | * Input: 20 | * @msg: 校验数据包 21 | * @len: 数据包长度 22 | * @addr: 从站地址 23 | * @code: 操作码 24 | * Output: None 25 | */ 26 | int dlt645_2007_recv_check(uint8_t *msg, int len, uint8_t * addr, uint32_t code) 27 | { 28 | if (dlt645_common_check(msg, len, addr) < 0) 29 | { 30 | return -1; 31 | } 32 | if (msg[DLT645_CONTROL_POS] == 0x94) 33 | return 0; 34 | 35 | uint8_t *code_buf = (uint8_t *)&code; 36 | 37 | for (uint8_t i = 0; i < 4; i++) 38 | { 39 | code_buf[i] += 0x33; 40 | } 41 | 42 | if (*((uint32_t *)(msg + DLT645_DATA_POS)) != code) 43 | return -1; 44 | 45 | return 0; 46 | } 47 | 48 | /** 49 | * Name: dlt645_2007_parsing_data 50 | * Brief: DLT645-2007 数据包解析 51 | * Input: 52 | * @code: 标识符 53 | * @read_data: 数据包指针 54 | * @len: 数据包长度 55 | * @real_val: 数据存储地址 56 | * Output: 数据包长度 57 | */ 58 | int dlt645_2007_parsing_data(uint32_t code, uint8_t *read_data, uint16_t len, uint8_t *real_val) 59 | { 60 | switch (code) 61 | { 62 | case DIC_0: 63 | case DIC_100: 64 | case DIC_200: 65 | case DIC_300: 66 | case DIC_400: 67 | case DIC_10000: 68 | case DIC_10100: 69 | case DIC_10200: 70 | case DIC_10300: 71 | case DIC_10400: 72 | case DIC_20000: 73 | case DIC_20100: 74 | case DIC_20200: 75 | case DIC_20300: 76 | case DIC_20400: 77 | case DIC_30000: 78 | case DIC_40000: 79 | case DIC_50000: 80 | case DIC_60000: 81 | case DIC_70000: 82 | case DIC_80000: 83 | case DIC_90000: 84 | { 85 | dlt645_data_parse_by_format_to_float(read_data,4,"XXXXXX.XX",real_val); 86 | break; 87 | } 88 | case DIC_2010100: 89 | case DIC_2010200: 90 | case DIC_2010300: 91 | case DIC_20C0100: 92 | case DIC_20C0200: 93 | case DIC_20C0300: 94 | { 95 | dlt645_data_parse_by_format_to_float(read_data,2,"XXX.X",real_val); 96 | break; 97 | } 98 | case DIC_2020100: 99 | case DIC_2020200: 100 | case DIC_2020300: 101 | { 102 | dlt645_data_parse_by_format_to_float(read_data,3,"XXX.XXX",real_val); 103 | break; 104 | } 105 | case DIC_2030000: 106 | case DIC_2030100: 107 | case DIC_2030200: 108 | case DIC_2030300: 109 | case DIC_2040000: 110 | case DIC_2040100: 111 | case DIC_2040200: 112 | case DIC_2040300: 113 | case DIC_2050000: 114 | case DIC_2050100: 115 | case DIC_2050200: 116 | case DIC_2050300: 117 | { 118 | dlt645_data_parse_by_format_to_float(read_data,3,"XX.XXXX",real_val); 119 | break; 120 | } 121 | case DIC_2060000: 122 | case DIC_2060100: 123 | case DIC_2060200: 124 | case DIC_2060300: 125 | { 126 | dlt645_data_parse_by_format_to_float(read_data,2,"X.XXX",real_val); 127 | break; 128 | } 129 | case DIC_2800002: 130 | { 131 | dlt645_data_parse_by_format_to_float(read_data,2,"XX.XX",real_val); 132 | break; 133 | } 134 | case DIC_4000403: 135 | case DIC_5060001: 136 | case DIC_7000001: 137 | case DIC_7000002: 138 | for (uint16_t i = 0; i < len; i++) 139 | { 140 | real_val[i] = read_data[i] - 0x33; 141 | } 142 | break; 143 | default: 144 | for (uint16_t i = 0; i < len; i++) 145 | { 146 | real_val[i] = ((read_data[i] - 0x33) & 0x0f) + ((read_data[i] - 0x33) >> 4) * 10; 147 | } 148 | break; 149 | } 150 | return len; 151 | } 152 | 153 | /** 154 | * Name: dlt645_2007_read_data 155 | * Brief: DLT645-2007 数据读取 156 | * Input: 157 | * @ctx: 645句柄 158 | * @addr: 从站地址 159 | * @code: 数据标识 160 | * @read_data: 数据存储地址 161 | * Output: None 162 | */ 163 | int dlt645_2007_read_data(dlt645_t *ctx, 164 | uint32_t code, 165 | uint8_t *read_data) 166 | { 167 | uint8_t send_buf[DLT645_2007_RD_CMD_LEN]; 168 | uint8_t read_buf[DLT645_RESP_LEN]; 169 | 170 | memset(read_buf, 0, sizeof(read_buf)); 171 | memset(send_buf, 0, sizeof(send_buf)); 172 | 173 | memset(send_buf, DLT645_PREMBLE_CODE, DLT645_PREMBLE_LEN); 174 | memcpy(send_buf + DLT645_ADDR_POS, ctx->addr, DLT645_ADDR_LEN); 175 | 176 | send_buf[DLT645_CONTROL_POS] = C_2007_CODE_RD; 177 | send_buf[DLT645_LEN_POS] = 4; 178 | 179 | uint8_t send_code[4] = {0}; 180 | send_code[0] = (code & 0xff) + 0x33; 181 | send_code[1] = ((code >> 8) & 0xff) + 0x33; 182 | send_code[2] = ((code >> 16) & 0xff) + 0x33; 183 | send_code[3] = ((code >> 24) & 0xff) + 0x33; 184 | 185 | memcpy(send_buf + DLT645_DATA_POS, send_code, 4); 186 | 187 | if (dlt645_send_msg(ctx, send_buf, DLT645_2007_RD_CMD_LEN) < 0) 188 | { 189 | DLT645_LOG("send data error!\n"); 190 | return -1; 191 | } 192 | 193 | if (dlt645_receive_msg(ctx, read_buf, DLT645_RESP_LEN, code, DLT645_2007) < 0) 194 | { 195 | DLT645_LOG("receive msg error!\n"); 196 | return -1; 197 | } 198 | 199 | return dlt645_2007_parsing_data(code, read_buf + DLT645_DATA_POS + 4, read_buf[DLT645_LEN_POS] - 4, read_data); 200 | } 201 | 202 | /** 203 | * Name: dlt645_write_data 204 | * Brief: DLT645-2007 数据写入 205 | * Input: 206 | * @ctx: 645句柄 207 | * @addr: 从站地址 208 | * @code: 数据标识 209 | * @write_data: 写入数据的指针 210 | * @write_len: 写入长度 211 | * Output: None 212 | */ 213 | int dlt645_write_data(dlt645_t *ctx, 214 | uint32_t addr, 215 | uint32_t code, 216 | uint8_t *write_data, 217 | uint8_t write_len) 218 | { 219 | uint8_t send_buf[DLT645_WR_LEN]; 220 | uint8_t read_buf[DLT645_RESP_LEN]; 221 | 222 | memset(read_buf, 0, sizeof(read_buf)); 223 | memset(send_buf, 0, sizeof(send_buf)); 224 | 225 | memcpy(send_buf + 1, ctx->addr, DLT645_ADDR_LEN); 226 | 227 | send_buf[DLT645_CONTROL_POS] = C_2007_CODE_WR; 228 | send_buf[DLT645_LEN_POS] = 12 + write_len; 229 | 230 | uint8_t send_code[4] = {0}; 231 | send_code[0] = (code & 0xff) + 0x33; 232 | send_code[1] = ((code >> 8) & 0xff) + 0x33; 233 | send_code[2] = ((code >> 16) & 0xff) + 0x33; 234 | send_code[3] = ((code >> 24) & 0xff) + 0x33; 235 | 236 | for (uint8_t i = 0; i < write_len; i++) 237 | { 238 | write_data[i] += 0x33; 239 | } 240 | 241 | memcpy(send_buf + DLT645_DATA_POS, send_code, 4); 242 | memcpy(send_buf + DLT645_DATA_POS + 12, write_data, write_len); 243 | if (dlt645_send_msg(ctx, send_buf, 24 + write_len) < 0) 244 | { 245 | DLT645_LOG("send data error!\n"); 246 | return -1; 247 | } 248 | 249 | if (dlt645_receive_msg(ctx, read_buf, DLT645_RESP_LEN, code, DLT645_2007) < 0) 250 | { 251 | DLT645_LOG("receive msg error!\n"); 252 | return -1; 253 | } 254 | return 0; 255 | } 256 | -------------------------------------------------------------------------------- /src/dlt645_data.c: -------------------------------------------------------------------------------- 1 | /************************************************* 2 | Copyright (c) 2019 3 | All rights reserved. 4 | File name: dlt645_data.c 5 | Description: DLT645 数据处理源文件 6 | History: 7 | 1. Version: 8 | Date: 2019-09-20 9 | Author: wangjunjie 10 | Modify: 11 | *************************************************/ 12 | #include "dlt645_private.h" 13 | #include 14 | #include 15 | 16 | //字节位置枚举类型 17 | typedef enum 18 | { 19 | BYTE_RESET = 0, //重置 20 | BYTE_LOW_ADDRESS, //低位 21 | BYTE_HIGH_ADDRESS //高位 22 | } byte_part; 23 | 24 | /** 25 | * Name: _crc 26 | * Brief: crc和校验 27 | * Input: 28 | * @msg: 校验数据包 29 | * @len: 数据包长度 30 | * Output: 校验值 31 | */ 32 | int _crc(uint8_t *msg, int len) 33 | { 34 | uint8_t crc = 0; 35 | while (len--) 36 | { 37 | crc += *msg++; 38 | } 39 | return crc; 40 | } 41 | 42 | /** 43 | * Name: dlt645_common_check 44 | * Brief: 645协议接收数据公共部分校验 45 | * Input: 46 | * @msg: 校验数据包 47 | * @len: 数据包长度 48 | * @addr: 从站地址 49 | * Output: 校验成功:0,校验失败:-1 50 | */ 51 | int dlt645_common_check(uint8_t *msg, int len, uint8_t *addr) 52 | { 53 | //数据包长度校验 54 | if (len < 7) 55 | { 56 | return -1; 57 | } 58 | //数据帧标志校验 59 | if (msg[DLT645_START_POS] != DLT645_START_CODE || 60 | msg[DLT645_START_POS + DLT645_ADDR_LEN + 1] != DLT645_START_CODE || 61 | msg[len - 1] != DLT645_STOP_CODE) 62 | { 63 | DLT645_LOG("check code error!\n"); 64 | return -1; 65 | } 66 | //CRC校验 67 | uint8_t crc = _crc(msg + DLT645_PREMBLE_LEN, len - DLT645_PREMBLE_LEN - 2); 68 | if (crc != msg[len - 2]) 69 | { 70 | DLT645_LOG("check crc error!\n"); 71 | return -1; 72 | } 73 | //控制码主从校验 74 | if ((msg[DLT645_CONTROL_POS] & C_TD_MASK) == (C_TD_MASTER << C_TD_POS)) 75 | { 76 | DLT645_LOG("check control direction error!\n"); 77 | return -1; 78 | } 79 | //控制码应答校验 80 | if ((msg[DLT645_CONTROL_POS] & C_ACK_MASK) == (C_ACK_ERR << C_ACK_POS)) 81 | { 82 | DLT645_LOG("check ACK error!\n"); 83 | return msg[len - 3]; 84 | } 85 | //从站地址校验 86 | if (memcmp(msg + DLT645_ADDR_POS, addr, 6) != 0) 87 | { 88 | // 万能地址无需校验 89 | for(int i = 0; i < 6; i++) 90 | { 91 | if(addr[i] != DLT645_GADDR_CODE) 92 | { 93 | return -1; 94 | } 95 | } 96 | } 97 | 98 | return 0; 99 | } 100 | 101 | /** 102 | * Name: dec2bcd 103 | * Brief: 十进制转BCD码(目前支持32位数字大小) 104 | * Input: 105 | * @val: 十进制值 106 | * Output: BCD码值 107 | */ 108 | uint32_t dec_to_bcd(uint32_t val) 109 | { 110 | uint32_t data = 0; 111 | 112 | if (val < 100) 113 | { 114 | uint8_t byte0 = val % 10; 115 | uint8_t byte1 = val / 10; 116 | data = (byte1 << 4) + byte0; 117 | } 118 | else if (val < 10000) 119 | { 120 | uint8_t byte0 = val % 10; 121 | uint8_t byte1 = (val / 10) % 10; 122 | uint8_t byte2 = (val / 100) % 10; 123 | uint8_t byte3 = (val / 1000) % 10; 124 | data = (byte3 << 12) + 125 | (byte2 << 8) + 126 | (byte1 << 4) + byte0; 127 | } 128 | else if (val < 1000000) 129 | { 130 | uint8_t byte0 = val % 10; 131 | uint8_t byte1 = (val / 10) % 10; 132 | uint8_t byte2 = (val / 100) % 10; 133 | uint8_t byte3 = (val / 1000) % 10; 134 | uint8_t byte4 = (val / 10000) % 10; 135 | uint8_t byte5 = (val / 100000) % 10; 136 | data = (byte5 << 20) + 137 | (byte4 << 16) + 138 | (byte3 << 12) + 139 | (byte2 << 8) + 140 | (byte1 << 4) + byte0; 141 | } 142 | else if (val < 100000000) 143 | { 144 | uint8_t byte0 = val % 10; 145 | uint8_t byte1 = (val / 10) % 10; 146 | uint8_t byte2 = (val / 100) % 10; 147 | uint8_t byte3 = (val / 1000) % 10; 148 | uint8_t byte4 = (val / 10000) % 10; 149 | uint8_t byte5 = (val / 100000) % 10; 150 | uint8_t byte6 = (val / 1000000) % 10; 151 | uint8_t byte7 = (val / 10000000) % 10; 152 | data = (byte7 << 28) + 153 | (byte6 << 24) + 154 | (byte5 << 20) + 155 | (byte4 << 16) + 156 | (byte3 << 12) + 157 | (byte2 << 8) + 158 | (byte1 << 4) + byte0; 159 | } 160 | return data; 161 | } 162 | 163 | /** 164 | * Name: str_to_bcd 165 | * Brief: 字符串转BCD形式 166 | * Input: 167 | * @str: 要转换的字符串 168 | * @bcd_store_address: 转换后的存储地址 169 | * @bcd_len: BCD码总长度 170 | * Output: 成功0,失败-1 171 | */ 172 | int str_to_bcd(char *str, uint8_t *bcd_store_address, uint16_t bcd_len) 173 | { 174 | //字符串偏移 175 | int str_pos = bcd_len * 2 - strlen(str); 176 | //字符串比BCD码长度长 177 | if (str_pos < 0) 178 | { 179 | return -1; 180 | } 181 | memset(bcd_store_address, 0, bcd_len); 182 | 183 | for (int i = 0; i < strlen(str); i++) 184 | { 185 | if (str[i] >= '0' && str[i] <= '9') 186 | { 187 | bcd_store_address[(i + str_pos) / 2] |= (str[i] - '0') << (4 * ((i + 1 + (strlen(str) % 2)) % 2)); 188 | } 189 | else 190 | { 191 | //当前字符不为数字,返回错误 192 | return -1; 193 | } 194 | } 195 | return 0; 196 | } 197 | 198 | /** 199 | * Name: data_package_translate_to_int 200 | * Brief: 将接收到的dlt645数据包中的数据转化为整数 201 | * Input: 202 | * @read_data: 数据首地址 203 | * @len: 数据长度 204 | * Output: 转化后的整数 205 | */ 206 | int data_package_translate_to_int(uint8_t *read_data, uint16_t len) 207 | { 208 | //权值 209 | uint8_t number_weight = 0; 210 | //当前数组下标索引 211 | uint8_t current_index = 0; 212 | //当前解析字节位 213 | uint8_t current_byte_part = BYTE_RESET; 214 | //当前整数值 215 | int i_value = 0; 216 | 217 | while (len--) 218 | { 219 | current_byte_part = BYTE_LOW_ADDRESS; 220 | do 221 | { 222 | switch (current_byte_part) 223 | { 224 | //当前处理字节低位 225 | case BYTE_LOW_ADDRESS: 226 | i_value += ((read_data[current_index] - 0x33) & 0x0f) * pow(10, number_weight); 227 | number_weight++; 228 | current_byte_part = BYTE_HIGH_ADDRESS; 229 | break; 230 | //当前处理字节高位 231 | case BYTE_HIGH_ADDRESS: 232 | i_value += ((read_data[current_index] - 0x33) >> 4) * pow(10, number_weight); 233 | number_weight++; 234 | current_byte_part = BYTE_RESET; 235 | break; 236 | } 237 | } while (current_byte_part != BYTE_RESET); 238 | current_index++; 239 | } 240 | return i_value; 241 | } 242 | 243 | /** 244 | * Name: dlt645_data_parse_by_format_to_float 245 | * Brief: 根据数据格式将645协议读取的数据转换为真实数据并存储 246 | * !真实数据为浮点数据,需要注意的是无论读取数据长度是多少,存储数据长度都应是4字节 247 | * Input: 248 | * @read_data: 645协议读取的数据 249 | * @read_len: 读取数据的长度 250 | * @data_format: 转换的数据格式,如 XX.XX,XX.XXX 251 | * Output: 转换成功返回0,失败返回-1 252 | */ 253 | int dlt645_data_parse_by_format_to_float(uint8_t *read_data, uint16_t read_len, const char *data_format, uint8_t *store_address) 254 | { 255 | //权值 256 | int num_weight = 0; 257 | int ival = data_package_translate_to_int(read_data, read_len); 258 | 259 | for (int i = 0; i < strlen(data_format); i++) 260 | { 261 | if (*(data_format + i) == '.') 262 | { 263 | num_weight = strlen(data_format) - i - 1; 264 | if (num_weight < 0) 265 | { 266 | return -1; 267 | } 268 | break; 269 | } 270 | } 271 | float fval = ival / pow(10, num_weight); 272 | memcpy(store_address, &fval, 4); 273 | return 0; 274 | } 275 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # DL/T 645 采集软件包使用说明 2 | 3 | 本软件包用于 DL/T 645 协议的采集与数据处理。在硬件层的移植(主要针对于串口收发数据)完成之后, **用户仅需调用一个API即可完成针对于特定协议(DL/T 1997 或 DL/T 2007)的标识符数据读取、处理与存储。** 使用户无需关注请求数据的封包与接收数据的解包等复杂的协议内部操作,真正做到 **一键采集** 。 4 | 5 | 当然,由于本人精力有限,无法第一时间考虑并编写所有可能的情况与功能,所以在软件包的初期其功能只是根据个人所用到的功能进行编写,无法涵盖所有的需求。也可能会有一些小问题。不过随着时间的推移,本软件包会逐步趋向于完善,也希望使用本软件包的开发人员们能够加入到软件包的完善中来,为该软件包的成长提供一份宝贵的力量! 6 | 7 | ### 目前支持的功能: 8 | 9 | 1. DL/T 645 1997 版数据采集与部分标识符数据解析 10 | 2. DL/T 645 2007 版数据采集与部分标识符数据解析 11 | - 标识符的解析提供了便捷的接口,用户可以调用该接口实现自己所需标识符的解析功能 12 | - 目前还不支持数据写入功能 13 | 14 | 15 | ## 一、软件包使用说明 16 | 17 | 在使用软件包之前, 需要把 **src** 目录下的所有源文件加入工程,将 **inc** 添加到头文件目录,并且参照 **port** 路径下的 **dlt645_port.c** 源文件实现基于用户使用的硬件平台的底层硬件操作的移植。scons现在默认不会将port文件加入到工程中,使用时需在工程的其他目录下手动加入。(详细的移植方法会在下文进行描述) 18 | 19 | 当完成硬件接口移植后,可以参照以下步骤进行对软件包的使用: 20 | 21 | 1. 调用用户实现的硬件接口初始化用于DLT645采集的硬件,并注册645环境结构体。 22 | 2. 调用 `dlt645_set_addr()` 函数设置需要采集的从机设备地址。(函数详细说明请阅读 **API详解** ) 23 | 3. 调用 `dlt645_read_data()` 函数进行具体标识符数据的读取并存储到用户指定的地址下。(函数详细说明请阅读 **API详解** ) 24 | 25 | 26 | ## 二、软件包移植指南 27 | 28 | 软件包提供了完备的基于DL/T 645协议的数据包封包与解包操作,而用户则需要根据自己的平台给软件包提供底层数据的发送与接收接口,让软件包能够接收到数据并且成功发送出去。下面就来详细介绍一下软件包的移植操作。如文字描述有概念模糊或者不理解的地方可以参考本节下方的移植案例。 29 | 30 | ### port文件说明 31 | 1. **dlt645_port.c** 基于UART串口通信,文件里有485控制脚的宏开关用于简易操作485的收发功能; 32 | 2. **dlt645_rs485_port.c** 基于RS485通信,使用RT-Thread的RS485软件包的底层驱动。 33 | 34 | ### 移植步骤 35 | 36 | 1. 初始化用于dlt645协议通信的硬件。(如串口、RS485) 37 | 2. 定义一个dlt645结构体作为dlt645通信的环境结构体。 38 | 3. 实现数据发送与接收函数。(发送和接收单位为一个数据包) 39 | 4. 将数据发送与接收函数注册到dlt645环境结构体中。 40 | 41 | #### 环境结构体: 42 | 43 | 环境结构体是本软件包运行时的内核对象,内核在工作中会调用其中的数据及接口,因此,用户提供的接口也需要注册到该结构体中。其结构如下: 44 | 45 | ```C 46 | 47 | typedef struct dlt645 48 | { 49 | uint8_t addr[6]; //从机地址 50 | uint8_t debug; //调试标志 51 | int (*write)(struct dlt645 *ctx, uint8_t *buf, uint16_t len); //底层写函数 52 | int (*read) (struct dlt645 *ctx, uint8_t *msg, uint16_t len); //底层读函数 53 | void *port_data; //移植层拓展接口 54 | } dlt645_t; 55 | 56 | ``` 57 | 58 | |成员|说明| 59 | |--|-----| 60 | |addr|从机六位地址数组| 61 | |debug|内核调试开关标志| 62 | |write|硬件层写数据接口(用户提供)| 63 | |read|硬件层读数据接口(用户提供)| 64 | |port_data|用户拓展接口| 65 | 66 | 67 | #### 数据发送接口: 68 | 69 | **接口格式:** 70 | 71 | ```C 72 | int (*write)(struct dlt645 *ctx, uint8_t *buf, uint16_t len); 73 | ``` 74 | 75 | **详细介绍:** 76 | 77 | 在用户想要 读/写 从机设备时软件包内核需要调用该接口发送相应的指令帧,用户需要针对于自己的硬件平台来实现该函数。该函数会传入三个参数: 78 | 79 | 1. **ctx:** 为dlt645环境结构体; 80 | 2. **buf:** 为待发送的数据首地址; 81 | 3. **len:** 为要发送的长度。 82 | 83 | #### 数据接收接口: 84 | 85 | **接口格式:** 86 | 87 | ```C 88 | int (*read) (struct dlt645 *ctx, uint8_t *msg, uint16_t len); 89 | ``` 90 | 91 | **详细介绍:** 92 | 93 | 当内核成功发送一个命令后,会立刻调用该接口进行从机设备回应数据的读取,调用时会传入三个参数: 94 | 95 | 1. **ctx:** 为dlt645环境结构体; 96 | 2. **msg:** 为接收到数据的存储地址; 97 | 3. **len:** 为允许接收数据的最大长度。 98 | 99 | 其中 **len** 参数标志着本次接收的最大长度,用户要注意对接收数据长度进行判断,若实际接收长度大于传入的允许接收数据最大长度,用户应该 **视本次数据接收无效,返回0**。 100 | 101 | ### 拓展接口 102 | 103 | 在 dlt645 结构体中提供了一个 **port_data** 成员字段,其类型为 `void *` ,用户可以利用此字段创建针对于自身应用的结构体,从而拓展更多的接口和数据字段,实现更为复杂的硬件操作。 104 | 105 | ### 移植案例 106 | 107 | ```C 108 | 109 | /************************************************* 110 | Copyright (c) 2019 111 | All rights reserved. 112 | File name: dlt645_port.c 113 | Description: DLT645 移植&使用例程文件 114 | History: 115 | 1. Version: 116 | Date: 2019-09-19 117 | Author: wangjunjie 118 | Modify: 119 | *************************************************/ 120 | #include "dlt645.h" 121 | #include "rtthread.h" 122 | #include "drv_gpio.h" 123 | 124 | //DLT645采集使用的串口名 125 | #define DLT645_SERIAL_NAME "uart4" 126 | //RS485的收发控制引脚,如果驱动层已有485驱动,不使用填-1 127 | #define DLT645_RS485_DE_PIN -1 //如果用PA15,改为15 128 | 129 | //DL/T 645硬件拓展结构体 130 | typedef struct 131 | { 132 | rt_sem_t dlt645_sem; //用于串口接收的信号量 133 | uint32_t byte_timeout; //字节间的超时时间 134 | } dlt645_port_t; 135 | 136 | static dlt645_port_t dlt645_port = { 137 | .dlt645_sem = RT_NULL, 138 | .byte_timeout = 10, //接收字节间超时时间 139 | }; 140 | //dlt645 采集设备句柄 141 | static rt_device_t dlt645_device = RT_NULL; 142 | //dlt645 采集接收信号量 143 | static struct rt_semaphore dlt645_receive_sem; 144 | //串口配置参数 145 | struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; 146 | //dlt645 环境结构体 147 | dlt645_t dlt645; 148 | 149 | //串口接收数据回调函数 150 | rt_err_t uart_handler(rt_device_t dev, rt_size_t size) 151 | { 152 | //接收到一个数据释放信号量 153 | rt_sem_release(&dlt645_receive_sem); 154 | return RT_EOK; 155 | } 156 | 157 | /** 158 | * Name: dlt645_hw_read 159 | * Brief: dlt645 硬件层接收数据 160 | * Input: 161 | * @ctx: 645运行环境 162 | * @msg: 接收数据存放地址 163 | * @len: 数据最大接收长度 164 | * Output: 读取数据的长度 165 | */ 166 | static int dlt645_hw_read(dlt645_t *ctx, uint8_t *msg ,uint16_t len) 167 | { 168 | //实际接收长度 169 | int read_len = 0; 170 | //清缓存变量 171 | uint8_t buf = 0; 172 | 173 | //清空缓存 174 | while(rt_device_read(dlt645_device,0,&buf,1)); 175 | //等待串口接收到数据 176 | if(rt_sem_take(&dlt645_receive_sem, 1000) == -RT_ETIMEOUT) 177 | { 178 | return 0; 179 | } 180 | //每次读取一个字节的数据 181 | while (rt_device_read(dlt645_device, 0, msg + read_len, 1) == 1) 182 | { 183 | if(read_len > len) 184 | { 185 | return 0; 186 | } 187 | else 188 | { 189 | read_len ++; 190 | } 191 | //读取超时标志一帧数据读取完成 192 | if (rt_sem_take(&dlt645_receive_sem, ((dlt645_port_t *)(ctx->port_data))->byte_timeout) == -RT_ETIMEOUT) 193 | { 194 | break; 195 | } 196 | } 197 | return read_len; 198 | } 199 | 200 | /** 201 | * Name: dlt645_hw_write 202 | * Brief: dlt645 硬件层发送数据 203 | * Input: 204 | * @ctx: 645运行环境 205 | * @buf: 待发送数据 206 | * @len: 发送长度 207 | * Output: 实际发送的字节数,错误返回-1 208 | */ 209 | static int dlt645_hw_write(dlt645_t *ctx, uint8_t *buf, uint16_t len) 210 | { 211 | if(DLT645_RS485_DE_PIN != -1) 212 | { 213 | rt_pin_write(DLT645_RS485_DE_PIN, PIN_HIGH); 214 | } 215 | 216 | //串口发送数据 217 | int ret = rt_device_write(dlt645_device,0,buf,len); 218 | 219 | if(DLT645_RS485_DE_PIN != -1) 220 | { 221 | rt_pin_write(DLT645_RS485_DE_PIN, PIN_LOW); 222 | } 223 | return ret; 224 | } 225 | 226 | 227 | /** 228 | * Name: dlt645_port_init 229 | * Brief: 645采集硬件层初始化 230 | * Input: None 231 | * Output: None 232 | */ 233 | int dlt645_port_init(void) 234 | { 235 | //串口初始化 236 | dlt645_device = rt_device_find(DLT645_SERIAL_NAME); 237 | if (dlt645_device == RT_NULL) 238 | { 239 | rt_kprintf("cannot find device %s \r\n", DLT645_SERIAL_NAME); 240 | return -RT_ERROR; 241 | } 242 | if (rt_device_open(dlt645_device, RT_DEVICE_FLAG_INT_RX) != RT_EOK) 243 | { 244 | rt_kprintf("cannot open device %s \r\n", DLT645_SERIAL_NAME); 245 | return -RT_ERROR; 246 | } 247 | else 248 | { 249 | config.baud_rate = BAUD_RATE_9600; 250 | config.data_bits = DATA_BITS_8; 251 | config.stop_bits = STOP_BITS_1; 252 | config.parity = PARITY_NONE; 253 | /* 打开设备后才可修改串口配置参数 */ 254 | rt_device_control(dlt645_device, RT_DEVICE_CTRL_CONFIG, &config); 255 | rt_kprintf("device %s open success \r\n", DLT645_SERIAL_NAME); 256 | } 257 | 258 | //信号量初始化 259 | if (rt_sem_init(&dlt645_receive_sem, "receive_sem", 0, RT_IPC_FLAG_FIFO) == RT_EOK) 260 | { 261 | dlt645_port.dlt645_sem = &dlt645_receive_sem; 262 | } 263 | else 264 | { 265 | return -RT_ERROR; 266 | } 267 | 268 | //设置串口接收回调函数 269 | rt_device_set_rx_indicate(dlt645_device, uart_handler); 270 | 271 | //485控制引脚初始化 272 | if(DLT645_RS485_DE_PIN != -1) 273 | { 274 | rt_pin_mode(DLT645_RS485_DE_PIN, PIN_MODE_OUTPUT); 275 | } 276 | return RT_EOK; 277 | } 278 | 279 | //645结构体注册 280 | dlt645_t dlt645 = { 281 | {0}, 282 | 0, 283 | dlt645_hw_write, 284 | dlt645_hw_read, 285 | (void *)&dlt645_port}; 286 | 287 | 288 | ``` 289 | 290 | ## 三、API详解 291 | 292 | ### 1、设置从机地址 293 | 294 | **简介:** 用户在通过645协议读取某设备的数据时,首先要通过该接口设置想要读取的从机的地址。 295 | 296 | **API格式:** 297 | 298 | ```C 299 | void dlt645_set_addr(dlt645_t *ctx, uint8_t *addr); 300 | ``` 301 | 302 | **参数:** 303 | 304 | |参数名|介绍| 305 | |-|--------| 306 | |ctx|645结构句柄| 307 | |addr|6位地址数组| 308 | 309 | **使用示例** 310 | 311 | ```C 312 | 313 | //dlt645 环境结构体 314 | extern dlt645_t dlt645; 315 | //从机地址为111 316 | uint8_t slave_addr[6] = {0x00,0x00,0x00,0x00,0x01,0x11}; 317 | 318 | dlt645_set_addr(&dlt645,slave_addr); 319 | 320 | ``` 321 | 322 | ### 2、读取数据 323 | 324 | **简介:** 用户通过调用该接口读取从机指定标识符的数据 325 | 326 | **API格式:** 327 | 328 | ```C 329 | int dlt645_read_data(dlt645_t *ctx, uint32_t code, uint8_t *read_data, dlt645_protocal protocal); 330 | ``` 331 | 332 | **参数:** 333 | 334 | |参数名|描述| 335 | |-|--------| 336 | |ctx|645结构句柄| 337 | |code|标识符| 338 | |read_data|读取数据的存储地址(注意地址长度为4字节)| 339 | |protocal|指定协议类型(可选 **DLT645_2007** 或 **DLT645_1997** )| 340 | 341 | |返回值|描述| 342 | |-|--------| 343 | |整数|读取数据长度| 344 | |-1|读取失败| 345 | 346 | - **read_data** 参数为读取数据的存储地址,目前的版本中将所有的数据都转换为浮点数保存,(尚不支持超过4字节大小的数据,若有需要可以修改内核)因此 **read_data** 大小必须为4字节。后续版本会进行修改,从而支持任意大小。 347 | 348 | - **protocal** 参数指定了读取的协议类型,可选值为:`DLT645_1997` 和 `DLT645_2007` ,分别对应1997版与2007版。 349 | 350 | **使用示例** 351 | 352 | ```C 353 | 354 | //dlt645 环境结构体 355 | extern dlt645_t dlt645; 356 | //dlt645 采集测试标识符 (A相电压) 357 | #define DLT645_2007_READ_TEST_CODE 0x02010100 358 | //用于存放读取数据的数组 359 | uint8_t read_buf[4]; 360 | 361 | //读取数据 362 | if (dlt645_read_data(&dlt645,DLT645_2007_READ_TEST_CODE,read_buf,DLT645_2007) > 0) 363 | { 364 | printf("读取成功,A相电压值为: %.2f \r\n",*(float *)read_buf); 365 | } 366 | else 367 | { 368 | rt_kprintf("读取失败\r\n"); 369 | } 370 | 371 | ``` 372 | 373 | ## 四、使用案例 374 | 375 | ```C 376 | 377 | /************************************************* 378 | Copyright (c) 2019 379 | All rights reserved. 380 | File name: sample.c 381 | Description: DLT645 软件包使用样例 382 | History: 383 | 1. Version: 384 | Date: 2019-09-23 385 | Author: wangjunjie 386 | Modify: 387 | *************************************************/ 388 | #include "dlt645.h" 389 | #include "rtthread.h" 390 | #include "stdio.h" 391 | #include "dlt645_port.h" 392 | 393 | // dlt645 采集测试标识符 (A相电压) 394 | #define DLT645_2007_READ_TEST_CODE 0x02010100 395 | #define DLT645_1997_READ_TEST_CODE 0xB611 396 | uint8_t test_addr[6] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA}; 397 | 398 | /** 399 | * Name: dlt645_read_test 400 | * Brief: dlt645协议采集测试程序 401 | * Input: None 402 | * Output: None 403 | */ 404 | static void dlt645_read_test(void) { 405 | uint8_t read_buf[4]; 406 | rt_memset(read_buf, 0, 4); 407 | 408 | // 设置从机地址 409 | dlt645_set_addr(&dlt645, test_addr); 410 | 411 | // 设置debug模式 412 | dlt645_set_debug(&dlt645, 0); 413 | 414 | // if(dlt645_read_data(&dlt645,DLT645_1997_READ_TEST_CODE,read_buf,DLT645_1997) > 0) //1997采集测试 415 | if (dlt645_read_data(&dlt645, DLT645_2007_READ_TEST_CODE, read_buf, DLT645_2007) > 0) // 2007采集测试 416 | { 417 | printf("读取成功,A相电压值为: %.2f \r\n", *(float *)read_buf); 418 | } else { 419 | rt_kprintf("读取失败\r\n"); 420 | } 421 | } 422 | 423 | /** 424 | * Name: dlt645_entry 425 | * Brief: dlt645协议测试线程 426 | * Input: None 427 | * Output: None 428 | */ 429 | void dlt645_entry(void *param) { 430 | // dlt645 硬件层初始化 431 | dlt645_port_init(); 432 | while (1) { 433 | // 采集测试 434 | dlt645_read_test(); 435 | rt_thread_mdelay(1000); 436 | } 437 | } 438 | 439 | int dlt645_test(void) { 440 | rt_thread_t tid; 441 | tid = rt_thread_create("dlt645", dlt645_entry, RT_NULL, 4096, 8, 20); 442 | if (tid != RT_NULL) rt_thread_startup(tid); 443 | return 0; 444 | } 445 | MSH_CMD_EXPORT(dlt645_test, dlt645 test); 446 | 447 | 448 | ``` 449 | 终端输入 `dlt645_test` 命令打开采集功能. 450 | 451 | ## 五、注意事项 452 | 453 | 1. 目前插件不支持自动识别前导码,不会自动拼包或者过滤处理; 454 | 2. 如果串口有数据输出和输入但是打印读取失败,考虑是前导码的问题,不同厂商的仪表会有长度不一的前导码。 455 | - 可以用PC工具发送68开头的数据:68 AA AA AA AA AA AA 68 11 04 33 34 34 35 B1 16 456 | - 返回的报文68开头前的FE个数就是前导码的长度,然后配置DLT645_PREMBLE_LEN即可。 457 | 458 | ## 支持 459 | 460 | ![支持](./docs/_assets/wechat_support.png) 461 | 462 | 如果这个软件包解决了你的问题,不妨扫描上面二维码请我喝杯咖啡吧 -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | (This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.) 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | {description} 474 | Copyright (C) {year} {fullname} 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | {signature of Ty Coon}, 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | --------------------------------------------------------------------------------