Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
FinSH
卡死
RT-Thread一般讨论
请教list_sem()输出卡死finsh终端的问题
发布于 2022-07-18 11:35:11 浏览:563
订阅该版
我实际的控制器是gd32f103的,然后建立项目的时候选的stm32f103的,直接pin to pin的,现在有个问题,我根据串口部分的教程弄了两个信号量用来做modbus通讯,现在的现象是只要在finsh终端使用list_sem命令,finsh终端就没反应了,但是串口依旧可打印出数据来,截图如下 ![Snipaste_2022-07-15_18-11-23.png](https://oss-club.rt-thread.org/uploads/20220718/4decb776696f00c49f9d67c73c9a9c0a.png "Snipaste_2022-07-15_18-11-23.png") ```c /* modbus_slave */ /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-07-12 lenovo the first version */ #include "rs485_slave.h" #include
#define DBG_TAG "rs485_slave" #define DBG_LVL DBG_INFO #include
#define RS485_SLAVE_RE_PIN GET_PIN(E, 15) #define RS485_SLAVE_TX_EN() rt_pin_write(RS485_SLAVE_RE_PIN, PIN_HIGH) #define RS485_SLAVE_RX_EN() rt_pin_write(RS485_SLAVE_RE_PIN, PIN_LOW) static rt_device_t _dev = RT_NULL; static rt_sem_t _rx_sem = RT_NULL; #define TAB_MAX_NUM 10 static uint8_t _tab_bits[TAB_MAX_NUM] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; static uint8_t _tab_input_bits[TAB_MAX_NUM] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; static uint16_t _tab_registers[TAB_MAX_NUM] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; static uint16_t _tab_input_registers[TAB_MAX_NUM] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; /** * @brief 从机回调函数 * @param ctx modbus 句柄 * @param slave_info 从机信息体 * @return =0:正常; * <0:异常 * (-AGILE_MODBUS_EXCEPTION_UNKNOW(-255): 未知异常,从机不会打包响应数据) * (其他负数异常码: 从机会打包异常响应数据) */ static int slave_callback(agile_modbus_t *ctx, struct agile_modbus_slave_info *slave_info) { int function = slave_info->sft->function; int ret = 0; switch (function) { case AGILE_MODBUS_FC_READ_COILS: case AGILE_MODBUS_FC_READ_DISCRETE_INPUTS: { int address = slave_info->address; int nb = slave_info->nb; int send_index = slave_info->send_index; int is_input = (function == AGILE_MODBUS_FC_READ_DISCRETE_INPUTS); for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) { if (now_address >= 0 && now_address < TAB_MAX_NUM) { int index = now_address - 0; agile_modbus_slave_io_set(ctx->send_buf + send_index, i, is_input ? _tab_input_bits[index] : _tab_bits[index]); } } } break; case AGILE_MODBUS_FC_READ_HOLDING_REGISTERS: case AGILE_MODBUS_FC_READ_INPUT_REGISTERS: { int address = slave_info->address; int nb = slave_info->nb; int send_index = slave_info->send_index; int is_input = (function == AGILE_MODBUS_FC_READ_INPUT_REGISTERS); for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) { if (now_address >= 0 && now_address < TAB_MAX_NUM) { int index = now_address - 0; agile_modbus_slave_register_set(ctx->send_buf + send_index, i, is_input ? _tab_input_registers[index] : _tab_registers[index]); } } } break; case AGILE_MODBUS_FC_WRITE_SINGLE_COIL: case AGILE_MODBUS_FC_WRITE_MULTIPLE_COILS: { int address = slave_info->address; if (function == AGILE_MODBUS_FC_WRITE_SINGLE_COIL) { if (address >= 0 && address < TAB_MAX_NUM) { int index = address - 0; int data = *((int *)slave_info->buf); _tab_bits[index] = data; } } else { int nb = slave_info->nb; for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) { if (now_address >= 0 && now_address < TAB_MAX_NUM) { int index = now_address - 0; uint8_t status = agile_modbus_slave_io_get(slave_info->buf, i); _tab_bits[index] = status; } } } } break; case AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER: case AGILE_MODBUS_FC_WRITE_MULTIPLE_REGISTERS: { int address = slave_info->address; if (function == AGILE_MODBUS_FC_WRITE_SINGLE_REGISTER) { if (address >= 0 && address < TAB_MAX_NUM) { int index = address - 0; int data = *((int *)slave_info->buf); _tab_registers[index] = data; } } else { int nb = slave_info->nb; for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) { if (now_address >= 0 && now_address < TAB_MAX_NUM) { int index = now_address - 0; uint16_t data = agile_modbus_slave_register_get(slave_info->buf, i); _tab_registers[index] = data; } } } } break; case AGILE_MODBUS_FC_MASK_WRITE_REGISTER: { int address = slave_info->address; if (address >= 0 && address < TAB_MAX_NUM) { int index = address - 0; uint16_t data = _tab_registers[index]; uint16_t and = (slave_info->buf[0] << 8) + slave_info->buf[1]; uint16_t or = (slave_info->buf[2] << 8) + slave_info->buf[3]; data = (data & and) | (or &(~and)); _tab_registers[index] = data; } } break; case AGILE_MODBUS_FC_WRITE_AND_READ_REGISTERS: { int address = slave_info->address; int nb = (slave_info->buf[0] << 8) + slave_info->buf[1]; uint16_t address_write = (slave_info->buf[2] << 8) + slave_info->buf[3]; int nb_write = (slave_info->buf[4] << 8) + slave_info->buf[5]; int send_index = slave_info->send_index; /* Write first. 7 is the offset of the first values to write */ for (int now_address = address_write, i = 0; now_address < address_write + nb_write; now_address++, i++) { if (now_address >= 0 && now_address < TAB_MAX_NUM) { int index = now_address - 0; uint16_t data = agile_modbus_slave_register_get(slave_info->buf + 7, i); _tab_registers[index] = data; } } /* and read the data for the response */ for (int now_address = address, i = 0; now_address < address + nb; now_address++, i++) { if (now_address >= 0 && now_address < TAB_MAX_NUM) { int index = now_address - 0; agile_modbus_slave_register_set(ctx->send_buf + send_index, i, _tab_registers[index]); } } } break; default: ret = -AGILE_MODBUS_EXCEPTION_ILLEGAL_FUNCTION; break; } return ret; } static rt_err_t rs485_ind_cb(rt_device_t dev, rt_size_t size) { if (size > 0) { rt_sem_release(_rx_sem); } return RT_EOK; } int rs485_slave_send(uint8_t *buf, int len) { RS485_SLAVE_TX_EN(); rt_device_write(_dev, 0, buf, len); RS485_SLAVE_RX_EN(); return len; } int rs485_slave_receive(uint8_t *buf, int bufsz, int timeout, int bytes_timeout) { int len = 0; while(1) { rt_sem_control(_rx_sem, RT_IPC_CMD_RESET, RT_NULL); int rc = rt_device_read(_dev, 0, buf + len, bufsz); if(rc > 0) { timeout = bytes_timeout; len += rc; bufsz -= rc; if(bufsz == 0) break; continue; } if(rt_sem_take(_rx_sem, rt_tick_from_millisecond(timeout)) != RT_EOK) break; timeout = bytes_timeout; } return len; } int rs485_slave_init(void) { /* init config paras */ struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; rt_pin_mode(RS485_SLAVE_RE_PIN, PIN_MODE_OUTPUT); RS485_SLAVE_TX_EN(); _rx_sem = rt_sem_create("M_slave", 0, RT_IPC_FLAG_FIFO); if(_rx_sem == RT_NULL) { LOG_E("create rs485_slave_rx_sem failed."); return -RT_ERROR; } /* step1: find serial device */ _dev = rt_device_find("uart3"); if (_dev == RT_NULL) { LOG_E("can't find device uart3."); rt_sem_delete(_rx_sem); return -RT_ERROR; } /* step2: change serial config */ config.baud_rate = BAUD_RATE_9600; //baud_rate 9600 config.data_bits = DATA_BITS_8; //data_bits 8 config.stop_bits = STOP_BITS_1; //stop_bits 1 config.bufsz = 1024; //bufsz 512 config.parity = PARITY_NONE; //parity none /* step3: control serial config */ rt_device_control(_dev, RT_DEVICE_CTRL_CONFIG, &config); /* step4: set reback function */ rt_device_set_rx_indicate(_dev, rs485_ind_cb); /* step5: open serial device */ rt_device_open(_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX); RS485_SLAVE_RX_EN(); return RT_EOK; } void rs485_slave_thread_entry(void *p) { uint8_t ctx_send_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; uint8_t ctx_read_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; 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); while(1) { rt_thread_mdelay(100); int read_len = rs485_slave_receive(ctx->read_buf, ctx->read_bufsz, 1000, 20); if (read_len == 0) { //LOG_W("Receive Master Cmd timeout."); continue; } int rc = agile_modbus_slave_handle(ctx, read_len, 1, slave_callback, NULL); if (rc < 0) { LOG_W("Master Cmd Receive failed."); if (rc != -1) LOG_W("Error code:%d", -128 - rc); continue; } rs485_slave_send(ctx->send_buf, rc); } } int rs485_slave_app(void) { rt_thread_t rs485_slave_thread = RT_NULL; rs485_slave_thread = rt_thread_create("M_slave_app", rs485_slave_thread_entry, RT_NULL, 2048, 15, 10); if(rs485_slave_thread == RT_NULL) { return RT_ERROR; } rt_thread_startup(rs485_slave_thread); rs485_slave_init(); return RT_EOK; } INIT_APP_EXPORT(rs485_slave_app); ``` ```c /* modbus_master */ /* * Copyright (c) 2006-2021, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2022-07-11 lenovo the first version */ #include "rs485_master.h" #include
#define DBG_TAG "rs485_master" #define DBG_LVL DBG_INFO #include
#define RS485_MASTER_RE_PIN GET_PIN(A, 1) #define RS485_MASTER_TX_EN() rt_pin_write(RS485_MASTER_RE_PIN, PIN_HIGH) #define RS485_MASTER_RX_EN() rt_pin_write(RS485_MASTER_RE_PIN, PIN_LOW) static rt_device_t _dev = RT_NULL; static rt_sem_t _rx_sem = RT_NULL; static rt_err_t rs485_master_ind_cb(rt_device_t dev, rt_size_t size) { if (size > 0) { rt_sem_release(_rx_sem); } return RT_EOK; } int rs485_master_send(uint8_t *buf, int len) { RS485_MASTER_TX_EN(); rt_device_write(_dev, 0, buf, len); RS485_MASTER_RX_EN(); return len; } int rs485_master_receive(uint8_t *buf, int bufsz, int timeout, int bytes_timeout) { int len = 0; while(1) { rt_sem_control(_rx_sem, RT_IPC_CMD_RESET, RT_NULL); int rc = rt_device_read(_dev, 0, buf + len, bufsz); if(rc > 0) { timeout = bytes_timeout; len += rc; bufsz -= rc; if(bufsz == 0) break; continue; } if(rt_sem_take(_rx_sem, rt_tick_from_millisecond(timeout)) != RT_EOK) break; timeout = bytes_timeout; } return len; } int rs485_master_init(void) { /* init config paras */ struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; rt_pin_mode(RS485_MASTER_RE_PIN, PIN_MODE_OUTPUT); RS485_MASTER_RX_EN(); _rx_sem = rt_sem_create("M_master", 0, RT_IPC_FLAG_FIFO); if(_rx_sem == RT_NULL) { LOG_E("create rs485_master_rx_sem failed."); return -RT_ERROR; } /* step1: find serial device */ _dev = rt_device_find("uart2"); if (_dev == RT_NULL) { LOG_E("can't find device uart2."); rt_sem_delete(_rx_sem); return -RT_ERROR; } /* step2: change serial config */ config.baud_rate = BAUD_RATE_9600; //baud_rate 9600 config.data_bits = DATA_BITS_8; //data_bits 8 config.stop_bits = STOP_BITS_1; //stop_bits 1 config.bufsz = 1024; //bufsz 512 config.parity = PARITY_NONE; //parity none /* step3: control serial config */ rt_device_control(_dev, RT_DEVICE_CTRL_CONFIG, &config); /* step4: set reback function */ rt_device_set_rx_indicate(_dev, rs485_master_ind_cb); /* step5: open serial device */ rt_device_open(_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX); return RT_EOK; } #define RegisterCount 10 void rs485_master_thread_entry(void *p) { uint8_t ctx_send_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; uint8_t ctx_read_buf[AGILE_MODBUS_MAX_ADU_LENGTH]; uint16_t hold_register[RegisterCount]; uint8_t coli_register[RegisterCount] = { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; 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("rs485_master_thread Running."); while (1) { rt_thread_mdelay(500); agile_modbus_set_slave(ctx, 1); int send_len = agile_modbus_serialize_read_registers(ctx, 64, RegisterCount); rs485_master_send(ctx->send_buf, send_len); int read_len = rs485_master_receive(ctx->read_buf, ctx->read_bufsz, 1000, 20); if (read_len == 0) { //LOG_W("address 1 Receive timeout."); continue; } int rc = agile_modbus_deserialize_read_registers(ctx, read_len, hold_register); if (rc < 0) { //LOG_W("address 1 Receive failed."); if (rc != -1) LOG_W("Error code:%d", -128 - rc); continue; } /* LOG_I("Hold Registers:"); for (int i = 0; i < RegisterCount; i++) LOG_I("Register [%d]: 0x%04X", i, hold_register[i]); rt_kprintf("\r\n\r\n\r\n");*/ for(int j = 0; j < RegisterCount; j++) { coli_register[j] = !coli_register[j]; } agile_modbus_set_slave(ctx, 2); send_len = agile_modbus_serialize_write_bits(ctx, 0, RegisterCount, &coli_register[0]); rs485_master_send(ctx->send_buf, send_len); read_len = rs485_master_receive(ctx->read_buf, ctx->read_bufsz, 1000, 20); if (read_len == 0) { //LOG_W("address 2 Receive timeout."); continue; } rc = agile_modbus_deserialize_write_bits(ctx, read_len); if (rc < 0) { //LOG_W("address 2 Receive failed."); if (rc != -1) LOG_W("Error code:%d", -128 - rc); continue; }else { /*LOG_I("Write Coils Registers is Ok!");*/ } } } int rs485_master_app(void) { rt_thread_t rs485_master_thread = RT_NULL; rs485_master_thread = rt_thread_create("M_master_app", rs485_master_thread_entry, RT_NULL, 2048, 14, 10); if(rs485_master_thread == RT_NULL) { return RT_ERROR; } rt_thread_startup(rs485_master_thread); rs485_master_init(); return RT_EOK; } INIT_APP_EXPORT(rs485_master_app); ``` 整个工程代码如下,文件有点大我分包了: [testadc.zip](https://club.rt-thread.org/file_download/34c90ca010bc2c6c) [testadc.z01](https://club.rt-thread.org/file_download/367d16b744b94f89) [testadc.z02](https://club.rt-thread.org/file_download/4cbf4760ccd7a25a)
查看更多
2
个回答
默认排序
按发布时间排序
aozima
2022-07-18
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
调试器接上看下,可能被写穿了。
abcde1224
2022-07-18
这家伙很懒,什么也没写!
再补一张图,上面的问题解决后再运行list_sem有时候又出现打印如下图这个情况…… ![Snipaste_2022-07-18_13-36-36.png](https://oss-club.rt-thread.org/uploads/20220718/98ae5bf15425cfe51b6caea8b271c997.png "Snipaste_2022-07-18_13-36-36.png")
撰写答案
登录
注册新账号
关注者
0
被浏览
563
关于作者
abcde1224
这家伙很懒,什么也没写!
提问
2
回答
5
被采纳
0
关注TA
发私信
相关问题
1
有关动态模块加载的一篇论文
2
最近的调程序总结
3
晕掉了,这么久都不见layer2的踪影啊
4
继续K9ii的历程
5
[GUI相关] FreeType 2
6
[GUI相关]嵌入式系统中文输入法的设计
7
20081101 RT-Thread开发者聚会总结
8
嵌入式系统基础
9
linux2.4.19在at91rm9200 上的寄存器设置
10
[转]基于嵌入式Linux的通用触摸屏校准程序
推荐文章
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组件
最新文章
1
使用百度AI助手辅助编写一个rt-thread下的ONVIF设备发现功能的功能代码
2
RT-Thread 发布 EtherKit开源以太网硬件!
3
rt-thread使用cherryusb实现虚拟串口
4
《C++20 图形界面程序:速度与渲染效率的双重优化秘籍》
5
《原子操作:程序世界里的“最小魔法单位”解析》
热门标签
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
ESP8266
I2C_IIC
WIZnet_W5500
UART
ota在线升级
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
at_device
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
a1012112796
13
个答案
2
次被采纳
张世争
9
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
本月文章贡献
程序员阿伟
7
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
3
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部