C 中的串行端口 read/open 错误,Linux

Serial port read/open error in C, Linux

我无法为要打开的串口选择正确的设置。 我掌握的信息如下:

我有的是这样的:

bool configurePort(void)   {
    struct termios port_settings;
    bzero(&port_settings, sizeof(port_settings));

    tcgetattr(fd, &port_settings);

    cfsetispeed(&port_settings, B9600);
    cfsetospeed(&port_settings, B9600);

    port_settings.c_cflag &= ~CSIZE;
    port_settings.c_cflag |= CS8;

    // parity bit
    //port_settings.c_cflag &= ~PARENB;
    //port_settings.c_cflag &= ~PARODD;
    // hardware flow
    port_settings.c_cflag &= ~CRTSCTS;
    // stop bit
    //port_settings.c_cflag &= ~CSTOPB;

    port_settings.c_iflag = IGNBRK;
    port_settings.c_iflag &= ~(IXON | IXOFF | IXANY);
    port_settings.c_lflag = 0;
    port_settings.c_oflag = 0;

    port_settings.c_cc[VMIN] = 1;   
    port_settings.c_cc[VTIME] = 0;
    port_settings.c_cc[VEOF] = 4;   

    tcsetattr(fd, TCSANOW, &port_settings);


    return true;
}

尝试了各种修改,但似乎没有任何效果。

设备通过 USB 串口 (ttyUSB0) 连接,我有权限。 它打开设备,发送(?)数据但永远不会得到任何回...

谁能告诉我应该怎么做?

试试这个:

bool configurePort(void)   {
    struct termios port_settings;
    bzero(&port_settings, sizeof(port_settings));

    if(tcgetattr(fd, &port_settings) < 0) {
        perror("tcgetattr");
        return false;
    }

    cfmakeraw(&port_settings);

    cfsetispeed(&port_settings, B9600);
    cfsetospeed(&port_settings, B9600);

    //input
    port_settings.c_iflag &= ~(IXON | IXOFF | IXANY); //disable flow control

    //local
    port_settings.c_lflag = 0;  // No local flags

    //output
    port_settings.c_oflag |= ONLRET;
    port_settings.c_oflag |= ONOCR;
    port_settings.c_oflag &= ~OPOST;


    port_settings.c_cflag &= ~CRTSCTS; // Disable RTS/CTS    
    port_settings.c_cflag |= CREAD; // Enable receiver
    port_settings.c_cflag &= ~CSTOPB;


    tcflush(fd, TCIFLUSH);


    if(tcsetattr(fd, TCSANOW, &port_settings) < 0) {
        perror("tcsetattr");
        return false;
    }

    int iflags = TIOCM_DTR;
    ioctl(fd, TIOCMBIC, &iflags); // turn off DTR

    return true;
} //configure port