開(kāi)發(fā)環(huán)境:野火的stm32f407,rt-thread studio版本是版本: 2.2.6,stm32f4的資源包為0.2.2,Agile Modbus軟件包版本為v1.1.2。工程使用上一篇 RT-Thread中RS485驅(qū)動(dòng)包的使用 工程為基礎(chǔ)。
單片機(jī)作為主站Master,從站使用軟件Modbus Slave模擬。
打開(kāi)工程,添加軟件包Agile Modbus,添加成功,先進(jìn)行編譯,查看是否有錯(cuò)誤。編譯正常。
根據(jù)Agile Modbus軟件包的文檔介紹,
- 主機(jī):
- 1.agile_modbus_rtu_init / agile_modbus_tcp_init 初始化 RTU/TCP 環(huán)境
- 2.agile_modbus_set_slave 設(shè)置從機(jī)地址
- 3.清空接收緩存
- 4.agile_modbus_serialize_xxx 打包請(qǐng)求數(shù)據(jù)
- 5.發(fā)送數(shù)據(jù)
- 6.等待數(shù)據(jù)接收結(jié)束
- 7.agile_modbus_deserialize_xxx 解析響應(yīng)數(shù)據(jù)
- 8.用戶處理得到的數(shù)據(jù)
參考代碼為
#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("rnrnrn");
}
}
因?yàn)槲覀兪褂玫氖莚s485驅(qū)動(dòng)包,根據(jù)例程,我們知道需要增加幾個(gè)函數(shù),如下:
static int Bsp_Rs485_init(void); //Rs485初始化+設(shè)置超時(shí)時(shí)間
static int Bsp_Rs485_Tx(uint8_t *buf,int size); //Rs485 發(fā)送
static int Bsp_Rs485_Rx(uint8_t *buf,int size); //Rs485接受
根據(jù)例程,修改主函數(shù)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("rnrnrn");
}
}
編譯正常,下載到開(kāi)發(fā)板。
打開(kāi)模擬軟件Modbus Slave,設(shè)置基本參數(shù)。Modbus Slave的使用參考:參考
可以看到輸出端口打印出了從站數(shù)據(jù)的信息。
-
驅(qū)動(dòng)器
+關(guān)注
關(guān)注
54文章
8378瀏覽量
147717 -
ModBus協(xié)議
+關(guān)注
關(guān)注
3文章
179瀏覽量
33702 -
RS485接口
+關(guān)注
關(guān)注
2文章
62瀏覽量
14310 -
STM32F407
+關(guān)注
關(guān)注
15文章
188瀏覽量
29821 -
RT-Thread
+關(guān)注
關(guān)注
31文章
1321瀏覽量
40845
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
使用menuconfig配置基于RT-Thread的NimBLE軟件包
基于RT-Thread實(shí)現(xiàn)的Agile Modbus協(xié)議棧
RT-Thread中Agile Modbus軟件包的使用
RT-Thread中的mymqtt軟件包添加步驟與使用方法
RT-Thread Studio安裝軟件包agile-modbus后編譯出現(xiàn)arm-none-eabi-gcc: fatal錯(cuò)誤
RT-Thread 軟件包介紹
RT-Thread軟件包定義和使用

RT-Thread社區(qū)上有哪些優(yōu)秀的軟件包?
2022 RT-Thread全球技術(shù)大會(huì):RT-Thread軟件包

RT-Thread全球技術(shù)大會(huì):恩智浦新增100+軟件包

RT-Thread在線軟件包改為本地軟件包的方法

RT-Thread使用cjson軟件包發(fā)送64位長(zhǎng)整型數(shù)據(jù)

RT-Thread中Lan8720和lwip協(xié)議棧的使用

RT-Thread中mymqtt軟件包的使用方法

評(píng)論