CAMS协议适配

This commit is contained in:
李健 2024-05-16 19:07:30 +08:00
parent fd397b5bbf
commit de6db49458
10 changed files with 329 additions and 28 deletions

View File

@ -1,12 +1,12 @@
## CAMS DATU CAMS计算机辅助缝纫管理系统采集模块 ## CAMS DATU CAMS计算机辅助缝纫管理系统采集模块
#### 说明 #### 说明
- 待完成 - 待完成
RS232协议未指定 RS232协议未指定
发送数据信息(未指定格式)
- 已完成 - 已完成
@ -25,6 +25,8 @@
8.4G联网、MQTT连接 8.4G联网、MQTT连接
9.命令输出指定log用于调试 9.命令输出指定log用于调试
10 .适配协议CAMS协议解析与发送指令接收后具体事项还未确定
#### 开发环境 #### 开发环境
@ -52,7 +54,7 @@ RT-Thread 软件打开 [点击下载](https://www.rt-thread.org/download.html#do
| fram | 读写flash用以保存读取配置 | | fram | 读写flash用以保存读取配置 |
| IO | 端口电平等 | | IO | 端口电平等 |
| OLED | 屏幕 | | OLED | 屏幕 |
| modbus | modbus 功能 | | modbus | modbus 功能CAMS协议适配 |
| thread | 线程管理 | | thread | 线程管理 |
| LTE | 网络配置 | | LTE | 网络配置 |
| ADC | ADC读值转换0-10V、3.3V、4-20ma等 | | 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_lte | 4G与mqtt的log |
| log_moddbus | modbus的log | | log_moddbus | modbus的log |
| log_adc | adc的log | | log_adc | adc的log |
| log_datu | datu协议栈收发的log |
| no_log | 关闭log | | no_log | 关闭log |
| IO_key < 0-8 > < 1 0 >| 指定输出端口改变电平 | | IO_key < 0-8 > < 1 0 >| 指定输出端口改变电平 |

View File

@ -63,7 +63,9 @@ void adc_read()
* = vout - 0.2* 6.8 / 2 * = vout - 0.2* 6.8 / 2
* *
* 4-20ma vin = i * 150 * 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); //读取采样值 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) 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) if (ADC.log)
@ -96,7 +98,7 @@ void adc_read()
} }
else if (ADC.ADC2_mode == 2) 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) if (ADC.log)

256
applications/Modbus/DATU.c Normal file
View File

@ -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 <DATU.h>
#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;
}

View File

@ -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 <rtthread.h>
#include <drv_common.h>
#include <rtdevice.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <stdint.h>
#include <rs485.h>
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_ */

View File

@ -252,23 +252,23 @@ void Menu3_485()
{ {
// char buf[4]; // char buf[4];
ssd1306_FillArea(0, 18, 128, 46, Black); ssd1306_FillArea(0, 18, 128, 46, Black);
ssd1306_SetCursor(2, 20); // ssd1306_SetCursor(2, 20);
ssd1306_WriteString("addr:", Font_7x10, White); // ssd1306_WriteString("addr:", Font_7x10, White);
ssd1306_SetCursor(55, 20); // ssd1306_SetCursor(55, 20);
rt_sprintf(buf, "0x%2d", get_Modbus_Addr()); // rt_sprintf(buf, "0x%2d", get_Modbus_Addr());
ssd1306_WriteString(buf, Font_6x8, White); // ssd1306_WriteString(buf, Font_6x8, White);
//
ssd1306_SetCursor(2, 32); // ssd1306_SetCursor(2, 32);
ssd1306_WriteString("reg:", Font_7x10, White); // ssd1306_WriteString("reg:", Font_7x10, White);
ssd1306_SetCursor(55, 32); // ssd1306_SetCursor(55, 32);
rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Addr()); // rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Addr());
ssd1306_WriteString(buf, Font_6x8, White); // ssd1306_WriteString(buf, Font_6x8, White);
//
ssd1306_SetCursor(2, 44); // ssd1306_SetCursor(2, 44);
ssd1306_WriteString("r_num:", Font_7x10, White); // ssd1306_WriteString("r_num:", Font_7x10, White);
ssd1306_SetCursor(55, 44); // ssd1306_SetCursor(55, 44);
rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Num()); // rt_sprintf(buf, "0x%2d", get_Modbus_Reg_Num());
ssd1306_WriteString(buf, Font_6x8, White); // ssd1306_WriteString(buf, Font_6x8, White);
ssd1306_UpdateScreenArea(0, 18, 128, 46); 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) if (rt_pin_read(key_pins[var]) == PIN_LOW)
{ {
rt_kprintf("%d\n", var); // rt_kprintf("%d\n", var);
return var + 1; return var + 1;
} }
} }
@ -404,7 +404,7 @@ void oled_menu()
} }
menu = menu_table[menu_index].menu; menu = menu_table[menu_index].menu;
rt_kprintf("index %d\n", menu_index); //rt_kprintf("index %d\n", menu_index);
(*menu)(); (*menu)();
} }

View File

@ -21,7 +21,7 @@
#include <LTE.h> #include <LTE.h>
#include <mb85rs.h> #include <mb85rs.h>
#include <gui.h> #include <gui.h>
#include <modbus_rtu.h> //#include <modbus_rtu.h>
#include <mqtt.h> #include <mqtt.h>
#include <adc.h> #include <adc.h>
#include <inout.h> #include <inout.h>

View File

@ -577,15 +577,21 @@ void log_adc()
set_ADC_log(1); set_ADC_log(1);
} }
void log_datu()
{
set_DATU_log(1);
}
void no_log() void no_log()
{ {
set_DATU_log(0);
set_modbus_log(0); set_modbus_log(0);
set_LTE_log(0); set_LTE_log(0);
set_mqtt_log(0); set_mqtt_log(0);
set_ADC_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(change_config, save to flash);
MSH_CMD_EXPORT(log_modbus, view_log to console); MSH_CMD_EXPORT(log_modbus, view_log to console);
MSH_CMD_EXPORT(log_lte, view_log to console); MSH_CMD_EXPORT(log_lte, view_log to console);

View File

@ -18,6 +18,7 @@
#include <adc.h> #include <adc.h>
#include <mqtt.h> #include <mqtt.h>
#include "modbus_rtu.h" #include "modbus_rtu.h"
#include <DATU.h>
//读取配置 //读取配置
void read_config(); void read_config();

View File

@ -135,7 +135,8 @@ static void Task_485(void *parameter)
{ {
while (1) while (1)
{ {
Modbus_init(); //Modbus_init();
datu_init();
rt_thread_mdelay(100); rt_thread_mdelay(100);
} }
} }

View File

@ -17,7 +17,8 @@
#include <LTE.h> #include <LTE.h>
#include <mb85rs.h> #include <mb85rs.h>
#include <gui.h> #include <gui.h>
#include <modbus_rtu.h> //#include <modbus_rtu.h>
#include <DATU.h>
#include <console.h> #include <console.h>
#include <mqtt.h> #include <mqtt.h>
#include <adc.h> #include <adc.h>