日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

串行端口 linux,规范模式Linux串行端口

發布時間:2025/5/22 linux 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 串行端口 linux,规范模式Linux串行端口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

是否將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

#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的內容為:

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配置可以去除回車符或將回車符視為行定界符。

總結

以上是生活随笔為你收集整理的串行端口 linux,规范模式Linux串行端口的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。