我对读取和写入串行端口感到有点困惑.我在Linux中有一个USB设备,它使用FTDI USB串行设备转换器驱动程序.当我插入它时,它会创建:/dev/ttyUSB1.

我以为用C语言打开和读写它会很简单.我知道波特率和奇偶校验信息,但似乎没有这个标准?

是我错过了什么,还是有人给我指对了方向?

推荐答案

我很久以前写过这篇文章(from years 1985-1992, with just a few tweaks since then),只需将所需的部分复制并粘贴到每个项目中.

您必须在从tcgetattr获得的tty上拨打cfmakeraw.您不能将struct termios置零,对其进行配置,然后将tty设置为tcsetattr.如果您使用清零方法,那么您将遇到无法解释的间歇性故障,特别是在BSD和OSX上."无法解释的间歇性故障"包括在read(3)挂起.

#include <errno.h>
#include <fcntl.h> 
#include <string.h>
#include <termios.h>
#include <unistd.h>

int
set_interface_attribs (int fd, int speed, int parity)
{
        struct termios tty;
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tcgetattr", errno);
                return -1;
        }

        cfsetospeed (&tty, speed);
        cfsetispeed (&tty, speed);

        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars
        // disable IGNBRK for mismatched speed tests; otherwise receive break
        // as \000 chars
        tty.c_iflag &= ~IGNBRK;         // disable break processing
        tty.c_lflag = 0;                // no signaling chars, no echo,
                                        // no canonical processing
        tty.c_oflag = 0;                // no remapping, no delays
        tty.c_cc[VMIN]  = 0;            // read doesn't block
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl

        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,
                                        // enable reading
        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity
        tty.c_cflag |= parity;
        tty.c_cflag &= ~CSTOPB;
        tty.c_cflag &= ~CRTSCTS;

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
        {
                error_message ("error %d from tcsetattr", errno);
                return -1;
        }
        return 0;
}

void
set_blocking (int fd, int should_block)
{
        struct termios tty;
        memset (&tty, 0, sizeof tty);
        if (tcgetattr (fd, &tty) != 0)
        {
                error_message ("error %d from tggetattr", errno);
                return;
        }

        tty.c_cc[VMIN]  = should_block ? 1 : 0;
        tty.c_cc[VTIME] = 5;            // 0.5 seconds read timeout

        if (tcsetattr (fd, TCSANOW, &tty) != 0)
                error_message ("error %d setting term attributes", errno);
}


...
char *portname = "/dev/ttyUSB1"
 ...
int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
{
        error_message ("error %d opening %s: %s", errno, portname, strerror (errno));
        return;
}

set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)
set_blocking (fd, 0);                // set no blocking

write (fd, "hello!\n", 7);           // send 7 character greeting

usleep ((7 + 25) * 100);             // sleep enough to transmit the 7 plus
                                     // receive 25:  approx 100 uS per char transmit
char buf [100];
int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read

速度的值是B115200B230400B9600B19200B38400B57600B1200B2400B4800等.奇偶校验的值是0(表示无奇偶校验)、PARENB|PARODD(启用奇偶校验并使用奇数)、PARENB(启用奇偶校验并使用偶数)、PARENB|PARODD|CMSPAR(标记奇偶校验)和PARENB|CMSPAR(空格奇偶校验).

"BLOCKING"设置端口上的read()是否等待指定数量的字符到达.设置no blocking意味着,无论有多少个字符可用,read()都会返回,而无需等待更多字符,直到缓冲区限制.


增编:

只有 Select 标记和空格奇偶校验时才需要CMSPAR,这并不常见.对于大多数应用程序,它可以省略.只有在定义了预处理器符号__USE_MISC的情况下,我的头文件/usr/include/bits/termios.h才能定义CMSPAR.该定义(在features.h中)出现在

#if defined _BSD_SOURCE || defined _SVID_SOURCE
 #define __USE_MISC     1
#endif

<features.h>的介绍性 comments 说:

/* These are defined by the user (or the compiler)
   to specify the desired environment:

...
   _BSD_SOURCE          ISO C, POSIX, and 4.3BSD things.
   _SVID_SOURCE         ISO C, POSIX, and SVID things.
...
 */

C++相关问答推荐

C中的整字母后缀i是什么

Apple Libm的罪恶功能

理解没有返回语句的递归C函数的行为

在32位处理器上优化53—32位模计算>

堆栈帧和值指针

自定义应用程序上的日志(log)轮换问题

创建一个fork导致fget无限地重新读取文件

在c++中使用堆栈的有效括号

为什么即使在强制转换时,此代码也会溢出?

双指针指向常量双指针的指针类型赋值不兼容

我怎么才能用GCC编译一个c库,让它包含另一个库呢?

如何使用指向 struct 数组的指针并访问数组中特定索引处的 struct

在WSL关闭/重新启动后,是什么原因导致共享对象依赖关系发生更改?

getline()从c中的外部函数传递指针时输出null

从TCP连接启动UDP(C套接字)

在C++中允许使用字符作为宏参数

哪个首选包含第三个库S头文件?#INCLUDE;文件名或#INCLUDE<;文件名&>?

如何使用WRITE()以指针地址的十六进制形式写入标准输出

变量值不正确的问题

Tcl_GetDoubleFromObj在列表的迭代中是一个缺点