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

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

语音识别模块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),波特率为 115200
    if ((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.kler.cn/news/160860.html

相关文章:

  • Java语言中的修饰符
  • 统计centos系统哪一个进程打开文件描述符
  • CSS 在性能优化方面的实践
  • 使用pytorch从零开始实现迷你GPT
  • R语言手册30分钟上手
  • Javase | 贪吃蛇小游戏
  • L1-025:正整数A+B
  • python筛选并删除两个文件夹中不同文件名的文件
  • 2-redis高级-centos上安装redis(编译安装、redis启动)、redis客户端操作、redis使用场景、redis中的通用命令
  • Google Guava 集合工具使用详解
  • Node.js快速搭建简单的HTTP服务器并发布公网远程访问
  • 多向通信----多人聊天
  • Elaticsearch 学习笔记
  • 量子纠缠通讯:未来信息技术的革新者
  • AI算力研究报告:智算供给格局分化国产化进程有望加速
  • 【华为网络-配置-025】- 同 VLAN 下不同网段通信(启用 Sub 地址)
  • 圆通单号查询,圆通速递物流查询,对需要的单号进行颜色标记
  • 你了解架构图吗?
  • pytorch中的transpose用法
  • Verilog开源项目——百兆以太网交换机(四)令牌桶管理单元设计
  • CSS-2
  • php中WebSocket简单使用
  • C++——红黑树
  • uniapp实战 —— 自定义顶部导航栏
  • GPT 中文提示词技巧:参照 OpenAI 官方教程
  • mysql知识分享(包含安装卸载)(一)
  • Vue + Element 实现按钮指定间隔时间点击
  • [MTK]安卓8 ADB执行ota升级
  • 文心一言 VS 讯飞星火 VS chatgpt (151)-- 算法导论12.2 7题
  • JPA(Java Persistence API)是什么