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

【智能家居】三、添加语音识别模块的串口读取功能点

语音识别模块SU-03T
串口通信线程控制代码

  • inputCommand.h(输入控制指令)
  • voiceControl.c(语音控制模块指令)
  • main.c(主函数)
  • 编译
  • 运行结果

语音识别模块SU-03T

AI智能语音识别模块离线语音控制模块语音识别芯片声控模块SU-03T
离线语音模组 SU-03T开发文档

在这里插入图片描述
在这里插入图片描述

串口通信线程控制代码

inputCommand.h(输入控制指令)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>      
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <wiringPi.h>
#include <wiringSerial.h>struct InputCommand
{char commandName[128];                              //“控制方式”名char deviceName[128];               				//“设备工厂”名char command[32];                                   //存放指令int fd;                                             //存放文件描述符(串口/网络)int s_fd;									        //存放服务器套接字描述符char port[12];								        //存放端口号char ipAdress[32];							        //存放 IP地址char log[1024];                                     //日志int (*Init)(struct InputCommand *voice);            //“初始化”函数指针int (*getCommand)(struct InputCommand *voice);      //“获取指令”函数指针struct InputCommand *next;
};struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead);		//“语音控制”加入指令链表函数声明

voiceControl.c(语音控制模块指令)

#include "inputCommand.h"// 语音控制模块初始化函数
int voiceInit(struct InputCommand *voice)
{int fd;// 打开串口设备 (voice->deviceName),波特率为 115200if ((fd = serialOpen (voice->deviceName, 115200)) < 0) { fprintf (stderr, "Unable to open serial device: %s\n", strerror (errno)) ; return 1 ; }voice->fd = fd; // 将文件描述符存储在 voice->fd 中return fd;
}// 从语音控制模块获取指令的函数 
int voiceGetCommand(struct InputCommand *voice)
{int nread = 0;// 从串口 (voice->fd) 读取数据到 voice->command 中nread = read(voice->fd, voice->command, sizeof(voice->command));//返回读取到数据的字节数,实际读取的指令放到了command里return nread;            
}// 全局变量,表示语音控制的输入命令对象 
struct InputCommand voiceControl = {.commandName = "voice",.deviceName = "/dev/ttyS5",.command = '\0',.Init = voiceInit,.getCommand = voiceGetCommand,.log = {'\0'},.next = NULL
};// 将语音控制对象加入到输入命令链表中的函数 
struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead)	//“语音控制”(对象)加入指令方式链表函数
{if (phead == NULL) {return &voiceControl;}else {voiceControl.next = phead;phead = &voiceControl;return phead; // 如果链表不为空,将语音控制对象插入到链表头,并返回链表头的指针}
}

main.c(主函数)

#include <pthread.h>
#include "controlDevice.h"
#include "inputCommand.h"// 定义指令工厂初始链表头
struct InputCommand *pcommandHead = NULL;// 查找指令对象 by 名称
struct InputCommand* findCommandByName(char *name, struct InputCommand *phead) 
{struct InputCommand *tmp = phead;if (phead == NULL) {return NULL;} else {while (tmp != NULL) {if (strcmp(tmp->commandName, name) == 0) {return tmp;}tmp = tmp->next;}return NULL;}
}// 语音控制线程执行函数
void *voiceControlThread(void *data) 
{int nread;struct InputCommand *voiceHandler = NULL;// 查找名为 "voice" 的指令处理对象voiceHandler = findCommandByName("voice", pcommandHead);if (voiceHandler == NULL) {printf("find voiceHandler error\n");pthread_exit(NULL);} else {// 初始化语音控制功能if (voiceHandler->Init(voiceHandler) < 0) {printf("voiceControl init error\n");pthread_exit(NULL);} else {printf("voiceControl init success\n");}while (1) {// 清空指令缓存memset(voiceHandler->command, '\0', sizeof(voiceHandler->command));// 从语音控制模块获取指令nread = voiceHandler->getCommand(voiceHandler);if (nread == 0) {// 串口没有获取到指令printf("No voiceCommand received\n");} else {// 获取到指令printf("Get VoiceCommand --> %s\n", voiceHandler->command);}}}
}int main() 
{if (wiringPiSetup() == -1) {fprintf(stdout, "Unable to start wiringPi: %s\n", strerror(errno));return 1;}pthread_t voiceControl_thread;// 指令工厂初始化,将语音控制对象加入到指令链表中pcommandHead = addVoiceControlToInputCommandLink(pcommandHead);// 创建语音控制线程pthread_create(&voiceControl_thread, NULL, voiceControlThread, NULL);// 主函数等待语音控制线程退出pthread_join(voiceControl_thread, NULL);return 0;
}
http://www.lryc.cn/news/253756.html

相关文章:

  • 物联网开发(一)新版Onenet 基础配置
  • qt/c/c++文件操作总结
  • 表示你的shell未被正确配置以使用conda activate--换成清华源anaconda
  • VT-MRPA1-151-1X/V0/0控制2FRE16模块式模拟放大器
  • 无需公网IP实现公网远程访问本地WebDAV服务
  • 远程服务器QEMU+Ubuntu+GRUB+VNC最佳实践
  • macbook电脑运行缓慢和卡顿内存怎么清理了?
  • 优化用户直播体验:第三方美颜SDK的前沿技术
  • UE4/UE5 材质实现带框环形进度条
  • Docker 环境中 Spring Boot 应用的 Arthas 故障排查与性能优化实战
  • Django 用户验证与权限管理
  • 二手物品交易系统源码小程序H5闲置物品转让APP成品
  • Linux库之动态库静态库
  • xilinx系列FPGA基于VIVADO的pin delay列表生成说明
  • 1.vue学习笔记(vue简介+API风格+开发前的准备)
  • 树莓派学习:学习opencv+用opencv获取树莓派mjpg摄像头视频流
  • 探索Playwright的现代自动化测试力量
  • 游戏mod制作--引擎与解包
  • 3D模型制作木质纹理贴图
  • [足式机器人]Part2 Dr. CAN学习笔记-数学基础Ch0-4线性时不变系统中的冲激响应与卷积
  • CSS BFC特性和应用
  • 软件工程 - 第8章 面向对象建模 - 3 - 动态建模
  • Stable Diffusion AI绘画系列【16】:霸气侧漏的二次元武侠风
  • 第二证券:苹果市值重返3万亿美元,关键因素并非人工智能
  • 西南科技大学C++程序设计实验六( 继承与派生一)
  • MySQL 性能优化
  • 求职招聘小程序源码系统 全开源源代码:找工作+招人才 平台级别运营版 附带完整的搭建教程
  • 26、卷积 - 实际上是一个特征提取器
  • web前端之vue3
  • 原来在C++的类中声明函数时可以不写参数名只写参数类型