是否将0xD0xA(CRLF)字节放在传输线的开头以告知read()函数数据已准备好被读取?
在 “串行端口” 或 “硬件” 没有一个概念 “开始” 或 “结束” 的的 “传输线” 。只是到U [S] ART的有效载荷数据。
仅当在标准模式下使用termios读取串行终端缓冲区时,线路终端才具有上下文。
请参阅Linux串行驱动程序,以了解如何从硬件中删除用户空间代码。
Linux使用换行符或具有ASCII代码0x0A的换行符作为行终止符,如 手册 页(已引述)中明确指出的那样。
Termios允许定义其他行尾字符,即串行终端的VEOL和VEOL2。
每次出现行定界符都会并且将导致(待定)规范 read() 返回。
行定界符将是缓冲区中返回的最后一个字符,除非用户缓冲区太小而无法包含整行。
为EOF定义的字符,即 VEOF ,默认为EOT的ASCII代码0x04,由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: %sn", 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; tty.c_cflag &= ~PARENB; tty.c_cflag &= ~CSTOPB; tty.c_cflag &= ~CRTSCTS; tty.c_lflag |= ICANON | ISIG; tty.c_lflag &= ~(ECHO | ECHOE | ECHONL | IEXTEN); tty.c_iflag &= ~IGNCR; tty.c_iflag &= ~INPCK; tty.c_iflag &= ~(INLCR | ICRNL | IUCLC | IMAXBEL); tty.c_iflag &= ~(IXON | IXOFF | IXANY); 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: %sn", 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: %sn", portname, strerror(errno)); return -1; } set_interface_attribs(fd, B115200); wlen = write(fd, "Hello!n", 7); if (wlen != 7) { printf("Error from write: %d, %dn", wlen, errno); } tcdrain(fd); do { unsigned char buf[81]; unsigned char *p; int rdlen; rdlen = read(fd, buf, sizeof(buf) - 1); if (rdlen > 0) { buf[rdlen] = 0; printf("Read %d:", rdlen); for (p = buf; rdlen-- > 0; p++) { printf(" 0x%x", *p); if (*p < ' ') *p = '.'; } printf("n "%s"nn", buf); } else if (rdlen < 0) { printf("Error from read: %d: %sn", rdlen, strerror(errno)); } else { printf("Nothing read. EOF?n"); } } while (1);}请注意,该程序不会去除’ r’字符(即,属性IGNCR被清除),但是回车符也未定义为行定界符。
因此,在此termios配置中,回车符没有特殊含义,并且像任何可打印字符一样通过。
因此,键入(等效于)
ABCDEFG^M^J的内容为:
Read 9: 0x41 0x42 0x43 0x44 0x45 0x46 0x47 0xd 0xa "ABCDEFG.."
123^Mabc^J读取为:
Read 8: 0x31 0x32 0x33 0xd 0x61 0x62 0x63 0xa "123.abc."
可选的termios配置可以去除回车符或将回车符视为行定界符。



