Linux終端驅動程序緩衝輸入,即使它沒有打開。這可能是一個有用的功能,特別是如果速度/奇偶校驗等。適當設置。
要複製較小的操作系統的行爲,只要它是開放的從端口讀取所有未決的輸入:
...
int fd = open ("/dev/ttyS0", O_RDWR | O_NOCTTY | O_SYNC);
if (fd < 0)
exit (1);
set_blocking (fd, 0); // disable reads blocked when no input ready
char buf [10000];
int n;
do {
n = read (fd, buf, sizeof buf);
} while (n > 0);
set_blocking (fd, 1); // enable read blocking (if desired)
... // now there is no pending input
void set_blocking (int fd, int should_block)
{
struct termios tty;
memset (&tty, 0, sizeof tty);
if (tcgetattr (fd, &tty) != 0)
{
error ("error %d getting term settings set_blocking", errno);
return;
}
tty.c_cc[VMIN] = should_block ? 1 : 0;
tty.c_cc[VTIME] = should_block ? 5 : 0; // 0.5 seconds read timeout
if (tcsetattr (fd, TCSANOW, &tty) != 0)
error ("error setting term %sblocking", should_block ? "" : "no");
}
我在想,可能是這樣。那麼,爲什麼我只看到這種行爲的一部分?我使用pyserial進行測試,並將包含的miniterm.py腳本作爲交互式終端。這不僅僅是一個串口有問題。它似乎是交替的。所有串行端口在關閉時都會有數據傳入,但只有一個端口在我打開它時會緩衝。也許波特率只能在那個波特率上正確設置? – djs
@djs:端口速度是我想到的第一件事。也可能是'gpsd'或'logind'正在訪問封閉的端口以尋找GPS設備或登錄連接並推測性地改變端口速度。 – wallyk
這些服務都沒有運行。 stty在我檢查之前報告了115200的所有端口,發現其中只有一個保留了緩衝區。這是用來檢查波特率的正確工具嗎? – djs