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

语音识别模块SU-03T

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

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

串口通信线程控制代码

inputCommand.h(输入控制指令)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h&gt;
#include <unistd.h&gt;
#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: %sn", 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 = '',
    .Init = voiceInit,
    .getCommand = voiceGetCommand,
    .log = {''},
    .next = NULL
};

// 将语音控制对象加入到输入命令链表中的函数 
struct InputCommand* addVoiceControlToInputCommandLink(struct InputCommand *phead)	//“语音控制”(对象加入指令方式链表函数
{
	if (phead == NULL) {
		return &amp;voiceControl;
	}
	else {
		voiceControl.next = phead;
		phead = &amp;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 errorn");
        pthread_exit(NULL);
    } 
    else {
        // 初始化语音控制功能
        if (voiceHandler->Init(voiceHandler) < 0) {
            printf("voiceControl init errorn");
            pthread_exit(NULL);
        } 
        else {
            printf("voiceControl init successn");
        }

        while (1) {
            // 清空指令缓存
            memset(voiceHandler->command, '', sizeof(voiceHandler->command));
            // 从语音控制模块获取指令
            nread = voiceHandler->getCommand(voiceHandler);

            if (nread == 0) {
                // 串口没有获取到指令
                printf("No voiceCommand receivedn");
            } 
            else {
                // 获取到指令
                printf("Get VoiceCommand --> %sn", voiceHandler->command);
            }
        }
    }
}

int main() 
{
    if (wiringPiSetup() == -1) {
        fprintf(stdout, "Unable to start wiringPi: %sn", strerror(errno));
        return 1;
    }

    pthread_t voiceControl_thread;

    // 指令工厂初始化,将语音控制对象加入到指令链表
    pcommandHead = addVoiceControlToInputCommandLink(pcommandHead);

    // 创建语音控制线程
    pthread_create(&amp;voiceControl_thread, NULL, voiceControlThread, NULL);

    // 主函数等待语音控制线程退出
    pthread_join(voiceControl_thread, NULL);

    return 0;
}

原文地址:https://blog.csdn.net/m0_62140641/article/details/134707098

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_29352.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注