Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
RT-Thread一般讨论
rt-thread 2.01版本的串口如何使用?
发布于 2016-02-17 00:37:39 浏览:2328
订阅该版
我照着rt-thread的编程手册中的第六章弄的,可以发送但是不能接收 ``` #include "echo.h" struct rx_msg { rt_device_t dev; rt_size_t size; }; static struct rt_messagequeue rx_mq; static char uart_rx_buffer[64]; static char msg_pool[2048]; // 串口侦听回调函数 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 usr_echo_thread_entry(void* parameter) { struct rx_msg msg; rt_device_t device; rt_err_t result = RT_EOK; // 从RT系统中获取串口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); } while(1) { // 从消息队列中获取被回调函数放入消息队列中的内容 result = rt_mq_recv(&rx_mq, &msg, sizeof(struct rx_msg), 50); if (result == -RT_ETIMEOUT) { // timeout, do nothing } 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] = ' '; // 将内容写回到串口1 rt_device_write(device, 0, &uart_rx_buffer[0], rx_length); } } } // 串口例程初始化函数 void usr_echo_init() { rt_thread_t thread ; rt_err_t result; // 创建消息队列,分配队列存储空间 result = rt_mq_init(&rx_mq, "mqt", &msg_pool[0], 128 - sizeof(void*), sizeof(msg_pool), RT_IPC_FLAG_FIFO); if (result != RT_EOK) { rt_kprintf("init message queue failed. "); return; } // 创建任务线程 thread = rt_thread_create("devt", usr_echo_thread_entry, RT_NULL, 1024, 25, 7); // 启动任务线程 if (thread != RT_NULL) rt_thread_startup(thread); } ``` ``` void USART1_IRQHandler(void) { struct stm32_uart* uart; uart = &uart1; /* enter interrupt */ rt_interrupt_enter(); if(USART_GetITStatus(uart->uart_device, USART_IT_RXNE) != RESET) { rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND); /* clear interrupt */ USART_ClearITPendingBit(uart->uart_device, USART_IT_RXNE); } if (USART_GetITStatus(uart->uart_device, USART_IT_TC) != RESET) { /* clear interrupt */ USART_ClearITPendingBit(uart->uart_device, USART_IT_TC); } if (USART_GetFlagStatus(uart->uart_device, USART_FLAG_ORE) == SET) { stm32_getc(&serial1); } /* leave interrupt */ rt_interrupt_leave(); } ``` 是不是串口中断这里要改些什么啊 ``` int rt_application_init(void) { rt_thread_t init_thread; rt_err_t result; /* init led thread */ result = rt_thread_init(&led_thread, "led", led_thread_entry, RT_NULL, (rt_uint8_t*)&led_stack[0], sizeof(led_stack), 20, 5); if (result == RT_EOK) { rt_thread_startup(&led_thread); } // // //echo usr_echo_init(); ////动态线程 //#if (RT_THREAD_PRIORITY_MAX == 32) // init_thread = rt_thread_create("init", // rt_init_thread_entry, RT_NULL, // 2048, 8, 20); //#else // init_thread = rt_thread_create("init", // rt_init_thread_entry, RT_NULL, // 2048, 80, 20); //#endif // if (init_thread != RT_NULL) // rt_thread_startup(init_thread); return 0; } ```
查看更多
4
个回答
默认排序
按发布时间排序
筱l灬夏初eri
2016-02-17
这家伙很懒,什么也没写!
问了一下,弄好了,2.01版本的串口 open的时候需要新版本需要带额外参数,例如INT_RX, 我改成 ``` rt_device_open(device, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_RX)); ``` 就可以了
aozima
2016-02-17
调网络不抓包,调I2C等时序不上逻辑分析仪,就像电工不用万用表!多用整理的好的文字,比截图更省流量,还能在整理过程中思考。
如果没需要,请不要添加 RT_DEVICE_FLAG_STREAM 不然发送换行时,自动多加一个回车。
筱l灬夏初eri
2016-02-18
这家伙很懒,什么也没写!
恩,是的文档里面有写
撰写答案
登录
注册新账号
关注者
0
被浏览
2.3k
关于作者
筱l灬夏初eri
这家伙很懒,什么也没写!
提问
4
回答
10
被采纳
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
【RT-Thread】【ci】【scons】将ci.attachconfig.yml和scons结合使用
2
Rt-thread中OTA下载后,bootloader不搬程序
3
ulog 日志 LOG_HEX 输出时间改为本地日期时间
4
在RT-Thread Studio中构建前执行python命令
5
研究一了一段时间RTT,直接标准版上手太难,想用nano,但又舍不得组件
热门标签
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
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部