串行端口 linux,规范模式Linux串行端口
是否將0xD0xA(CRLF)字節(jié)放在傳輸線的開頭以告知read()函數(shù)數(shù)據(jù)已準備好被讀取?
在 “串行端口” 或 “硬件” 沒有一個概念 “開始” 或 “結(jié)束” 的的 “傳輸線” 。只是到U [S] ART的有效載荷數(shù)據(jù)。
僅當在標準模式下使用termios讀取串行終端緩沖區(qū)時,線路終端才具有上下文。
請參閱Linux串行驅(qū)動程序,以了解如何從硬件中刪除用戶空間代碼。
Linux使用換行符或具有ASCII代碼0x0A的換行符作為行終止符,如 手冊 頁(已引述)中明確指出的那樣。
Termios允許定義其他行尾字符,即串行終端的VEOL和VEOL2。
每次出現(xiàn)行定界符都會并且將導致(待定)規(guī)范 read() 返回。
行定界符將是緩沖區(qū)中返回的最后一個字符,除非用戶緩沖區(qū)太小而無法包含整行。
為EOF定義的字符,即 VEOF ,默認為EOT的ASCII代碼0x04,由termios處理略有不同。
EOF字符的接收導致(待定的)規(guī)范 read() 像行定界符一樣返回,但是EOF字符未存儲在返回的緩沖區(qū)中。
因此,當EOF前面有行定界符時, read() 的返回碼為零,即實際的空行!
如果您是一位懷疑的托馬斯,那么您應該將一對USB-RS232適配器交叉連接在一起,并測試使用termios從串行終端讀取數(shù)據(jù)時會發(fā)生什么。在第一個串行終端上
使用終端仿真器程序(例如 minicom) 輸入數(shù)據(jù),并使用以下C程序查看另一個串行終端上的規(guī)范讀取。
#define SERIALTERMINAL "/dev/ttyUSB1"
#include
#include
#include
#include
#include
#include
#include
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[81];
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);
}
請注意,該程序不會去除’\ r’字符(即,屬性IGNCR被清除),但是回車符也未定義為行定界符。
因此,在此termios配置中,回車符沒有特殊含義,并且像任何可打印字符一樣通過。
因此,鍵入(等效于)ABCDEFG^M^J的內(nèi)容為:
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配置可以去除回車符或?qū)⒒剀嚪暈樾卸ń绶?/p>
總結(jié)
以上是生活随笔為你收集整理的串行端口 linux,规范模式Linux串行端口的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 重启openssl服务linux,Ngi
- 下一篇: linux c语言定时任务crontab