Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
agile_modbus
RS485
串口
RT-Thread中Agile Modbus软件包的使用
发布于 2023-02-06 15:14:50 浏览:1859
订阅该版
开发环境:野火的stm32f407,rt-thread studio版本是版本: 2.2.6,stm32f4的资源包为0.2.2,Agile Modbus软件包版本为v1.1.2。工程使用上一篇 RT-Thread中RS485驱动包的使用 工程为基础。 单片机作为主站Master,从站使用软件Modbus Slave模拟。 打开工程,添加软件包Agile Modbus,添加成功,先进行编译,查看是否有错误。编译正常。 根据Agile Modbus软件包的文档介绍, * 主机: * 1.agile_modbus_rtu_init / agile_modbus_tcp_init 初始化 RTU/TCP 环境 * 2.agile_modbus_set_slave 设置从机地址 * 3.清空接收缓存 * 4.agile_modbus_serialize_xxx 打包请求数据 * 5.发送数据 * 6.等待数据接收结束 * 7.agile_modbus_deserialize_xxx 解析响应数据 * 8.用户处理得到的数据 [参考网址](https://github.com/loogg/agile_modbus_mcu_demos/blob/master/bootloader_rtt/applications/main.c),参考代码为 ``` #include
#include
#include "rs485.h" #include
#define DBG_TAG "rtu_master" #define DBG_LVL DBG_INFO #include
int main(void) { rs485_init(); uint8_t ctx_send_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; uint8_t ctx_read_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; uint16_t hold_register[10]; agile_modbus_rtu_t ctx_rtu; agile_modbus_t *ctx = &ctx_rtu._ctx; agile_modbus_rtu_init(&ctx_rtu, ctx_send_buf, sizeof(ctx_send_buf), ctx_read_buf, sizeof(ctx_read_buf)); agile_modbus_set_slave(ctx, 1); LOG_I("Running."); while (1) { rt_thread_mdelay(100); int send_len = agile_modbus_serialize_read_registers(ctx, 0, 10); rs485_send(ctx->send_buf, send_len); int read_len = rs485_receive(ctx->read_buf, ctx->read_bufsz, 1000, 20); if (read_len == 0) { LOG_W("Receive timeout."); continue; } int rc = agile_modbus_deserialize_read_registers(ctx, read_len, hold_register); if (rc < 0) { LOG_W("Receive failed."); if (rc != -1) LOG_W("Error code:%d", -128 - rc); continue; } LOG_I("Hold Registers:"); for (int i = 0; i < 10; i++) LOG_I("Register [%d]: 0x%04X", i, hold_register[i]); rt_kprintf("\r\n\r\n\r\n"); } } ``` 因为我们使用的是rs485驱动包,根据例程,我们知道需要增加几个函数,如下: ``` static int Bsp_Rs485_init(void); //Rs485初始化+设置超时时间 static int Bsp_Rs485_Tx(uint8_t *buf,int size); //Rs485 发送 static int Bsp_Rs485_Rx(uint8_t *buf,int size); //Rs485接受 ``` 根据例程,修改主函数modbus后的代码如下: ``` #include
#include
#include "rs485.h" #include
#define DBG_TAG "rtu_master" #define DBG_LVL DBG_INFO #include
int main(void) { Bsp_Rs485_init();// uint8_t ctx_send_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; uint8_t ctx_read_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; uint16_t hold_register[10]; agile_modbus_rtu_t ctx_rtu; agile_modbus_t *ctx = &ctx_rtu._ctx; agile_modbus_rtu_init(&ctx_rtu, ctx_send_buf, sizeof(ctx_send_buf), ctx_read_buf, sizeof(ctx_read_buf)); agile_modbus_set_slave(ctx, 1); LOG_I("Running."); while (1) { rt_thread_mdelay(100); int send_len = agile_modbus_serialize_read_registers(ctx, 0, 10); Bsp_Rs485_Tx(ctx->send_buf, send_len);// int read_len = Bsp_Rs485_Rx(ctx->read_buf, ctx->read_bufsz);// if (read_len == 0) { LOG_W("Receive timeout."); continue; } int rc = agile_modbus_deserialize_read_registers(ctx, read_len, hold_register); if (rc < 0) { LOG_W("Receive failed."); if (rc != -1) LOG_W("Error code:%d", -128 - rc); continue; } LOG_I("Hold Registers:"); for (int i = 0; i < 10; i++) LOG_I("Register [%d]: 0x%04X", i, hold_register[i]); rt_kprintf("\r\n\r\n\r\n"); } } ``` 编译正常,下载到开发板。 打开模拟软件Modbus Slave,设置基本参数。Modbus Slave的使用参考:[参考](https://chenzhengyi.blog.csdn.net/article/details/77979114) 可以看到输出端口打印出了从站数据的信息。 ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230206/ebc8ecd05e227197fb2ffe24be3eceb5.png) ![screenshot_image.png](https://oss-club.rt-thread.org/uploads/20230206/7861bd415846452fc17cd50522c27c09.png)
3
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
YZRD
这家伙很懒,什么也没写!
文章
25
回答
224
被采纳
19
关注TA
发私信
相关文章
1
串口DMA发送数据时,数据被覆盖
2
关于串口DMA模式下rt_device_close问题
3
利用stm32f427实现usb转串口,电脑端什么也没有识别到
4
finsh 控制台 适配 RS 485请大神指点????
5
uart_sample.c 中,读串口设备时偏移量pos要设置为-1而不是0?
6
【结贴】at_device软件包中对串口接收数据缺少判断导致数据接收异常
7
串口无法接受数据,但可以发送
8
串口如何有效的清除掉接收缓冲,而不必一个一个的去读取
9
串口接收使用方式问题
10
雅特力FINSH问题
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
I2C_IIC
ESP8266
UART
WIZnet_W5500
ota在线升级
cubemx
PWM
flash
freemodbus
BSP
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
flashDB
GD32
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
SFUD
msh
keil_MDK
ulog
C++_cpp
MicroPython
本月问答贡献
出出啊
1518
个答案
343
次被采纳
小小李sunny
1444
个答案
290
次被采纳
张世争
813
个答案
177
次被采纳
crystal266
547
个答案
161
次被采纳
whj467467222
1222
个答案
149
次被采纳
本月文章贡献
出出啊
1
篇文章
5
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
1
篇文章
3
次点赞
crystal266
2
篇文章
2
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部