簡體   English   中英

規范模式 Linux 串口

[英]Canonical Mode Linux Serial Port

規范模式狀態的 Termios 手冊頁 ( http://man7.org/linux/man-pages/man3/termios.3.html ):

輸入是逐行提供的。 當輸入行分隔符之一(NL、EOL、EOL2;或行首的 EOF)時,輸入行可用。 除了 EOF 的情況外,行分隔符包含在 read(2) 返回的緩沖區中。

我的問題是:當一塊硬件輸出符合規范的數據時 - 是否將 0xD0xA (CRLF) 字節放在傳輸線的開頭以告訴 read() 函數數據已准備好讀取?

我以前沒有考慮過這個問題,並且默認(可能是錯誤的)認為 0xD0xA 位於傳輸線的末端。

是否將 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);
}

請注意,該程序不會刪除 '\\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 配置可以去掉回車符或將回車符視為行分隔符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM