繁体   English   中英

Linux串行通信的波特率

[英]baud rate for serial communication in linux

默认情况下,串行通信期间的波特率是多少。 就是说,如果编写一个程序时我没有提到任何波特率,那么将考虑什么波特率?

如果在POSIX系统上:

  1. 使用open()打开端口。
  2. 将文件描述符从1.传递到tcgetattr()以初始化struct termios
  3. 将对struct termios cfgetispeed()的引用从2传递到cfgetispeed() / cfgetospeed()以获取端口的当前入站/出站波特率。

例:

#include <termios.h>
#include <unistd.h>

[...]

struct termios t = {0};
speed_t baudrate_in = 0;
speed_t baudrate_out = 0;
int fd = open("/dev/ttyS0", O_RDWR);
if (-1 == fd)
{
  perror("open() failed");
  exit(1);
}

if (-1 == tcgetattr(fd, &t))
{
  perror("tcgetattr() failed");
  exit(1);
}

baudrate_in = cfgetispeed(&t);    
baudrate_out = cfgetospeed(&t);

您可以使用setserial找出http://linux.die.net/man/8/setserial

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM