linux命令行、程序配置修改串口波特率

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;
 }

  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

弗朗克21

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值