当前位置: 首页 > news >正文

Hi3861 OpenHarmony嵌入式应用入门--LiteOS MessageQueue

CMSIS 2.0接口中的消息(Message)功能主要涉及到实时操作系统(RTOS)中的线程间通信。在CMSIS 2.0标准中,消息通常是通过消息队列(MessageQueue)来进行处理的,以实现不同线程之间的信息交换。

注意事项

在使用CMSIS 2.0的消息队列API时,需要确保在RTOS内核启动之前(即在调用osKernelStart()之前)创建并初始化消息队列。

发送和接收消息时,需要确保传递的指针和缓冲区是有效的,并且大小与创建消息队列时指定的参数相匹配。

如果在超时时间内无法发送或接收消息,函数将返回相应的错误状态。开发人员需要根据这些状态来处理可能的错误情况。

MessageQueue API

API名称

说明

osMessageQueueNew

创建和初始化一个消息队列

osMessageQueueGetName

返回指定的消息队列的名字

osMessageQueuePut

向指定的消息队列存放1条消息,如果消息队列满了,那么返回超时

osMessageQueueGet

从指定的消息队列中取得1条消息,如果消息队列为空,那么返回超时

osMessageQueueGetCapacity

获得指定的消息队列的消息容量

osMessageQueueGetMsgSize

获得指定的消息队列中可以存放的最大消息的大小

osMessageQueueGetCount

获得指定的消息队列中当前的消息数

osMessageQueueGetSpace

获得指定的消息队列中还可以存放的消息数

osMessageQueueReset

将指定的消息队列重置为初始状态

osMessageQueueDelete

删除指定的消息队列

代码编写

