树莓派和语音模块实现语音识别功能
树莓派与语音模块接线图
语音模块
修改LDchip.c
找到LD_AsrAddFixed函数
功能描述:向LD模块添加关键词
修改main.c
找到User_handle函数
识别出结果后,把数据从语音模块发出来
树莓派串口配置
初次使用树莓派串口编程,需要对串口进行配置
/* 修改 cmdline.txt文件 */
>cd /boot/
>sudo vim cmdline.txt
删除【】之间的部分
dwc_otg.lpm_enable=0 【console=ttyAMA0,115200】 kgdboc=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait
语音识别功能实现代码
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <wiringPi.h>
#include <wiringSerial.h>
int main()
{
int fd;
char cmd[128] = {'\0'};
int nread;
//使用wiringpi引脚就必须初始化设备
if(wiringPiSetup() == -1)
{
printf("初始化失败!\n");
return -1;
}
//打开串口设备文件
fd = serialOpen("/dev/ttyAMA0",9600);
//树莓派接收读取语音发来的数据
while(1){
//int serialGetchar (int fd)
nread = read(fd,cmd,sizeof(cmd));//cmd为树莓派接收到的指令
printf("getdata=%dbyte over,please say\n",nread);
if(strlen(cmd) == 0)
{
printf("overtime\n");
continue;
}
if(strstr(cmd,"Open") != NULL)//strstr:若str2不是str1的字串,返回NULL
{
printf("Open Light success\n");
}
if(strstr(cmd,"Close") != NULL)
{
printf("Close Light success\n");
}
//清空数据
memset(cmd,'\0',sizeof(cmd)/sizeof(char));
}
return 0;
}