跳轉到內容

序列埠程式設計/termio

來自華夏公益教科書,開放的書籍,為了一個開放的世界

<termio.h>(注意缺少 's') 是較舊的 System V 終端 I/O API。它已被<termios.h>在現代系統上取代。它在許多系統中仍然被使用,例如嵌入式 Unix 系統或基於 Unix System V 的系統。通常,現代系統也仍然提供舊的 API。

對序列裝置的所有設定都是透過 ioctl(2) 系統呼叫完成的,而不是透過<termios.h>.

特殊字元

[編輯 | 編輯原始碼]
  • INTR (預設 Ctrl-C 或 ASCII ETX)
  • QUIT (預設 Ctrl-\ 或 ASCII ES)
  • ERASE (預設退格鍵或 ASCII BS)
  • KILL (Ctrl-U 或 ASCII NAK)
  • EOF (Ctrl-D 或 ASCII EOT)
  • NL (ASCII LF)
  • EOL (ASCII LF)
  • STOP (Ctrl-S 或 ASCII DC3)
  • START (Ctrl-Q 或 ASCII DC1)

透過 Termio 設定和獲取引數

[編輯 | 編輯原始碼]
主要命令
[編輯 | 編輯原始碼]

使用 termio 控制序列(終端)I/O 的主要命令都使用以下形式的 ioctl() 呼叫

ioctl(int fileDescriptor, int termioCommand, struct termio *arg);

以下命令使用此 ioctl() 形式

TCGETA
獲取當前引數
#include <termio.h>
    :
    .
struct termio params;
ioctl(fileDescriptor, TCGETA, &params);
TCSETA
立即設定引數
TCSETAW
在輸出為空時設定引數(等待更改,直到所有緩衝資料都被髮送)。
TCSETAF
等待輸出為空,然後重新整理輸入,然後設定引數。

struct termio引數在所有上述ioctl(2)命令中使用,如下所示

struct termio
/*
 * Classic struct termio. More modern Unix versions
 * contain additional information. Unix versions who
 * support termio and termios often use the same
 * structure for termio and termios, so termio
 * contains the full termios data on this systems.
 */
#define NCC 8
struct termio {
    int c_iflag;	 /* input modes	  */
    int c_oflag;	 /* output modes  */
    int c_cflag;	 /* control modes */
    int c_lflag;	 /* local modes	  */
    char c_cc[NCC];	 /* control chars */
};


  • struct termio 中的 c_cc 陣列
  • struct termio 中的 c_iflag 輸入模式標誌
    • IGNBRK
    • BRKINT
    • IGNPAR
    • PARMRK
    • INPCK
    • ISTRIP
    • INLCR
    • IGNCR
    • ICRNL
    • IUCLC
    • IXON
    • IXANY
    • IXOFF
  • struct termio 中的 c_oflag 輸出模式標誌
    • OPOST
    • OLCUC
    • ONLCR
    • OCRNL
    • ONOCR
    • ONLRET
    • OFILL
    • OFDEL
    • NLDLY
    • NL0
    • NL1
    • CRDLY
    • CR0
    • CR1
    • CR2
    • CR3
    • TABDLY
    • TAB0
    • TAB1
    • TAB2
    • TAB3
    • BSDLY
    • BS0
    • BS1
    • VTDLY
    • VT0
    • VT1
    • FFDLY
    • FF0
    • FF1
  • struct termio 中的 c_cflag
    • B0, B50, B75, B110, B134, B150, B300, B600, B1200, B1800, B2400, B4800, B9600, B19200, B38400 用於選擇波特率
    • CSIZE
    • CS5, CS6, CS7, CS8 用於設定資料位長度,介於 5..8 之間
    • CSTOPB
    • CREAD
    • PARENB 用於啟用奇偶校驗
    • PARODD 用於選擇奇校驗
    • HUPCL
    • CLOCAL
  • struct termio 中的 c_lflag
    • ISIG
    • ICANON
    • XCASE
    • ECHO
    • ECHOE
    • ECHOK
    • ECHONL
    • NOFLSH
附加命令
[編輯 | 編輯原始碼]

附加命令使用以下形式的 ioctl() 呼叫

ioctl(int fileDescriptor, int termioCommand, int arg);

以下 termio 命令使用此形式

TCSBRK
等待輸出為空(清空)。可選地,當發生這種情況時,可以傳送一個斷開連線。事實上,這是最常見的應用
#include <termio.h>
/* Convenience macros for the waitBreak cmd argument */ #define WAIT_N_BREAK 0 #define WAIT_ONLY 1
/* * Function for waiting until output is empty and opt. sending a break */ tcWaitBreak(int fileDescriptor, int cmd) { ioctl(fileDescriptor, TCSBRK, cmd); }
/* * Send break when output is empty */ tcBreak(int fileDescriptor) { tcWaitBreak(fileDescriptor, WAIT_N_BREAK); }
TCXONC
啟動或停止輸出。
#include <termio.h>
/* Convenience macros for the start/stop cmd argument */ #define STOP 0 #define START 1
/* * Function for starting /stopping output */ tcStartStop(int fileDescriptor, int cmd) { ioctl(fileDescriptor, TCXONC, cmd); }
/* * stop output */ tcStop(int fileDescriptor) { tcStartStop(fileDescriptor, STOP); }
/* * start output */ tcStart(int fileDescriptor) { tcStartStop(fileDescriptor, START); }


TCFLSH
重新整理輸入、輸出或兩個佇列。
#include <termio.h>
/* Convenience macros for the flush cmd argument */ #define FLUSH_IN 0 #define FLUSH_OUT 1 #define FLUSH_BOTH 2
/* * Function for flushing a terminal/serial device */ tcFlush(int fileDescriptor, int cmd) { ioctl(fileDescriptor, TCFLSH, cmd); }


請僅在書籍標題頁中新增 {{alphabetical}}

華夏公益教科書