首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用termios -无法将奇偶校验设置为even,出现EINVAL错误

使用termios -无法将奇偶校验设置为even,出现EINVAL错误
EN

Stack Overflow用户
提问于 2021-09-23 08:44:03
回答 1查看 73关注 0票数 1

摘要:

我目前正在开发一个使用termios的嵌入式Linux程序。如果我没有启用偶数奇偶校验,我的程序工作正常。当我将PARENB设置为active而PARODD设置为inactive时,我开始遇到问题。我的程序被设计成8E1,115200波特率。

发生的情况是,当我第一次在启用偶数奇偶校验的情况下在新启动时运行程序时,它将传输一条消息。传输的消息确实会发送,但它不会发送奇偶校验位。第二次运行该程序时,tcsetattr函数使用errno == EINVAL失败。

我正在Raspberry Pi上运行此程序进行调试,然后该程序将被移植到yocto环境。

当前环境:

Raspberry pi: 4B Debian版本: 10.10 GCC:(Raspbian 8.3.0-6+rpi1) 8.3.0。

我的问题是:

有没有人知道遗漏了什么?我自己也尝试过这样做,并且尝试了我在GitHub上找到的两个人的在线例子。他们都有这个问题。

我运行的代码是:

代码语言:javascript
运行
AI代码解释
复制
// C library headers
#include <stdio.h>
#include <string.h>

// Linux headers
#include <errno.h>  // Error integer and strerror() function
#include <fcntl.h>  // Contains file controls like O_RDWR
#include <stdlib.h>
#include <sys/types.h>
#include <termios.h>  // Contains POSIX terminal control definitions
#include <unistd.h>   // write(), read(), close()

#define SERIAL_DEVICE "/dev/ttyS0"

int main() {
    struct termios serial_port_settings;
    int fd;
    int retval;
    char buf[] = "hello world \n bye\n";
    char ch;
    int i;

    //SERAIL_DEVICE is "/dev/ttyS0"
    fd = open(SERIAL_DEVICE, O_RDWR);
    if (fd < 0) {
        perror("Failed to open SERIAL_DEVICE");
        exit(1);
    }

    retval = tcgetattr(fd, &serial_port_settings);
    if (retval < 0) {
        perror("Failed to get termios structure");
        exit(2);
    }

    //setting baud rate to B115200
    retval = cfsetospeed(&serial_port_settings, B115200);
    if (retval < 0) {
        perror("Failed to set 115200 output baud rate");
        exit(3);
    }
    retval = cfsetispeed(&serial_port_settings, B115200);
    if (retval < 0) {
        perror("Failed to set 115200 input baud rate");
        exit(4);
    }

    //parity and bytes, grabbed from documentation
    serial_port_settings.c_cflag |= PARENB;   //enable parity
    serial_port_settings.c_cflag &= ~PARODD;  //disable odd parity, therefore-> even parity
    serial_port_settings.c_cflag &= ~CSTOPB; //disable 2 stop bits, therefore-> 1 stop bit
    serial_port_settings.c_cflag &= ~CSIZE;  //remove size
    serial_port_settings.c_cflag |= CS8;     //8 data bits
    serial_port_settings.c_cflag |= CLOCAL | CREAD;  //ignore modem control lines, enable reciever
    serial_port_settings.c_lflag |= ICANON; // enable canonical mode

    
    retval = tcsetattr(fd, TCSANOW, &serial_port_settings);
    /*************** ERROR HAPPENS HERE ********************/
    /* errno is set to EINVAL here */
    
    if (retval < 0) {
        perror("Failed to set serial attributes");
        exit(5);
    }
    printf("Successfully set the baud rate\n");

    retval = write(fd, buf, 18);
    if (retval < 0) {
        perror("write on SERIAL_DEVICE failed");
        exit(6);
    }

    retval = tcdrain(fd);
    if (retval < 0) {
        perror("write on SERIAL_DEVICE failed");
        exit(6);
    }

    close(fd);
    return 0;
}
EN

回答 1

Stack Overflow用户

发布于 2021-09-23 12:22:23

@sawdust和@KamilCuk帮助确定了我的问题。软件不是问题所在。看起来Raspberry Pi 4上的ttyS0不是一个全功能的COM端口。因此,尝试添加奇偶校验位将失败。

上面的代码在另一台具有全功能COM端口的Linux机器上运行良好。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69303945

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档