Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
rt_mq_消息队列_msg_queue
为啥我这消息队列收不到消息啊?
发布于 2024-09-14 11:44:20 浏览:231
订阅该版
```c #include
#include
/*消息队列控制块*/ static struct rt_messagequeue mq; /*消息队列中用到的放置消息的内存池*/ static rt_size_t msg_pool[4096]; struct rt_thread thread1; struct rt_thread thread2; static char thread1_stack[1024]; static char thread2_stack[1024]; /*线程一入口函数*/ static void thread1_entry(void *parameter) { char buf = 0; rt_uint8_t cnt = 0; while(1) { /*从消息队列中接收消息*/ rt_err_t result = rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER); if(result == RT_EOK) { rt_kprintf("thread1 rece message : %c\n", buf); if(cnt == 19) { break; } }else { rt_kprintf("thread1 Receive failed: %d\n", result); return; } cnt++; rt_thread_mdelay(50); } rt_kprintf("thread1 detach mq\n"); rt_mq_delete(&mq); } static void thread2_entry(void *parameter) { int result; char buf = 'A'; rt_uint8_t cnt = 0; while(1) { if(cnt == 8) { /*如果count = 8,发送紧急消息到队列中*/ result = rt_mq_urgent(&mq, &buf, 1); if(result != RT_EOK) { rt_kprintf("rt_mq_urgent is fail!\n"); } else { rt_kprintf("thread2 send urgent message - %c\n", buf); } } else if(cnt >= 20) { rt_kprintf("message queue stop send, thread2 quit\n"); break; } else { /*发送消息到队列中*/ result = rt_mq_send(&mq, &buf, 1); if(result != RT_EOK) { rt_kprintf("rt_mq_urgent is fail!\n"); } else { rt_kprintf("thread2 send message - %c\n", buf); } } buf++; cnt++; rt_thread_mdelay(5); } } /*消息队列的初始化*/ int msgq_sample() { rt_err_t result; int ret = 0; /*初始化消息队列*/ result = rt_mq_init(&mq, "mqt", &msg_pool[0], 1, sizeof(msg_pool), RT_IPC_FLAG_FIFO); if(result != RT_EOK) { rt_kprintf("rt_mq_init is fail!\n"); return -1; } /*初始化两个线程*/ ret = rt_thread_init(&thread1, "thread1", thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), 11, 5); if(ret != RT_EOK) { rt_kprintf("rt_thread1_init is fail!\n"); return ret; } ret = rt_thread_init(&thread2, "thread2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), 10, 5); if(ret != RT_EOK) { rt_kprintf("rt_thread2_init is fail!\n"); return ret; } rt_thread_startup(&thread1); rt_thread_startup(&thread2); return 0; } ```
查看更多
2
个回答
默认排序
按发布时间排序
张世争
2024-09-15
学以致用
问题就是 `rt_mq_recv` 返回值问题,RT-Thread 新版本 `rt_mq_recv` 返回接收到的消息队列的长度,而不是 错误码,修改一下即可 ```c #include
#include
/*消息队列控制块*/ static struct rt_messagequeue mq; /*消息队列中用到的放置消息的内存池*/ static rt_size_t msg_pool[4096]; struct rt_thread thread1; struct rt_thread thread2; static char thread1_stack[1024]; static char thread2_stack[1024]; /*线程一入口函数*/ static void thread1_entry(void *parameter) { char buf = 0; rt_uint8_t cnt = 0; rt_ssize_t recv_len = 0; while (1) { /*从消息队列中接收消息*/ recv_len = rt_mq_recv(&mq, &buf, sizeof(buf), RT_WAITING_FOREVER); if (recv_len == sizeof(buf)) { rt_kprintf("thread1 rece message : %c\n", buf); if (cnt == 19) { break; } } else { rt_kprintf("thread1 Receive failed: %d\n", recv_len); return; } cnt++; rt_thread_mdelay(50); } rt_kprintf("thread1 detach mq\n"); rt_mq_detach(&mq); } static void thread2_entry(void *parameter) { int result; char buf = 'A'; rt_uint8_t cnt = 0; while (1) { if (cnt == 8) { /*如果count = 8,发送紧急消息到队列中*/ result = rt_mq_urgent(&mq, &buf, 1); if (result != RT_EOK) { rt_kprintf("rt_mq_urgent is fail!\n"); } else { rt_kprintf("thread2 send urgent message - %c\n", buf); } } else if (cnt >= 20) { rt_kprintf("message queue stop send, thread2 quit\n"); break; } else { /*发送消息到队列中*/ result = rt_mq_send(&mq, &buf, 1); if (result != RT_EOK) { rt_kprintf("rt_mq_urgent is fail!\n"); } else { rt_kprintf("thread2 send message - %c\n", buf); } } buf++; cnt++; rt_thread_mdelay(5); } } /*消息队列的初始化*/ int msgq_sample(void) { rt_err_t result; int ret = 0; /*初始化消息队列*/ result = rt_mq_init(&mq, "mqt", &msg_pool[0], 1, sizeof(msg_pool), RT_IPC_FLAG_FIFO); if (result != RT_EOK) { rt_kprintf("rt_mq_init is fail!\n"); return -1; } /*初始化两个线程*/ ret = rt_thread_init(&thread1, "thread1", thread1_entry, RT_NULL, &thread1_stack[0], sizeof(thread1_stack), 11, 5); if (ret != RT_EOK) { rt_kprintf("rt_thread1_init is fail!\n"); return ret; } ret = rt_thread_init(&thread2, "thread2", thread2_entry, RT_NULL, &thread2_stack[0], sizeof(thread2_stack), 10, 5); if (ret != RT_EOK) { rt_kprintf("rt_thread2_init is fail!\n"); return ret; } rt_thread_startup(&thread1); rt_thread_startup(&thread2); return 0; } MSH_CMD_EXPORT(msgq_sample, msgq_sample); ``` - 测试效果 ```c msh >msgq_sample thread2 send message - A thread1 rece message : A msh >thread2 send message - B thread2 send message - C thread2 send message - D thread2 send message - E thread2 send message - F thread2 send message - G thread2 send message - H thread1 rece message :thread2 send urgent message - I ssathread2 send message - J thread2 send message - K thread2 send message - L thread2 send message - M thread2 send message - N thread2 send message - O thread2 send message - P thread1 rece message : I thread2 send message - Q thread2 send message - R thread2 send message - S thread2 send message - T message queue stop send, thread2 quit thread1 rece message : C thread1 rece message : D thread1 rece message : E thread1 rece message : F thread1 rece message : G thread1 rece message : H thread1 rece message : J thread1 rece message : K thread1 rece message : L thread1 rece message : M thread1 rece message : N thread1 rece message : O thread1 rece message : P thread1 rece message : Q thread1 rece message : R thread1 rece message : S thread1 rece message : T thread1 detach mq ```
xddl00
2024-09-18
这家伙很懒,什么也没写!
这个不好,来回改API接口
撰写答案
登录
注册新账号
关注者
0
被浏览
231
关于作者
bruna
这家伙很懒,什么也没写!
提问
1
回答
0
被采纳
0
关注TA
发私信
相关问题
1
rt_object_init中报assertion failed错误?
2
在 MDK中的NANO 里创建消息队列失败,内存堆已开启
3
如何用消息队列传递结构体数据
4
消息队列满了以后接收乱码
5
消息队列传输不定长数据
6
使用消息队列在线程中发送总失败
7
初始化第二个消息队列时发生硬件错误
8
rtthread消息队列一对多的情况
9
消息队列为什么会出现获取到的内容有旧的数据?
10
通过消息队列名称,获取消息队列句柄
推荐文章
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
ota在线升级
UART
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
次被采纳
本月文章贡献
程序员阿伟
6
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
2
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部