修改pin
添加ltdc 添加modbus
This commit is contained in:
1 parent
ed00051a2c
commit
425aec3576
25 files changed
+8341
-112
No files matched your search
@@ -22,7 +22,7 @@ static struct rt_semaphore rx_sem;
|
||||
|
||||
#define RX_LINE_BUF_SIZE 1024
|
||||
#define RS485_SAMPLE_SLAVE_SERIAL "uart1"
|
||||
#define RS485_SAMPLE_SLAVE_BAUDRATE 115200
|
||||
#define RS485_SAMPLE_SLAVE_BAUDRATE 57600
|
||||
#define RS485_SAMPLE_MASTER_PARITY 0 //0 -- none parity
|
||||
#define RS485_SAMPLE_SLAVE_PIN -1 //-1 -- nonuse rs485 mode control
|
||||
#define RS485_SAMPLE_SLAVE_LVL 1
|
||||
@@ -89,7 +89,7 @@ void uart_thread_entry(void *parameter)
|
||||
|
||||
int start_uart_thread(void)
|
||||
{
|
||||
rt_thread_t tid = rt_thread_create("uart1",uart_thread_entry,RT_NULL,2048,21,10);
|
||||
rt_thread_t tid = rt_thread_create("sccm",uart_thread_entry,RT_NULL,2048,21,10);
|
||||
if (tid != RT_NULL)
|
||||
{
|
||||
rt_thread_startup(tid);
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (c) 2006-2021, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2025-10-20 Administrator the first version
|
||||
* 2025-11-12 Modified Switch to IDLE interrupt mode (no DMA, no ringbuffer)
|
||||
* 2025-11-21 Fixed Enable RX_IDLE mode correctly for RT-Thread 5.1
|
||||
*/
|
||||
|
||||
#include "rtthread.h"
|
||||
#include "modbus.h"
|
||||
|
||||
#define DBG_TAG "plc.rtu"
|
||||
#define DBG_LVL DBG_LOG
|
||||
#include <rtdbg.h>
|
||||
|
||||
static const mb_backend_param_t mb_bkd_prm = {
|
||||
.rtu.dev = "uart2", //设备名
|
||||
.rtu.baudrate = 57600, //波特率
|
||||
.rtu.parity = 0, //校验位, 0-无, 1-奇, 2-偶
|
||||
.rtu.pin = -1, //控制引脚, <0 表示不使用
|
||||
.rtu.lvl = 0 //控制发送电平
|
||||
};
|
||||
|
||||
|
||||
static void mb_plc_read_regs(mb_inst_t *hinst)
|
||||
{
|
||||
if (mb_connect(hinst) < 0)//连接失败, 延时返回
|
||||
{
|
||||
LOG_E("modbus connect fail.");
|
||||
return;
|
||||
}
|
||||
|
||||
u16 regs[64];
|
||||
int addr = 7000;
|
||||
int nb = 10;
|
||||
int total = mb_read_input_regs(hinst, addr, nb, regs);
|
||||
if (total <= 0)
|
||||
{
|
||||
LOG_E("modbus read register fail.");
|
||||
return;
|
||||
}
|
||||
u8 regsM[64];
|
||||
int addrM = 3000;
|
||||
int nbM = 10;
|
||||
int totalM = mb_read_bits(hinst, addrM, nbM, regsM);
|
||||
if (totalM <= 0)
|
||||
{
|
||||
LOG_E("modbus read register fail.");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
static void mb_plc_thread(void *args)//线程服务函数
|
||||
{
|
||||
mb_inst_t *hinst = mb_create(MB_BACKEND_TYPE_RTU, &mb_bkd_prm);
|
||||
RT_ASSERT(hinst != NULL);
|
||||
|
||||
mb_set_slave(hinst, 1);//修改从机地址, 默认地址为1, 可根据实际情况修改
|
||||
mb_set_prot(hinst, MB_PROT_RTU);//修改通信协议类型, RTU后端默认使用MODBUS-RTU通信协议
|
||||
mb_set_tmo(hinst, 500, 15);//修改超时时间, 应答超时500ms(默认300ms), 字节超时15ms(默认32ms)
|
||||
|
||||
while(1)
|
||||
{
|
||||
mb_plc_read_regs(hinst);
|
||||
rt_thread_mdelay(50);
|
||||
}
|
||||
}
|
||||
|
||||
static int mb_rtu_master_startup(void)
|
||||
{
|
||||
rt_thread_t tid = rt_thread_create("plc_rtu", mb_plc_thread, NULL, 2048, 5, 20);
|
||||
RT_ASSERT(tid != NULL);
|
||||
rt_thread_startup(tid);
|
||||
return(0);
|
||||
}
|
||||
INIT_APP_EXPORT(mb_rtu_master_startup);
|
||||
@@ -22,6 +22,7 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <ctype.h>
|
||||
#include "ini.h"
|
||||
|
||||
|
||||
Reference in new issue
Block a user