修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. import("//build/lite/config/component/lite_component.gni")lite_component("demo") {features = [#"base_00_helloworld:base_helloworld_example",#"base_01_led:base_led_example",#"base_02_loopkey:base_loopkey_example",#"base_03_irqkey:base_irqkey_example",#"base_04_adc:base_adc_example",#"base_05_pwm:base_pwm_example",#"base_06_ssd1306:base_ssd1306_example",#"kernel_01_task:kernel_task_example",#"kernel_02_timer:kernel_timer_example",#"kernel_03_event:kernel_event_example",#"kernel_04_mutex:kernel_mutex_example",#"kernel_05_semaphore_as_mutex:kernel_semaphore_as_mutex_example",#"kernel_06_semaphore_for_sync:kernel_semaphore_for_sync_example",#"kernel_07_semaphore_for_count:kernel_semaphore_for_count_example","kernel_08_message_queue:kernel_message_queue_example",]
}

创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_08_message_queue文件夹

文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_08_message_queue\kernel_message_queue_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\kernel_08_message_queue\BUILD.gn文件

# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#    http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. static_library("kernel_message_queue_example") {sources = ["kernel_message_queue_example.c"]include_dirs = ["//utils/native/lite/include","//kernel/liteos_m/kal/cmsis",]
}
/** Copyright (C) 2023 HiHope Open Source Organization .* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at**     http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/#include <stdio.h>
#include <unistd.h>#include "ohos_init.h"
#include "cmsis_os2.h"#define STACK_SIZE   (1024)
#define DELAY_TICKS_3   (3)
#define DELAY_TICKS_5   (5)
#define DELAY_TICKS_20 (20)
#define DELAY_TICKS_80 (80)#define QUEUE_SIZE 3
typedef struct {osThreadId_t tid;int count;
} message_entry;
osMessageQueueId_t qid;void sender_thread(void)
{// 定义一个静态变量count,用于记录发送的消息的次数static int count = 0;// 定义一个message_entry类型的变量sentrymessage_entry sentry;// 无限循环while (1) {// 将当前线程的ID赋值给sentry的tidsentry.tid = osThreadGetId();// 将count的值赋值给sentry的countsentry.count = count;// 打印当前线程的名字和count的值printf("[Message Test] %s send %d to message queue.\r\n",osThreadGetName(osThreadGetId()), count);// 将sentry放入消息队列中osMessageQueuePut(qid, (const void *)&sentry, 0, osWaitForever);// count加1count++;// 延时5个节拍osDelay(DELAY_TICKS_5);}
}void receiver_thread(void)
{// 定义一个消息队列rentrymessage_entry rentry;// 无限循环while (1) {// 从队列qid中获取消息osMessageQueueGet(qid, (void *)&rentry, NULL, osWaitForever);// 打印当前线程名称、接收到的消息计数和发送线程名称printf("[Message Test] %s get %d from %s by message queue.\r\n",osThreadGetName(osThreadGetId()), rentry.count, osThreadGetName(rentry.tid));// 延时3个时钟周期osDelay(DELAY_TICKS_3);}
}osThreadId_t newThread(char *name, osThreadFunc_t func, char *arg)
{osThreadAttr_t attr = {name, 0, NULL, 0, NULL, STACK_SIZE*2, osPriorityNormal, 0, 0};osThreadId_t tid = osThreadNew(func, (void *)arg, &attr);if (tid == NULL) {printf("[Message Test] osThreadNew(%s) failed.\r\n", name);} else {printf("[Message Test] osThreadNew(%s) success, thread id: %d.\r\n", name, tid);}return tid;
}void rtosv2_msgq_main(void)
{qid = osMessageQueueNew(QUEUE_SIZE, sizeof(message_entry), NULL);osThreadId_t ctid1 = newThread("recevier1", receiver_thread, NULL);osThreadId_t ctid2 = newThread("recevier2", receiver_thread, NULL);osThreadId_t ptid1 = newThread("sender1", sender_thread, NULL);osThreadId_t ptid2 = newThread("sender2", sender_thread, NULL);osThreadId_t ptid3 = newThread("sender3", sender_thread, NULL);osDelay(DELAY_TICKS_20);uint32_t cap = osMessageQueueGetCapacity(qid);printf("[Message Test] osMessageQueueGetCapacity, capacity: %u.\r\n", cap);uint32_t msg_size =  osMessageQueueGetMsgSize(qid);printf("[Message Test] osMessageQueueGetMsgSize, size: %u.\r\n", msg_size);uint32_t count = osMessageQueueGetCount(qid);printf("[Message Test] osMessageQueueGetCount, count: %u.\r\n", count);uint32_t space = osMessageQueueGetSpace(qid);printf("[Message Test] osMessageQueueGetSpace, space: %u.\r\n", space);osDelay(DELAY_TICKS_80);osThreadTerminate(ctid1);osThreadTerminate(ctid2);osThreadTerminate(ptid1);osThreadTerminate(ptid2);osThreadTerminate(ptid3);osMessageQueueDelete(qid);
}static void MessageTestTask(void)
{osThreadAttr_t attr;attr.name = "rtosv2_msgq_main";attr.attr_bits = 0U;attr.cb_mem = NULL;attr.cb_size = 0U;attr.stack_mem = NULL;attr.stack_size = STACK_SIZE;attr.priority = osPriorityNormal;if (osThreadNew((osThreadFunc_t)rtosv2_msgq_main, NULL, &attr) == NULL) {printf("[MessageTestTask] Falied to create rtosv2_msgq_main!\n");}
}
APP_FEATURE_INIT(MessageTestTask);

使用build,编译成功后,使用upload进行烧录。

相关文章:

Hi3861 OpenHarmony嵌入式应用入门--LiteOS MessageQueue

CMSIS 2.0接口中的消息&#xff08;Message&#xff09;功能主要涉及到实时操作系统&#xff08;RTOS&#xff09;中的线程间通信。在CMSIS 2.0标准中&#xff0c;消息通常是通过消息队列&#xff08;MessageQueue&#xff09;来进行处理的&#xff0c;以实现不同线程之间的信息…...

ffmpeg编码图象时报错Invalid buffer size, packet size * < expected frame_size *

使用ffmpeg将单个yuv文件编码转为jpg或其他图像格式时&#xff0c;报错&#xff1a; Truncating packet of size 11985408 to 3585 [rawvideo 0x1bd5390] Packet corrupt (stream 0, dts 1). image_3264_2448_0.yuv: corrupt input packet in stream 0 [rawvideo 0x1bd7c60…...

解决类重复的问题

1.针对AndroidX 类重复问题 解决办法&#xff1a; android.useAndroidXtrue android.enableJetifiertrue2.引用其他sdk出现类重复的问题解决办法&#xff1a;configurations {all { // You should exclude one of them not both of themexclude group: "com.enmoli"…...

使用 shell 脚本 统计app冷启动耗时

下面是一个 shell 脚本&#xff0c;它使用 参数将包名称作为参数--app&#xff0c;识别相应应用程序进程的 PID&#xff0c;使用 终止该进程adb shell kill&#xff0c;最后使用 重新启动该应用程序adb shell am start&#xff1a; #!/bin/bash# Check if package name is pro…...

使用容器部署redis_设置配置文件映射到本地_设置存储数据映射到本地_并开发java应用_连接redis---分布式云原生部署架构搭建011

可以看到java应用的部署过程,首先我们要准备一个java应用,并且我们,用docker,安装一个redis 首先我们去start.spring.io 去生成一个简单的web项目,然后用idea打开 选择以后下载 放在这里,然后我们去安装redis 在公共仓库中找到redis . 可以看到它里面介绍说把数据放到了/dat…...

第五节:如何使用其他注解方式从IOC中获取bean(自学Spring boot 3.x的第一天)

大家好&#xff0c;我是网创有方&#xff0c;上节我们实践了通过Bean方式声明Bean配置。咱们这节通过Component和ComponentScan方式实现一个同样功能。这节实现的效果是从IOC中加载Bean对象&#xff0c;并且将Bean的属性打印到控制台。 第一步&#xff1a;创建pojo实体类studen…...

Paragon NTFS与Tuxera NTFS有何区别 Mac NTFS 磁盘读写工具选哪个好

macOS系统虽然以稳定、安全系数高等优点著称&#xff0c;但因其封闭性&#xff0c;不能对NTFS格式磁盘写入数据常被人们诟病。优质的解决方案是使用磁盘管理软件Paragon NTFS for Mac&#xff08;点击获取激活码&#xff09;和Tuxera NTFS&#xff08;点击获取激活码&#xff0…...

EtherCAT主站IGH-- 2 -- IGH之coe_emerg_ring.h/c文件解析

EtherCAT主站IGH-- 2 -- IGH之coe_emerg_ring.h/c文件解析 0 预览一 该文件功能coe_emerg_ring.c 文件功能函数预览 二 函数功能介绍coe_emerg_ring.c 中主要函数的作用1. ec_coe_emerg_ring_init2. ec_coe_emerg_ring_clear3. ec_coe_emerg_ring_size4. ec_coe_emerg_ring_pus…...

psensor 的手势功能

psensor 的手势功能的移植过程 有时间再来写下...

使用 nvm 管理 Node 版本及 pnpm 安装

文章目录 GithubWindows 环境Mac/Linux 使用脚本进行安装或更新Mac/Linux 环境变量nvm 常用命令npm 常用命令npm 安装 pnpmNode 历史版本 Github https://github.com/nvm-sh/nvm Windows 环境 https://nvm.uihtm.com/nvm.html Mac/Linux 使用脚本进行安装或更新 curl -o- …...

uni-appx使用form表单页面初始化报错

因为UniFormSubmitEvent的类型时 e-->detail-->value,然后没有了具体值。所以页面初始化的时候 不能直接从value取值&#xff0c;会报错找不到 所以form表单里的数据我们要设置成一个对象来存放 这个问题的关键在于第22行代码 取值&#xff1a; 不能按照点的方式取值 …...

TiDB-从0到1-数据导出导入

TiDB从0到1系列 TiDB-从0到1-体系结构TiDB-从0到1-分布式存储TiDB-从0到1-分布式事务TiDB-从0到1-MVCCTiDB-从0到1-部署篇TiDB-从0到1-配置篇TiDB-从0到1-集群扩缩容 一、数据导出 TiDB中通过Dumpling来实现数据导出&#xff0c;与MySQL中的mysqldump类似&#xff0c;其属于…...

动手学深度学习(Pytorch版)代码实践 -卷积神经网络-16自定义层

16自定义层 import torch import torch.nn.functional as F from torch import nnclass CenteredLayer(nn.Module):def __init__(self):super().__init__()#从其输入中减去均值#X.mean() 计算的是整个张量的均值#希望计算特定维度上的均值&#xff0c;可以传递 dim 参数。#例如…...

树莓派4设置

使用sudo命令时要求输入密码 以 sudo 为前缀的命令以超级用户身份运行。默认情况下&#xff0c;超级用户不需要密码。不过&#xff0c;您可以要求所有以 sudo 运行的命令都输入密码&#xff0c;从而提高 Raspberry Pi 的安全性。 要强制 sudo 要求输入密码&#xff0c;请为你…...

44.商城系统(二十五):k8s基本操作,ingress域名访问,kubeSphere可视化安装

上一章我们已经配置好了k8s集群,如果没有配置好先去照着上面的配。 一、k8s入门操作 1.部署一个tomcat,测试容灾恢复 #在主机器上执行 kubectl create deployment tomcat6 --image=tomcat:6.0.53-jre8#查看k8s中的所有资源 kubectl get all kubectl get all -o wide#查看po…...

MySQL高级查询

MySQL 前言 文本源自微博客 (www.microblog.store),且已获授权. 一. mysql基础知识 1. mysql常用系统命令 启动命令 net start mysql停止命令 net stop mysql登录命令 mysql -h ip -P 端口 -u 用户名 -p ​ 本机可以省略 ip mysql -u 用户名 -p 查看数据库版本 mysql --ve…...

聊聊啥项目适合做自动化测试

作为测试从业者&#xff0c;你是否遇到过这样的场景&#xff0c;某天公司大Boss找你谈话。 老板&#xff1a;小李&#xff0c;最近工作辛苦了 小李&#xff1a;常感谢您的认可&#xff0c;这不仅是对我个人的鼓励&#xff0c;更是对我们整个团队努力的认可。我们的成果离不开每…...

ROS2开发机器人移动

.创建功能包和节点 这里我们设计两个节点 example_interfaces_robot_01&#xff0c;机器人节点&#xff0c;对外提供控制机器人移动服务并发布机器人的状态。 example_interfaces_control_01&#xff0c;控制节点&#xff0c;发送机器人移动请求&#xff0c;订阅机器人状态话题…...

【强化学习】第02期:动态规划方法

笔者近期上了国科大周晓飞老师《强化学习及其应用》课程&#xff0c;计划整理一个强化学习系列笔记。笔记中所引用的内容部分出自周老师的课程PPT。笔记中如有不到之处&#xff0c;敬请批评指正。 文章目录 2.1 动态规划&#xff1a;策略收敛法/策略迭代法2.2 动态规划&#xf…...

安全技术和防火墙(二)

接上一节 备份和还原 iptables-save > /opt/iptables.bak iptables-restore < /opt/iptables.bak snat和dnat snat源地址转换 内网到外网 内网ip转换成可以访问外网的ip 内网的多个主机可以只有一个有效的公网ip地址访问外部网络 dnat 目的地址转发 外部用户&#…...

挑战杯推荐项目

“人工智能”创意赛 - 智能艺术创作助手&#xff1a;借助大模型技术&#xff0c;开发能根据用户输入的主题、风格等要求&#xff0c;生成绘画、音乐、文学作品等多种形式艺术创作灵感或初稿的应用&#xff0c;帮助艺术家和创意爱好者激发创意、提高创作效率。 ​ - 个性化梦境…...

业务系统对接大模型的基础方案:架构设计与关键步骤

业务系统对接大模型&#xff1a;架构设计与关键步骤 在当今数字化转型的浪潮中&#xff0c;大语言模型&#xff08;LLM&#xff09;已成为企业提升业务效率和创新能力的关键技术之一。将大模型集成到业务系统中&#xff0c;不仅可以优化用户体验&#xff0c;还能为业务决策提供…...

linux之kylin系统nginx的安装

一、nginx的作用 1.可做高性能的web服务器 直接处理静态资源&#xff08;HTML/CSS/图片等&#xff09;&#xff0c;响应速度远超传统服务器类似apache支持高并发连接 2.反向代理服务器 隐藏后端服务器IP地址&#xff0c;提高安全性 3.负载均衡服务器 支持多种策略分发流量…...

Appium+python自动化(十六)- ADB命令

简介 Android 调试桥(adb)是多种用途的工具&#xff0c;该工具可以帮助你你管理设备或模拟器 的状态。 adb ( Android Debug Bridge)是一个通用命令行工具&#xff0c;其允许您与模拟器实例或连接的 Android 设备进行通信。它可为各种设备操作提供便利&#xff0c;如安装和调试…...

基于uniapp+WebSocket实现聊天对话、消息监听、消息推送、聊天室等功能,多端兼容

基于 ​UniApp + WebSocket​实现多端兼容的实时通讯系统,涵盖WebSocket连接建立、消息收发机制、多端兼容性配置、消息实时监听等功能,适配​微信小程序、H5、Android、iOS等终端 目录 技术选型分析WebSocket协议优势UniApp跨平台特性WebSocket 基础实现连接管理消息收发连接…...

Qwen3-Embedding-0.6B深度解析:多语言语义检索的轻量级利器

第一章 引言&#xff1a;语义表示的新时代挑战与Qwen3的破局之路 1.1 文本嵌入的核心价值与技术演进 在人工智能领域&#xff0c;文本嵌入技术如同连接自然语言与机器理解的“神经突触”——它将人类语言转化为计算机可计算的语义向量&#xff0c;支撑着搜索引擎、推荐系统、…...

现有的 Redis 分布式锁库(如 Redisson)提供了哪些便利?

现有的 Redis 分布式锁库&#xff08;如 Redisson&#xff09;相比于开发者自己基于 Redis 命令&#xff08;如 SETNX, EXPIRE, DEL&#xff09;手动实现分布式锁&#xff0c;提供了巨大的便利性和健壮性。主要体现在以下几个方面&#xff1a; 原子性保证 (Atomicity)&#xff…...

Java求职者面试指南:计算机基础与源码原理深度解析

Java求职者面试指南&#xff1a;计算机基础与源码原理深度解析 第一轮提问&#xff1a;基础概念问题 1. 请解释什么是进程和线程的区别&#xff1f; 面试官&#xff1a;进程是程序的一次执行过程&#xff0c;是系统进行资源分配和调度的基本单位&#xff1b;而线程是进程中的…...

论文阅读:LLM4Drive: A Survey of Large Language Models for Autonomous Driving

地址&#xff1a;LLM4Drive: A Survey of Large Language Models for Autonomous Driving 摘要翻译 自动驾驶技术作为推动交通和城市出行变革的催化剂&#xff0c;正从基于规则的系统向数据驱动策略转变。传统的模块化系统受限于级联模块间的累积误差和缺乏灵活性的预设规则。…...

MyBatis中关于缓存的理解

MyBatis缓存 MyBatis系统当中默认定义两级缓存&#xff1a;一级缓存、二级缓存 默认情况下&#xff0c;只有一级缓存开启&#xff08;sqlSession级别的缓存&#xff09;二级缓存需要手动开启配置&#xff0c;需要局域namespace级别的缓存 一级缓存&#xff08;本地缓存&#…...