How to set baud rate to 307200 on Linux?

后端 未结 6 2039
走了就别回头了
走了就别回头了 2020-11-28 10:36

Basically I\'m using the following code to set the baud rate of a serial port:

struct termios options;
tcgetattr(fd, &options);
cfsetispeed(&options,         


        
6条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 11:18

    I accomplished this using termios2 and ioctl() commands.

    struct termios2 options;
    ioctl(fd, TCGETS2, &options);
    options.c_cflag &= ~CBAUD;    //Remove current BAUD rate
    options.c_cflag |= BOTHER;    //Allow custom BAUD rate using int input
    options.c_ispeed = 307200;    //Set the input BAUD rate
    options.c_ospeed = 307200;    //Set the output BAUD rate
    ioctl(fd, TCSETS2, &options);
    

    After that, you should be able to query the port settings and see your custom BAUD rate, as well as the other settings (possible with stty commands).

提交回复
热议问题