44、串口接收程序,接收科大讯飞的语音唤醒模块数据

基本思想:需要一个串口接受程序,去接收语音唤醒模块的的唤醒数据,逐写了个demo

 

一、首先使用cutecom测试一下

二、使用串口代码接收数据测试一下 

cmakelist.txt

cmake_minimum_required(VERSION 3.16)
project(untitled6)

set(CMAKE_CXX_STANDARD 14)

add_executable(untitled6 main.cpp )
target_link_libraries(untitled6 )

main.cpp

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>
#include <string>
#include <iostream>

#define TRUE 1
#define FALSE 0

int analysis(char *buff);

int OpenDev(char *Dev);

void set_speed(int fd, int speed);

int set_Parity(int fd, int databits, int stopbits, int parity);

int speed_arr[] = {B115200, B38400, B19200, B9600, B4800, B2400, B1200, B300, B38400, B19200, B9600, B4800, B2400,
                   B1200, B300,};
int name_arr[] = {115200, 38400, 19200, 9600, 4800, 2400, 1200, 300, 38400, 19200, 9600, 4800, 2400, 1200, 300,};

int OpenDev(char *Dev) {
    int fd = open(Dev, O_RDWR | O_NOCTTY | O_NONBLOCK);
    if (-1 == fd) {
        perror("Can't Open Serial Port");
        return -1;
    } else {
        printf("Open com success!\n");
        return fd;
    }
}

void set_speed(int fd, int speed) {
    int i;
    int status;
    struct termios Opt;
    tcgetattr(fd, &Opt);
    for (i = 0; i < sizeof(speed_arr) / sizeof(int); i++) {
        if (speed == name_arr[i]) {
            tcflush(fd, TCIOFLUSH);
            cfsetispeed(&Opt, speed_arr[i]);
            cfsetospeed(&Opt, speed_arr[i]);
            status = tcsetattr(fd, TCSANOW, &Opt);
            if (status != 0) perror("tcsetattr fd1");
            return;
        }
        tcflush(fd, TCIOFLUSH);
    }
}

int set_Parity(int fd, int databits, int stopbits, int parity) {
    struct termios options;
    if (tcgetattr(fd, &options) != 0) {
        perror("SetupSerial 1");
        return (FALSE);
    }
    bzero(&options, sizeof(options));
    options.c_cflag |= CLOCAL | CREAD;
    options.c_cflag &= ~CSIZE;
    switch (databits) {
        case 7:
            options.c_cflag |= CS7;
            break;
        case 8:
            options.c_cflag |= CS8;
            break;
        default:
            fprintf(stderr, "Unsupported data size\n");
            return (FALSE);
    }
    switch (parity) {
        case 'n':
        case 'N':
            options.c_cflag &= ~PARENB;
            options.c_iflag &= ~INPCK;
            break;
        case 'o':
        case 'O':
            options.c_cflag |= (PARODD | PARENB);
            options.c_iflag |= (INPCK | ISTRIP);
            break;
        case 'e':
        case 'E':
            options.c_cflag |= PARENB;
            options.c_cflag &= ~PARODD;
            options.c_iflag |= (INPCK | ISTRIP);
            break;
        case 'S':
        case 's':
            options.c_cflag &= ~PARENB;
            options.c_cflag &= ~CSTOPB;
            break;
        default:
            fprintf(stderr, "Unsupported parity\n");
            return (FALSE);
    }
    switch (stopbits) {
        case 1:
            options.c_cflag &= ~CSTOPB;
            break;
        case 2:
            options.c_cflag |= CSTOPB;
            break;
        default:
            fprintf(stderr, "Unsupported stop bits\n");
            return (FALSE);
    }
    if (parity != 'n')
        options.c_iflag |= INPCK;
    options.c_cc[VTIME] = 0;
    options.c_cc[VMIN] = 0;
    tcflush(fd, TCIFLUSH);
    if (tcsetattr(fd, TCSANOW, &options) != 0) {
        perror("SetupSerial 3");
        return (FALSE);
    }
    return (TRUE);
}


int main() {
    int fd;
    int nread;
    char buff[1024] = {0};
    char *dev_name = "/dev/ttyACM0";//根据实际情况选择串口
    fd = OpenDev(dev_name); //打开串口
    if (fd > 0)
        set_speed(fd, 115200); //设置波特率
    else {
        printf("Can't Open Serial Port!\n");
        return -1;
    }

    if (set_Parity(fd, 8, 1, 'N') == FALSE) //设置校验位
    {
        printf("Set Parity Error\n");
        exit(1);
    }
    while (1) {
        sleep(1);
        memset(buff, 0, sizeof(buff));
        nread = read(fd, buff, sizeof(buff));
        if (nread < 0) {
            printf("error");
            break;
        }
        int start = 0;
        int end = 0;
        for (int i = 0; i < nread; i++) {
            if (buff[i] == '{') {
                start = i;
                break;
            }
        }

        for (int i = nread - 1; i >= 0; i--) {
            if (buff[i] == '}') {
                end = nread - i;
                break;
            }
        }
        if (start == 0 && end == 0)
            continue;
        std::string str;
        for (int i = start; i < nread - end + 1; i++) {
            if (buff[i] == '\\') continue;
            printf("%c", buff[i]);
            str.push_back(buff[i]);
        }
        printf("\n");
        std::cout << str << std::endl;
    }
    return 0;
}

测试结果

/home/ubuntu/untitled6/cmake-build-debug/untitled6
Open com success!
{"content":{"arg1":2,"arg2":0,"eventType":4,"info":"{"ivw":{"start_ms":15687380,"end_ms":15688030,"beam":2,"physical":2,"score":1112.0,"power":368928.1875,"angle":92.0,"keyword":"xiao3 wei1 xiao3 wei1"}}","result":"xiao3 wei1 xiao3 wei1"},"type":"aiui_event"}
{"content":{"arg1":2,"arg2":0,"eventType":4,"info":"{"ivw":{"start_ms":15687380,"end_ms":15688030,"beam":2,"physical":2,"score":1112.0,"power":368928.1875,"angle":92.0,"keyword":"xiao3 wei1 xiao3 wei1"}}","result":"xiao3 wei1 xiao3 wei1"},"type":"aiui_event"}
{"content":{"arg1":2,"arg2":0,"eventType":4,"info":"{"ivw":{"start_ms":15691250,"end_ms":15691880,"beam":2,"physical":2,"score":1199.0,"power":260333.390625,"angle":94.0,"keyword":"xiao3 wei1 xiao3 wei1"}}","result":"xiao3 wei1 xiao3 wei1"},"type":"aiui_event"}
{"content":{"arg1":2,"arg2":0,"eventType":4,"info":"{"ivw":{"start_ms":15691250,"end_ms":15691880,"beam":2,"physical":2,"score":1199.0,"power":260333.390625,"angle":94.0,"keyword":"xiao3 wei1 xiao3 wei1"}}","result":"xiao3 wei1 xiao3 wei1"},"type":"aiui_event"}

Process finished with exit code 15


版权声明:本文为sxj731533730原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。