规范模式 Linux 串口

Fra*_*Fra 0 c linux serial-port termios

规范模式状态的 Termios 手册页 ( http://man7.org/linux/man-pages/man3/termios.3.html ):

输入逐行可用。当键入行分隔符之一(NL、EOL、EOL2;或行首的 EOF)时,输入行可用。除了 EOF 的情况外,行分隔符包含在 read(2) 返回的缓冲区中。

我的问题是:当一块硬件输出符合规范的数据时 - 是否将 0xD0xA (CRLF) 字节放在传输线的开头以告诉 read() 函数数据已准备好读取?

我以前没有考虑过这个问题,并且默认(可能是错误的)认为 0xD0xA 位于传输线的末端。

saw*_*ust 5

是否将 0xD0xA (CRLF) 字节放在传输线的开头以告诉 read() 函数数据已准备好读取?

我在你的另一篇文章中对你的最后评论基本上已经回答了你的问题。
显然你不相信手册页或我,也不清楚“行分隔符”、行终止和 EOL 是什么意思。

“串行端口”“硬件”没有一个概念“开始”“结束”的的“传输线”。这只是 U[S]ART 的有效载荷数据。
行终止只有在规范模式下使用 termios 读取串行终端缓冲区时才有上下文。
请参阅Linux 串行驱动程序以了解如何从硬件中删除您的用户空间代码。

Linux 使用换行符或具有 ASCII 代码 0x0A 的换行符作为行终止符,如手册页(您已引用)中明确说明的那样。
Termios 允许定义额外的行尾字符,即串行终端的 VEOL 和 VEOL2。
每一次出现的行分隔符都可以并且将导致(待处理的)规范read()返回。
行分隔符将是缓冲区中返回的最后一个字符,除非用户缓冲区太小而无法包含整行。

为 EOF 定义的字符,即默认为 EOT 的 ASCII 码 0x04 的VEOF,termios 的处理略有不同。
收到 EOF 字符会导致(待处理的)规范read()像行分隔符一样返回,但 EOF 字符未存储在返回的缓冲区中。
因此,当 EOF 前面有一个行分隔符时,read()将有一个返回码为零,一个实际的空行!

如果您是这样一个怀疑的托马斯,那么您应该将一对 USB-RS232 适配器交叉连接在一起,并测试使用 termios 从串行终端读取时会发生什么。在第一个串行终端上
使用终端仿真程序(例如minicom)输入数据,并使用以下 C 程序查看另一个串行终端上的规范读取。

#define SERIALTERMINAL      "/dev/ttyUSB1"
#include <errno.h>
#include <fcntl.h> 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

int set_interface_attribs(int fd, int speed)
{
    struct termios tty;

    if (tcgetattr(fd, &tty) < 0) {
        printf("Error from tcgetattr: %s\n", strerror(errno));
        return -1;
    }

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

    tty.c_cflag |= CLOCAL | CREAD;
    tty.c_cflag &= ~CSIZE;
    tty.c_cflag |= CS8;         /* 8-bit characters */
    tty.c_cflag &= ~PARENB;     /* no parity bit */
    tty.c_cflag &= ~CSTOPB;     /* only need 1 stop bit */
    tty.c_cflag &= ~CRTSCTS;    /* no hardware flowcontrol */

    tty.c_lflag |= ICANON | ISIG;  /* canonical input */
    tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN);

    tty.c_iflag &= ~IGNCR;  /* preserve carriage return */
    tty.c_iflag &= ~INPCK;
    tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL);
    tty.c_iflag &= ~(IXON | IXOFF | IXANY);   /* no SW flowcontrol */

    tty.c_oflag &= ~OPOST;

    tty.c_cc[VEOL] = 0;
    tty.c_cc[VEOL2] = 0;
    tty.c_cc[VEOF] = 0x04;

    if (tcsetattr(fd, TCSANOW, &tty) != 0) {
        printf("Error from tcsetattr: %s\n", strerror(errno));
        return -1;
    }
    return 0;
}


int main()
{
    char *portname = SERIALTERMINAL;
    int fd;
    int wlen;

    fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
    if (fd < 0) {
        printf("Error opening %s: %s\n", portname, strerror(errno));
        return -1;
    }
    /*baudrate 115200, 8 bits, no parity, 1 stop bit */
    set_interface_attribs(fd, B115200);

    /* simple output */
    wlen = write(fd, "Hello!\n", 7);
    if (wlen != 7) {
        printf("Error from write: %d, %d\n", wlen, errno);
    }
    tcdrain(fd);    /* delay for output */


    /* simple canonical input */
    do {
        unsigned char buf[83];
        unsigned char *p;
        int rdlen;

        rdlen = read(fd, buf, sizeof(buf) - 1);
        if (rdlen > 0) {
            buf[rdlen] = 0;
            printf("Read %d:", rdlen);
            /* first display as hex numbers then ASCII */
            for (p = buf; rdlen-- > 0; p++) {
                printf(" 0x%x", *p);
                if (*p < ' ')
                    *p = '.';   /* replace any control chars */
            }
            printf("\n    \"%s\"\n\n", buf);
        } else if (rdlen < 0) {
            printf("Error from read: %d: %s\n", rdlen, strerror(errno));
        } else {  /* rdlen == 0 */
            printf("Nothing read. EOF?\n");
        }               
        /* repeat read */
    } while (1);
}
Run Code Online (Sandbox Code Playgroud)

请注意,该程序不会删除 '\r' 字符(即清除属性 IGNCR)​​,但回车符也未定义为行分隔符。
因此,此 termios 配置中的回车没有特殊含义,就像任何可打印字符一样被传递。

所以打字(相当于)ABCDEFG^M^J读作:

Read 9: 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0xd 0xa
    "ABCDEFG.."
Run Code Online (Sandbox Code Playgroud)

123^Mabc^J 读作:

Read 8: 0x31 0x32 0x33 0xd 0x61 0x62 0x63 0xa
    "123.abc."
Run Code Online (Sandbox Code Playgroud)

另一种 termios 配置可以去掉回车符或将回车符视为行分隔符。