Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
手册第6章串口驱动的例子是否缺少了rx_mq的初始化
发布于 2015-12-19 23:42:44 浏览:2018
订阅该版
手册rtthread_manual.zh中第6章,关于串口驱动使用的代码如下(第6章最后), ```/* * 程序清单:串口设备操作例程 * * 在这个例程中,将启动一个devt线程,然后打开串口1和2 * 当串口1和2有输入时,将读取其中的输入数据然后写入到 * 串口1设备中。 * */ #include
/* UART接收消息结构*/ struct rx_msg { rt_device_t dev; rt_size_t size; }; /* 用于接收消息的消息队列*/ static rt_mq_t rx_mq; /* 接收线程的接收缓冲区*/ static char uart_rx_buffer[64]; /* 数据到达回调函数*/ rt_err_t uart_input(rt_device_t dev, rt_size_t size) { struct rx_msg msg; msg.dev = dev; msg.size = size; /* 发送消息到消息队列中*/ rt_mq_send(rx_mq, &msg, sizeof(struct rx_msg)); return RT_EOK; } void device_thread_entry(void* parameter) { struct rx_msg msg; int count = 0; rt_device_t device, write_device; rt_err_t result = RT_EOK; /* 查找系统中的串口1设备 */ device = rt_device_find("uart1"); if (device!= RT_NULL) { /* 设置回调函数及打开设备*/ rt_device_set_rx_indicate(device, uart_input); rt_device_open(device, RT_DEVICE_OFLAG_RDWR|RT_DEVICE_FLAG_INT_RX); } /* 设置写设备为uart1设备 */ write_device = device; /* 查找系统中的串口2设备 */ device= rt_device_find("uart2"); if (device_!= RT_NULL) { /* 设置回调函数及打开设备*/ rt_device_set_rx_indicate(device, uart_input); rt_device_open(device, RT_DEVICE_OFLAG_RDWR); } while (1) { /* 从消息队列中读取消息*/ result = rt_mq_recv(rx_mq, &msg, sizeof(struct rx_msg), 50); if (result == -RT_ETIMEOUT) { /* 接收超时*/ rt_kprintf("timeout count:%d ", ++count); } /* 成功收到消息*/ if (result == RT_EOK) { rt_uint32_t rx_length; rx_length = (sizeof(uart_rx_buffer) - 1) > msg.size ? msg.size : sizeof(uart_rx_buffer) - 1; /* 读取消息*/ rx_length = rt_device_read(msg.dev, 0, &uart_rx_buffer[0], rx_length); uart_rx_buffer[rx_length] = ' '; /* 写到写设备中*/ if (write_device != RT_NULL) rt_device_write(write_device, 0, &uart_rx_buffer[0], rx_length); } } } int rt_application_init() { /* 创建devt线程*/ rt_thread_t thread = rt_thread_create("devt", device_thread_entry, RT_NULL, 1024, 25, 7); /* 创建成功则启动线程*/ if (thread_!= RT_NULL) rt_thread_startup(&thread); }``` 其中并没有rt_mq_create函数, 另外 ``` /* 查找系统中的串口2设备 */ device= rt_device_find("uart2"); if (device_!= RT_NULL) { /* 设置回调函数及打开设备*/ rt_device_set_rx_indicate(device, uart_input); rt_device_open(device, RT_DEVICE_OFLAG_RDWR); }``` uart2的rt_device_open函数没有用RT_DEVICE_FLAG_INT_RX参数,那么uart2就不会开启中断,回调函数uart_input就不会被调用,rx_mq也不会有数据,rt_mq_recv返回值不会是RT_EOK,所以也就不会调用rt_device_read函数,那么uart2接收的数据就不会被读取,不知道我分析的对不对? 是手册少了RT_DEVICE_FLAG_INT_RX参数还是其实可以正常运行? 我的理解是这个例子的作用是把uart1和uart2的输入都输出到uart1中,不知道是不是这样的?
查看更多
2
个回答
默认排序
按发布时间排序
pangwei
2015-12-21
这家伙很懒,什么也没写!
少了RT_DEVICE_FLAG_INT_RX参数,后面已经修改了。
撰写答案
登录
注册新账号
关注者
0
被浏览
2k
关于作者
moss
这家伙很懒,什么也没写!
提问
4
回答
226
被采纳
1
关注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
机器人操作系统 (ROS2) 和 RT-Thread 通信
4
国产MCU移植系列教程汇总,欢迎查看!
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
cJSON 避坑笔记
2
Vision-Board porting Xbox One USB Controller
3
可存万物的bsp_flash
4
TIM,硬件定时器驱动程序
5
LCD(mipi)+LVGL +gui(适配V8.x版本)
热门标签
RT-Thread Studio
串口
LWIP
Env
SPI
Bootloader
AT
ART-Pi
Hardfault
CAN总线
FinSH
USB
文件系统
DMA
RT-Thread
SCons
线程
RT-Thread Nano
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
ota在线升级
WIZnet_W5500
I2C_IIC
UART
flash
cubemx
packages_软件包
freemodbus
潘多拉开发板_Pandora
PWM
定时器
ADC
BSP
中断
编译报错
socket
flashDB
keil_MDK
GD32
Debug
MicroPython
msh
ulog
SFUD
SDIO总线
rt_mq_消息队列_msg_queue
本月问答贡献
出出啊
1506
个答案
339
次被采纳
小小李sunny
1423
个答案
281
次被采纳
张世争
740
个答案
161
次被采纳
crystal266
539
个答案
157
次被采纳
whj467467222
1218
个答案
146
次被采纳
本月文章贡献
出出啊
1
篇文章
5
次点赞
小小李sunny
1
篇文章
1
次点赞
张世争
3
篇文章
1
次点赞
crystal266
2
篇文章
1
次点赞
whj467467222
2
篇文章
2
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部