1.命令行修改串口波特率
查看串口配置信息:stty -a -F /dev/tty0 (tty0是串口名称)
设置串口波特率为115200:stty -F /dev/ttyUSB1 speed 115200
stty参数说明
-a, --all
print all current settings in human-readable form
-g, --save
print all current settings in a stty-readable form
-F, --file=DEVICE
open and use the specified DEVICE instead of stdin
–help
display this help and exit
–version
output version information and exit
2.程序修改串口波特率
①在linux系统下,一切皆文件,open()打开串口路径
②tcgetattr()获取串口有关参数
③cfsetispeed()和cfseospeed()函数设置输入与输出口的波特率
④tcgetattr()设置串口有关参数
例如将/dev/ttyUSB1串口比特率设置为B9600程序如下:
int main(int argc, char **argv)
{
struct termios options;
int fd = -1;
speed_t baud_rate = B9600;
if ((fd = open("/dev/ttyUSB1", O_RDONLY)) < 0)
{
printf("open failure\n");
return -1;
}
if (tcgetattr(fd, &options) < 0)
{
printf("tcgetattr failure\n");
return -2;
}
cfsetispeed(&options, baud_rate);
cfsetospeed(&options, baud_rate);
if (tcsetattr(fd, TCSANOW, &options) < 0)
{
printf("tcsetattr failure\n");
return -3;
}
close(fd);
return 0;
}