From de6db49458199f1d22875244dfcfb4e8aaa16155 Mon Sep 17 00:00:00 2001 From: LJ Date: Thu, 16 May 2024 19:07:30 +0800 Subject: [PATCH] =?UTF-8?q?CAMS=E5=8D=8F=E8=AE=AE=E9=80=82=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 +- applications/ADC/adc.c | 8 +- applications/Modbus/DATU.c | 256 ++++++++++++++++++++++++++++++++++ applications/Modbus/DATU.h | 31 ++++ applications/OLED/gui.c | 38 ++--- applications/OLED/gui.h | 2 +- applications/config/console.c | 8 +- applications/config/console.h | 1 + applications/thread/thread.c | 3 +- applications/thread/thread.h | 3 +- 10 files changed, 329 insertions(+), 28 deletions(-) create mode 100644 applications/Modbus/DATU.c create mode 100644 applications/Modbus/DATU.h diff --git a/README.md b/README.md index b5002f6..1edacdc 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ ## CAMS DATU :CAMS计算机辅助缝纫管理系统采集模块 + #### 说明 - 待完成 RS232协议(未指定) - 发送数据信息(未指定格式) - 已完成 @@ -25,6 +25,8 @@ 8.4G联网、MQTT连接 9.命令输出指定log用于调试 + + 10 .适配协议CAMS协议,解析与发送(指令接收后具体事项还未确定) #### 开发环境 @@ -52,7 +54,7 @@ RT-Thread 软件打开 [点击下载](https://www.rt-thread.org/download.html#do | fram | 读写flash,用以保存读取配置 | | IO | 端口电平等 | | OLED | 屏幕 | -| modbus | modbus 功能 | +| modbus | modbus 功能,CAMS协议适配 | | thread | 线程管理 | | LTE | 网络配置 | | ADC | ADC读值转换0-10V、3.3V、4-20ma等 | @@ -77,6 +79,7 @@ RT-Thread 软件打开 [点击下载](https://www.rt-thread.org/download.html#do | log_lte | 4G与mqtt的log | | log_moddbus | modbus的log | | log_adc | adc的log | +| log_datu | datu协议栈收发的log | | no_log | 关闭log | | IO_key < 0-8 > < 1 0 >| 指定输出端口改变电平 | diff --git a/applications/ADC/adc.c b/applications/ADC/adc.c index fd9cf6b..a6b31d3 100644 --- a/applications/ADC/adc.c +++ b/applications/ADC/adc.c @@ -63,7 +63,9 @@ void adc_read() * 外部电压 = (vout - 0.2)* 6.8 / 2 * * 4-20ma vin = i * 150 - * 外部电流 = (vout - 0.2)* 6.8 / 2 / 150 + * 外部电流 = (vout)* 6.8 / 2 / 150 + * + * */ ADC.ADC1_0_value = rt_adc_read(adc1_dev, ADC1_CHANNEL_0); //读取采样值 @@ -77,7 +79,7 @@ void adc_read() } else if (ADC.ADC1_mode == 2) { - ADC.ADC1_0_Current = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 - 0.2) / 150 * 340); + ADC.ADC1_0_Current = (rt_uint16_t) (((float) ADC.ADC1_0_vol / 100 ) / 150 * 340); } if (ADC.log) @@ -96,7 +98,7 @@ void adc_read() } else if (ADC.ADC2_mode == 2) { - ADC.ADC2_4_Current = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 - 0.2) / 150 * 340); + ADC.ADC2_4_Current = (rt_uint16_t) (((float) ADC.ADC2_4_vol / 100 ) / 150 * 340); } if (ADC.log) diff --git a/applications/Modbus/DATU.c b/applications/Modbus/DATU.c new file mode 100644 index 0000000..9fed14f --- /dev/null +++ b/applications/Modbus/DATU.c @@ -0,0 +1,256 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-05-16 lijian the first version + */ +#include +#define POLY 0x1021 // CRC-16-CCITT多项式 + +DATU_t DATU; + +rt_uint16_t CRC16_Calculate(rt_uint8_t *data, rt_uint8_t len) +{ + rt_uint16_t CRC16in = 0xFFFF; + rt_uint16_t CPoly = 0xA001; /*0x8005高低位交换*/ + + while (len--) + { + CRC16in ^= *(data++); + for(rt_uint8_t i = 0;i < 8;i++) + { + if(CRC16in & 0x01) + CRC16in = (CRC16in >> 1) ^ CPoly; + else + CRC16in = CRC16in >> 1; + } + } + + CRC16in = CRC16in % 256 * 256 + CRC16in / 256; /*调换高低Byte*/ + + return (CRC16in); +} + + +rt_uint8_t read_cmd(rt_uint8_t read_buf[], rt_uint8_t read_len) +{ + + uint8_t correct_header[] = { 0x44, 0x41, 0x54, 0x55 }; + + if (memcmp(read_buf, correct_header, sizeof(correct_header)) == 0){ + + if (read_buf[4] == 0x03) { + return 3; + } + else if (read_buf[4] == 0x06){ + return 6; + } + else{ + return -2; + } + } + else{ + return -1; + + } + +} + + + +void datu_init() +{ + rs485_inst_t *hinst = rs485_create(RS485_SAMPLE_MASTER_SERIAL, RS485_SAMPLE_MASTER_BAUDRATE, + RS485_SAMPLE_MASTER_PARITY, RS485_SAMPLE_MASTER_PIN, RS485_SAMPLE_MASTER_LVL); + + if (hinst == RT_NULL) + { + rt_kprintf("create rs485 instance fail.\n"); + return; + } + + rs485_set_recv_tmo(hinst, 1000); + if (rs485_connect(hinst) != RT_EOK) + { + rs485_destory(hinst); + rt_kprintf("rs485 connect fail.\n"); + return; + } + + // rt_kprintf("Running."); + rt_int8_t send_len; +// rt_uint8_t send_buf[128]; + rt_uint8_t send_buf[16] = { 'D', 'A', 'T', 'U', 0x01, 0x02, 0x03, 0x04, 0x05 }; + rt_uint8_t read_buf[10]; + rt_int8_t read_len; + rt_uint8_t i; + rt_int8_t rc; + while (1) + { + + /** 数据识别序列 数据内容 校验字 + 44 41 54 55 00 00 00 00 00 00 00 00 00 00 CRC + B0,B1,B2,B3 B4--B7 B8,B9 B10,B11 B12,B13 B14,B15 + D, A, T, U 主轴针数 面线1张力值 面线2张力值 传感器状态 B4--B13的16位CRC + * + */ + rt_thread_mdelay(100); + + // || 有针数 + if (DATU.send_data ) { + rt_uint16_t crc = CRC16_Calculate(send_buf + 4, 10); +// rt_kprintf("CRC %x\n ", crc); + send_buf[14] = crc >> 8; + send_buf[15] = crc & 0xFF; + send_len = rs485_send(hinst, (void *) send_buf, sizeof(send_buf)); + DATU.send_data =0; + } + + if (DATU.log) + { + rt_kprintf("rs485 send %d datas : ", send_len); + for (i = 0; i < send_len; i++) + { + rt_kprintf("%02X ", send_buf[i]); + } + + rt_kprintf("\n"); + send_len = 0; + + } + +// read_len = rs485_send_then_recv(hinst, (void *) send_buf, sizeof(send_buf), read_buf, sizeof(read_buf)); + + read_len = rs485_recv(hinst, (void *) read_buf, sizeof(read_buf)); + + if (read_len < 0) + { + if (DATU.log) + { + rt_kprintf("rs485 read datas error.\n"); + } + continue; + } + else if (read_len == 0) + { + if (DATU.log) + { + rt_kprintf("rs485 read timeout.\n"); + } + continue; + } + + //read_buf[read_len] = 0; + + + + if (DATU.log) + { + rt_kprintf("rs485 read %d datas : ", read_len); + for (i = 0; i < read_len; i++) + { + rt_kprintf("%2X ", read_buf[i]); + } + + rt_kprintf("\n"); + + } + + + //CRC校验 + rt_uint16_t received_crc = (read_buf[7] << 8) | read_buf[8]; + rt_kprintf("CRC %x\n ", received_crc); + rt_uint16_t calculated_crc = CRC16_Calculate(read_buf+4, 3); + rt_kprintf("CRC %x\n ", calculated_crc); + if (calculated_crc == received_crc) { + if (DATU.log) + { + rt_kprintf("read datas CRC OK\n"); + } + } else { + if (DATU.log) + { + rt_kprintf("read datas CRC ERR\n"); + } + continue; + } + + rc = read_cmd(read_buf, read_len); + + if (rc == -1) + { + if (DATU.log) + { + rt_kprintf("read header is not DATU.\n"); + } + continue; + } + else if (rc == -2) + { + if (DATU.log) + { + rt_kprintf("read cmd is not 0x03 / 0x06.\n"); + } + continue; + } + else if (rc == 3) + { + if (DATU.log) + { + rt_kprintf("read cmd is 0x03.\n"); + } + DATU.send_data =1; + continue; + } + else if (rc == 6) + { + if (DATU.log) + { + rt_kprintf("read cmd is 0x06.\n"); + rt_kprintf("read cmd is %d .\n",read_buf[6]); + } + + switch (read_buf[6]) { + case 1: + //主轴针数清零 + break; + case 2: + //闪灯 +// IO_key(1, argv); + break; + case 3: + //开锁1 + break; + case 4: + //开锁2 + break; + case 5: + //开锁3 + break; + case 6: + //报警灯开 + break; + case 7: + //报警灯关 + break; + default: + break; + } + + + } + + } + +} + + + +void set_DATU_log(rt_uint8_t log) +{ + DATU.log = log; +} + diff --git a/applications/Modbus/DATU.h b/applications/Modbus/DATU.h new file mode 100644 index 0000000..70d7254 --- /dev/null +++ b/applications/Modbus/DATU.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2024-05-16 lijian the first version + */ +#ifndef APPLICATIONS_MODBUS_DATU_H_ +#define APPLICATIONS_MODBUS_DATU_H_ + +#include +#include +#include +#include +#include +#include +#include + + +typedef struct +{ + rt_uint8_t send_data;//发送数据 + rt_uint8_t log; //log +} DATU_t; + +void datu_init(); +void set_DATU_log(rt_uint8_t log); + +#endif /* APPLICATIONS_MODBUS_DATU_H_ */ diff --git a/applications/OLED/gui.c b/applications/OLED/gui.c index fe1867b..cf3c865 100644 --- a/applications/OLED/gui.c +++ b/applications/OLED/gui.c @@ -252,23 +252,23 @@ void Menu3_485() { // char buf[4]; ssd1306_FillArea(0, 18, 128, 46, Black); - ssd1306_SetCursor(2, 20); - ssd1306_WriteString("addr:", Font_7x10, White); - ssd1306_SetCursor(55, 20); - rt_sprintf(buf, "0x%2d", get_Modbus_Addr()); - ssd1306_WriteString(buf, Font_6x8, White); - - ssd1306_SetCursor(2, 32); - ssd1306_WriteString("reg:", Font_7x10, White); - ssd1306_SetCursor(55, 32); - rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Addr()); - ssd1306_WriteString(buf, Font_6x8, White); - - ssd1306_SetCursor(2, 44); - ssd1306_WriteString("r_num:", Font_7x10, White); - ssd1306_SetCursor(55, 44); - rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Num()); - ssd1306_WriteString(buf, Font_6x8, White); +// ssd1306_SetCursor(2, 20); +// ssd1306_WriteString("addr:", Font_7x10, White); +// ssd1306_SetCursor(55, 20); +// rt_sprintf(buf, "0x%2d", get_Modbus_Addr()); +// ssd1306_WriteString(buf, Font_6x8, White); +// +// ssd1306_SetCursor(2, 32); +// ssd1306_WriteString("reg:", Font_7x10, White); +// ssd1306_SetCursor(55, 32); +// rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Addr()); +// ssd1306_WriteString(buf, Font_6x8, White); +// +// ssd1306_SetCursor(2, 44); +// ssd1306_WriteString("r_num:", Font_7x10, White); +// ssd1306_SetCursor(55, 44); +// rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Num()); +// ssd1306_WriteString(buf, Font_6x8, White); ssd1306_UpdateScreenArea(0, 18, 128, 46); } @@ -341,7 +341,7 @@ rt_uint8_t key_Value(void) { if (rt_pin_read(key_pins[var]) == PIN_LOW) { - rt_kprintf("%d\n", var); + // rt_kprintf("%d\n", var); return var + 1; } } @@ -404,7 +404,7 @@ void oled_menu() } menu = menu_table[menu_index].menu; - rt_kprintf("index %d\n", menu_index); + //rt_kprintf("index %d\n", menu_index); (*menu)(); } diff --git a/applications/OLED/gui.h b/applications/OLED/gui.h index 8d7f4df..b5b3690 100644 --- a/applications/OLED/gui.h +++ b/applications/OLED/gui.h @@ -21,7 +21,7 @@ #include #include #include -#include +//#include #include #include #include diff --git a/applications/config/console.c b/applications/config/console.c index be96c5e..e85fe80 100644 --- a/applications/config/console.c +++ b/applications/config/console.c @@ -577,15 +577,21 @@ void log_adc() set_ADC_log(1); } +void log_datu() +{ + set_DATU_log(1); +} + void no_log() { - + set_DATU_log(0); set_modbus_log(0); set_LTE_log(0); set_mqtt_log(0); set_ADC_log(0); } +MSH_CMD_EXPORT(log_datu, save to flash); MSH_CMD_EXPORT(change_config, save to flash); MSH_CMD_EXPORT(log_modbus, view_log to console); MSH_CMD_EXPORT(log_lte, view_log to console); diff --git a/applications/config/console.h b/applications/config/console.h index d03ee5f..55c588f 100644 --- a/applications/config/console.h +++ b/applications/config/console.h @@ -18,6 +18,7 @@ #include #include #include "modbus_rtu.h" +#include //读取配置 void read_config(); diff --git a/applications/thread/thread.c b/applications/thread/thread.c index f3625da..e8cba64 100644 --- a/applications/thread/thread.c +++ b/applications/thread/thread.c @@ -135,7 +135,8 @@ static void Task_485(void *parameter) { while (1) { - Modbus_init(); + //Modbus_init(); + datu_init(); rt_thread_mdelay(100); } } diff --git a/applications/thread/thread.h b/applications/thread/thread.h index d196972..0e52754 100644 --- a/applications/thread/thread.h +++ b/applications/thread/thread.h @@ -17,7 +17,8 @@ #include #include #include -#include +//#include +#include #include #include #include