{"id":307,"date":"2023-04-07T09:28:39","date_gmt":"2023-04-07T01:28:39","guid":{"rendered":"https:\/\/yajtech.synology.me\/wordpress\/?p=307"},"modified":"2023-04-08T21:14:34","modified_gmt":"2023-04-08T13:14:34","slug":"linux-rs232-%e8%a8%ad%e8%a8%88","status":"publish","type":"post","link":"https:\/\/www.yaj.com.tw\/wordpress\/?p=307","title":{"rendered":"Linux RS232 \u8a2d\u8a08"},"content":{"rendered":"\n<p>\u7d42\u7aef\u6a5f\u4ecb\u9762<\/p>\n\n\n\n<p>\u7d42\u7aef\u6a5f\u4ecb\u9762\u53c8\u7a31\u70baTTY\u4ecb\u9762\uff0c\u7528\u4f86\u8b93Linux\u7cfb\u7d71\u900f\u904eRS-232\u4e32\u5217\u57e0\u9023\u63a5\u8a2d\u5099\u3002\u7d42\u7aef\u6a5f\u4ecb\u9762\u6709\u4e8c\u7a2e\u6a21\u5f0f\uff1a\u6b63\u898f(canonical)\u6a21\u5f0f\u548c\u975e\u6b63\u898f(non-canonical)\u6a21\u5f0f\u3002\u6211\u5011\u53ef\u4ee5\u900f\u904eminicom\u7a0b\u5f0f\u4f86\u9032\u884c\u6e9d\u901a\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>\u6a21\u5f0f<\/td><td>\u8aaa\u660e<\/td><\/tr><tr><td>\u6b63\u898f\u6a21\u5f0f<\/td><td>\u53c8\u7a31\u70bacooked\u6a21\u5f0f\u3002\u5728\u9019\u7a2e\u6a21\u5f0f\u4e2d\uff0c\u7d42\u7aef\u8a2d\u5099\u6703\u8655\u7406\u7279\u6b8a\u5b57\u5143\uff0c\u4e14\u6703\u4ee5\u4e00\u6b21\u4e00\u5217\u7684\u65b9\u5f0f\u5c07\u8f38\u5165\u50b3\u7d66\u61c9\u7528\u7a0b\u5f0f\u3002\u4f8b\u5982Linux\u7684shell\u6307\u4ee4\u3002<\/td><\/tr><tr><td>\u975e\u6b63\u898f\u6a21\u5f0f<\/td><td>\u53c8\u7a31\u70baraw\u6a21\u5f0f\u3002\u5728\u9019\u7a2e\u6a21\u5f0f\u4e2d\uff0c\u7d42\u7aef\u8a2d\u5099\u4e0d\u6703\u8655\u7406\u7279\u6b8a\u5b57\u5143\uff0c\u4e14\u6703\u4ee5\u4e00\u6b21\u4e00\u500b\u5b57\u5143\u7684\u65b9\u5f0f\u5c07\u8f38\u5165\u50b3\u7d66\u61c9\u7528\u7a0b\u5f0f\u3002\u4f8b\u5982\u5728Linux\u4f7f\u7528vim\u7de8\u8f2f\u7a0b\u5f0f\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u958b\u555f\u901a\u8a0a\u57e0<\/p>\n\n\n\n<p>\u5728Linux\u4e2d\u5c07\u4e32\u5217\u57e0\u8996\u70ba\u4e00\u500b\u6a94\u6848\uff0c\u53ef\u4ee5\u4f7f\u7528open()\u51fd\u6578\u4f86\u958b\u555f\u4e32\u5217\u57e0\u3002\u5e95\u4e0b\u7684\u7a0b\u5f0f\u7528\u4f86\u958b\u555fPC\u7684COM1\u4e32\u5217\u901a\u8a0a\u57e0\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>O_NOCTTY<\/td><td>\u544a\u8a34Linux\u9019\u500b\u7a0b\u5f0f\u4e0d\u60f3\u63a7\u5236TTY\u4ecb\u9762\uff0c\u5982\u679c\u4e0d\u8a2d\u5b9a\u9019\u500b\u65d7\u6a19\uff0c\u6709\u4e9b\u8f38\u5165(\u4f8b\u5982\u9375\u76e4\u7684abort)\u4fe1\u865f\u53ef\u80fd\u5f71\u97ff\u7a0b\u5f0f\u3002<\/td><\/tr><tr><td> O_NDELAY <\/td><td>\u544a\u8a34Linux\u9019\u500b\u7a0b\u5f0f\u4e0d\u4ecb\u610fRS-232\u7684DCD\u4fe1\u865f\u7684\u72c0\u614b\u3002\u5982\u679c\u4e0d\u8a2d\u5b9a\u9019\u500b\u65d7\u6a19\uff0c\u7a0b\u5f0f\u6703\u8655\u65bcspeep\u72c0\u614b\uff0c\u76f4\u5230RS-232\u6709DCD\u4fe1\u865f\u9032\u4f86\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<pre class=\"wp-block-code\"><code>int open_port(void)\n{\n    int fd;\n    fd=open(\"\/dev\/ttyS0\",O_RDWR|O_NOCTTY|O_NDELAY);\n    if(fd == -1){\n        perror(\"open error\");\n    }\n    retuen(fd);\n}<\/code><\/pre>\n\n\n\n<p>Termios\u7d50\u69cb<\/p>\n\n\n\n<p>\u5728Linux\u4e2d\u8a2d\u5b9a\u4e32\u5217\u57e0\u7684\u53c3\u6578\uff0c\u4f8b\u5982\u9b91\u7387\u3001\u5b57\u5143\u9577\u5ea6\u7b49\uff0c\u53ef\u4ee5\u900f\u904ePOSIX\u6a19\u6e96\u7d42\u7aef\u4ecb\u9762\uff0c\u6b64\u4ecb\u9762\u7a31\u70baTermios\uff0c\u4e26\u5b9a\u7fa9\u65bc\u7cfb\u7d71\u7684\u6a19\u982d\u6a94\u4e2d\u3002termios\u7684\u7d50\u69cb\u5982\u4e0b\uff1a<br>struct termios{<br>&nbsp;&nbsp;&nbsp; tcflag_t c_iflag;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8f38\u5165\u6a21\u5f0f<br>&nbsp;&nbsp;&nbsp; tcflag_t c_oflag;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u8f38\u51fa\u6a21\u5f0f<br>&nbsp;&nbsp;&nbsp; tcflag_t c_cflag;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u63a7\u5236\u6a21\u5f0f<br>&nbsp;&nbsp;&nbsp; tcflag_t c_lflag;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \/\/\u5c40\u90e8\u6a21\u5f0f<br>&nbsp;&nbsp;&nbsp; cc_t c_cc[NCCS];&nbsp;&nbsp;     \/\/\u7279\u6b8a\u63a7\u5236\u5b57\u5143<br>}<\/p>\n\n\n\n<p>\u8f38\u5165\u6a21\u5f0f c_iflag<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u4f7f\u7528RS-232\u63a5\u6536\u5b57\u5143\uff0c\u57f7\u884c\u540c\u4f4d\u4f4d\u5143\u6aa2\u67e5\uff0cc_iflag\u8a2d\u5b9a\u5982\u4e0b\uff1a\noptions.c_iflag |= (INPCK | ISTRIP);\n\u4e32\u5217\u57e0\u5ffd\u7565\u540c\u4f4d\u932f\u8aa4\uff0c\u63a5\u6536\u50b3\u5165\u7684\u5b57\u5143\noptions.c_iflag |= IGNPAR;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>IGNBRK<\/td><td>Ignore BREAK condition on input.<\/td><\/tr><tr><td>BRKINT<\/td><td>If IGNBRK is set, a BREAK is ignored. If it is not set but BRKINT is set, then a BREAK causes the input and output queues to be flushed, and if the terminal is the controlling terminal of a foreground process group, it will cause a SIGINT to be sent to this foreground process group. When neither IGNBRK nor BRKINT are set, a BREAK reads as a NUL character, except when PARMRK is set, in which case it reads as the sequence \\377 \\0 \\0.<\/td><\/tr><tr><td>IGNPAR<\/td><td>Ignore framing errors and parity errors. \u5ffd\u7565frame\u548c\u540c\u4f4d\u932f\u8aa4<\/td><\/tr><tr><td>PARMRK<\/td><td>If IGNPAR is not set, prefix a character with a parity error or framing error with \\377 \\0. If neither IGNPAR nor PARMRK is set, read a character with a parity error or framing error as \\0.<\/td><\/tr><tr><td>INPCK<\/td><td>Enable input parity checking. \u57f7\u884c\u540c\u4f4d\u4f4d\u5143\u6aa2\u67e5<\/td><\/tr><tr><td>ISTRIP<\/td><td>Strip off eighth bit. \u53bb\u9664\u7b2c8\u500b\u4f4d\u5143<\/td><\/tr><tr><td>INLCR<\/td><td>Translate NL to CR on input.<\/td><\/tr><tr><td>IGNCR<\/td><td>Ignore carriage return on input.<\/td><\/tr><tr><td>ICRNL<\/td><td>Translate carriage return to newline on input (unless IGNCR is set).<\/td><\/tr><tr><td>IUCLC<\/td><td>(not in POSIX) Map uppercase characters to lowercase on input.<\/td><\/tr><tr><td>IXON<\/td><td>Enable XON\/XOFF flow control on output.<\/td><\/tr><tr><td>IXANY<\/td><td>(not in POSIX.1; XSI) Enable any character to restart output.<\/td><\/tr><tr><td>IXOFF<\/td><td>Enable XON\/XOFF flow control on input.<\/td><\/tr><tr><td>IMAXBEL<\/td><td>(not in POSIX) Ring bell when input queue is full. Linux does not implement this bit, and acts as if it is always set.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u8f38\u51fa\u6a21\u5f0f c_oflag<\/p>\n\n\n\n<p>c_oflag\u5305\u542b\u8f38\u51fa\u904e\u6ffe\u529f\u80fd\uff0c\u8ca0\u8cac\u63a7\u5236\u8f38\u51fa\u5b57\u5143\u7684\u8655\u7406\u65b9\u5f0f\u3002\u8f38\u51fa\u5b57\u5143\u5728\u50b3\u9001\u5230\u5e8f\u5217\u57e0\u6216\u87a2\u5e55\u4e4b\u524d\u662f\u5982\u4f55\u88ab\u7a0b\u5f0f\u8655\u7406\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u82e5\u8981\u555f\u52d5\u8f38\u51fa\u8655\u7406\uff0c\u5fc5\u9808\u52a0\u5165OPOST\u9078\u9805\uff0c\u7a0b\u5f0f\u78bc\u5982\u4e0b\uff1a\noptions.c_oflag |= OPOST;\n\u5c07\u63db\u5217\u5b57\u5143\u8f49\u63db\u6210&#91;CR]&#91;LF]\noptions.c_oflag |= OPOST | ONLCR;\n\u82e5\u8981\u555f\u52d5\u975e\u6b63\u898f\u6a21\u5f0f\uff0c\u5c07OPOST\u9078\u9805\u8a2d\u70badisable\uff0c\u8a2d\u5b9a\u5982\u4e0b\uff1a\noptions.c_oflag &amp;= ~OPOST;<\/code><\/pre>\n\n\n\n<p>c_oflag\u7684\u65d7\u6a19\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>OPOST<\/td><td>Enable implementation-defined output processing.<\/td><\/tr><tr><td>OLCUC<\/td><td>(not in POSIX) Map lowercase characters to uppercase on output.<\/td><\/tr><tr><td>ONLCR<\/td><td>(XSI) Map NL to CR-NL on output.<\/td><\/tr><tr><td>OCRNL<\/td><td>Map CR to NL on output.<\/td><\/tr><tr><td>ONOCR<\/td><td>Don&#8217;t output CR at column 0.<\/td><\/tr><tr><td>ONLRET<\/td><td>Don&#8217;t output CR.<\/td><\/tr><tr><td>OFILL<\/td><td>Send fill characters for a delay, rather than using a timed delay.<\/td><\/tr><tr><td>OFDEL<\/td><td>(not in POSIX) Fill character is ASCII DEL (0177). If unset, fill character is ASCII NUL.<\/td><\/tr><tr><td>NLDLY<\/td><td>Newline delay mask. Values are NL0 and NL1.<\/td><\/tr><tr><td>CRDLY<\/td><td>Carriage return delay mask. Values are CR0, CR1, CR2, or CR3.<\/td><\/tr><tr><td>TABDLY<\/td><td>Horizontal tab delay mask. Values are TAB0, TAB1, TAB2, TAB3 (or XTABS). A value of TAB3, that is, XTABS, expands tabs to spaces (with tab stops every eight columns).<\/td><\/tr><tr><td>BSDLY<\/td><td>Backspace delay mask. Values are BS0 or BS1. (Has never been implemented.)<\/td><\/tr><tr><td>VTDLY<\/td><td>Vertical tab delay mask. Values are VT0 or VT1.<\/td><\/tr><tr><td>FFDLY<\/td><td>Form feed delay mask. Values are FF0 or FF1.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u63a7\u5236\u6a21\u5f0f c_cflag<\/p>\n\n\n\n<p>termios\u7d50\u69cb\u7684c_cflag\u6210\u54e1\u7528\u4f86\u63a7\u5236\u4e32\u5217\u57e0\u7684\u9b91\u7387\u3001\u540c\u4f4d\u5143\u3001\u505c\u6b62\u4f4d\u5143\u7b49\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8a2d\u5b9a\u50b3\u8f38\u7684\u8cc7\u6599\u9577\u5ea6\u70ba8 bits\uff1a\noptions.c_cflag |= CS8;\n\n\u8a2d\u5b9a(8N1)\u50b3\u8f38\u8cc7\u6599\u9577\u5ea68\u4f4d\u5143\u3001\u7121\u540c\u4f4d\u5143\u6aa2\u67e5\u30011\u505c\u6b62\u4f4d\u5143\uff1a\noptions.c_cflag |= ~PARENB;     \/\/\u4e0d\u5141\u8a31\u540c\u4f4d\u5143\u6aa2\u67e5\noptions.c_cflag |= ~CSTOPB;     \/\/\u4e0d\u662f2\u505c\u6b62\u4f4d\u5143\noptions.c_cflag |= CS8;             \/\/8 bits\n\n\u8a2d\u5b9a(7E1)\u50b3\u8f38\u8cc7\u6599\u9577\u5ea67\u4f4d\u5143\u3001\u5076\u540c\u4f4d\u5143\u6aa2\u67e5\u30011\u505c\u6b62\u4f4d\u5143\uff1a\noptions.c_cflag |= PARENB;     \/\/\u5141\u8a31\u540c\u4f4d\u5143\u6aa2\u67e5\noptions.c_cflag |= ~PARODD;    \/\/\u4e0d\u662f\u5947\u540c\u4f4d\u5143\u6aa2\u67e5\noptions.c_cflag |= ~CSTOPB;     \/\/\u4e0d\u662f2\u505c\u6b62\u4f4d\u5143\noptions.c_cflag |= CS7;             \/\/7 bits\n\n\u8a2d\u5b9a(7O1)\u50b3\u8f38\u8cc7\u6599\u9577\u5ea67\u4f4d\u5143\u3001\u5947\u540c\u4f4d\u5143\u6aa2\u67e5\u30011\u505c\u6b62\u4f4d\u5143\uff1a\noptions.c_cflag |= PARENB;     \/\/\u5141\u8a31\u540c\u4f4d\u5143\u6aa2\u67e5\noptions.c_cflag |= PARODD;    \/\/\u662f\u5947\u540c\u4f4d\u5143\u6aa2\u67e5\noptions.c_cflag |= ~CSTOPB;     \/\/\u4e0d\u662f2\u505c\u6b62\u4f4d\u5143\noptions.c_cflag |= CS7;             \/\/7 bits<\/code><\/pre>\n\n\n\n<p>c_cflag\u7684\u9078\u9805\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>CBAUD<\/td><td>(not in POSIX) Baud speed mask (4+1 bits).<\/td><\/tr><tr><td>CBAUDEX<\/td><td>(not in POSIX) Extra baud speed mask (1 bit), included in CBAUD.<\/td><\/tr><tr><td>CSIZE<\/td><td>Character size mask. Values are CS5, CS6, CS7, or CS8.<\/td><\/tr><tr><td>CSTOPB<\/td><td>Set two stop bits, rather than one.<\/td><\/tr><tr><td>CREAD<\/td><td>Enable receiver. \u5141\u8a31\u4e32\u5217\u57e0\u8b80\u53d6\u50b3\u5165\u7684\u8cc7\u6599\u3002<\/td><\/tr><tr><td>PARENB<\/td><td>Enable parity generation on output and parity checking for input.<\/td><\/tr><tr><td>PARODD<\/td><td>Parity for input and output is odd.<\/td><\/tr><tr><td>HUPCL<\/td><td>Lower modem control lines after last process closes the device (hang up).<\/td><\/tr><tr><td>CLOCAL<\/td><td>Ignore modem control lines. \u5ffd\u7565\u6578\u64da\u6a5f\u63a7\u5236\u7dda\u7684\u4fe1\u865f<\/td><\/tr><tr><td>LOBLK<\/td><td>(not in POSIX) Block output from a noncurrent shell layer. (For use by shl.)<\/td><\/tr><tr><td>CIBAUD<\/td><td>(not in POSIX) Mask for input speeds. The values for the CIBAUD bits are the same as the values for the CBAUD bits, shifted left IBSHIFT bits.<\/td><\/tr><tr><td>CRTSCTS<\/td><td>(not in POSIX) Enable RTS\/CTS (hardware) flow control.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5c40\u90e8\u6a21\u5f0f c_lflag<\/p>\n\n\n\n<p>c_lflag\u7528\u4f86\u63a7\u5236\u4e32\u5217\u57e0\u5982\u4f55\u8655\u7406\u8f38\u5165\u5b57\u5143\u3002\u900f\u904ec_lflag\u8a2d\u5b9a\u4e32\u5217\u57e0\u70ba\u6b63\u898f\u6a21\u5f0f\u6216\u975e\u6b63\u898f\u6a21\u5f0f<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5c07\u4e32\u5217\u57e0\u8a2d\u5b9a\u70ba\u6b63\u898f\u6a21\u5f0f\noptions.c_lflag |= (ICANON | ECHO | ECHOE);\n\n\u5c07\u4e32\u5217\u57e0\u8a2d\u5b9a\u70ba\u975e\u6b63\u898f\u6a21\u5f0f\noptions.c_lflag |= ~(ICANON | ECHO | ECHOE | ISIG);\n<\/code><\/pre>\n\n\n\n<p>c_lflag\u7684\u65d7\u6a19\u503c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>ISIG<\/td><td>When any of the characters INTR, QUIT, SUSP, or DSUSP are received, generate the corresponding signal.<\/td><\/tr><tr><td>ICANON<\/td><td>Enable canonical mode. This enables the special characters EOF, EOL, EOL2, ERASE, KILL, LNEXT, REPRINT, STATUS, and WERASE, and buffers by lines.<\/td><\/tr><tr><td>XCASE<\/td><td>(not in POSIX; not supported under Linux) If ICANON is also set, terminal is uppercase only. Input is converted to lowercase, except for characters preceded by \\. On output, uppercase characters are preceded by \\ and lowercase characters are converted to uppercase.<\/td><\/tr><tr><td>ECHO<\/td><td>Echo input characters.<\/td><\/tr><tr><td>ECHOE<\/td><td>If ICANON is also set, the ERASE character erases the preceding input character, and WERASE erases the preceding word.<\/td><\/tr><tr><td>ECHOK<\/td><td>If ICANON is also set, the KILL character erases the current line.<\/td><\/tr><tr><td>ECHONL<\/td><td>If ICANON is also set, echo the NL character even if ECHO is not set.<\/td><\/tr><tr><td>ECHOCTL<\/td><td>(not in POSIX) If ECHO is also set, ASCII control signals other than TAB, NL, START, and STOP are echoed as ^X, where X is the character with ASCII code 0x40 greater than the control signal. For example, character 0x08 (BS) is echoed as ^H.<\/td><\/tr><tr><td>ECHOPRT<\/td><td>(not in POSIX) If ICANON and IECHO are also set, characters are printed as they are being erased.<\/td><\/tr><tr><td>ECHOKE<\/td><td>(not in POSIX) If ICANON is also set, KILL is echoed by erasing each character on the line, as specified by ECHOE and ECHOPRT.<\/td><\/tr><tr><td>DEFECHO<\/td><td>(not in POSIX) Echo only when a process is reading.<\/td><\/tr><tr><td>FLUSHO<\/td><td>(not in POSIX; not supported under Linux) Output is being flushed. This flag is toggled by typing the DISCARD character.<\/td><\/tr><tr><td>NOFLSH<\/td><td>Disable flushing the input and output queues when generating the SIGINT, SIGQUIT and SIGSUSP signals.<\/td><\/tr><tr><td>TOSTOP<\/td><td>Send the SIGTTOU signal to the process group of a background process which tries to write to its controlling terminal.<\/td><\/tr><tr><td>PENDIN<\/td><td>(not in POSIX; not supported under Linux) All characters in the input queue are reprinted when the next character is read. (bash handles typeahead this way.)<\/td><\/tr><tr><td>IEXTEN<\/td><td>Enable implementation-defined input processing. This flag, as well as ICANON must be enabled for the special characters EOL2, LNEXT, REPRINT, WERASE to be interpreted, and for the IUCLC flag to be effective.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7279\u6b8a\u63a7\u5236\u5b57\u5143 c_cc[NCCS]<\/p>\n\n\n\n<p>c_cc \u9663\u5217\u6210\u54e1\u7528\u4f86\u5b9a\u7fa9\u652f\u63f4\u7684\u7279\u6b8a\u63a7\u5236\u5b57\u5143\uff0c\u53ca\u4e00\u4e9btimeout\u53c3\u6578\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5c07\u4e32\u5217\u57e0\u8a2d\u70ba\u975e\u6b63\u898f\u6a21\u5f0f\uff0c\u8b80\u53d6\u6642\u9593timeout\u8a2d\u70ba1\u79d2\u3002\nstruct termios options;\noptions.c_cflag |= (CLOCAL | CREAD);\noptions.c_lflag |= ~(ICANON | ECHO | ECHOE | ISIG);\noptions.c_oflag |= ~OPOST;\noptions.c_cc&#91;VMIN] = 0;\noptions.c_cc&#91;VTIME] = 10;<\/code><\/pre>\n\n\n\n<p>\u5c0d\u6b63\u898f\u6a21\u5f0f\uff0cc_cc\u9663\u5217\u7684\u5305\u62ec<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>\u9805\u76ee<\/td><td> \u6b63\u898f\u6a21\u4ee3\u78bc<\/td><td> \u975e\u6b63\u898f\u6a21\u5f0f\u4ee3\u78bc<\/td><\/tr><tr><td>\u7279\u6b8a\u63a7\u5236\u5b57\u5143<\/td><td>NCCS<\/td><td> NCCS <\/td><\/tr><tr><td>EOF\u5b57\u5143<\/td><td>VEOF<\/td><td><\/td><\/tr><tr><td>EOL\u5b57\u5143<\/td><td>VEOL<\/td><td><\/td><\/tr><tr><td>ERASE\u5b57\u5143<\/td><td>VERASE<\/td><td><\/td><\/tr><tr><td>INTR\u5b57\u5143<\/td><td>VINTR<\/td><td><\/td><\/tr><tr><td>KILL\u5b57\u5143<\/td><td>VKILL<\/td><td><\/td><\/tr><tr><td>QUIT\u5b57\u5143<\/td><td>VQUIT<\/td><td><\/td><\/tr><tr><td>KINTR\u5b57\u5143<\/td><td><\/td><td>VINTR<\/td><\/tr><tr><td>MIN\u5b57\u5143<\/td><td><\/td><td>VMIN<\/td><\/tr><tr><td>QUIT\u5b57\u5143<\/td><td><\/td><td>VQUIT<\/td><\/tr><tr><td>SUSP\u5b57\u5143<\/td><td>VSUSP<\/td><td> VSUSP <\/td><\/tr><tr><td>TIME\u5b57\u5143<\/td><td><\/td><td>VTIME<\/td><\/tr><tr><td>START\u5b57\u5143<\/td><td>VSTART<\/td><td> VSTART <\/td><\/tr><tr><td>STOP\u5b57\u5143<\/td><td>VSTOP<\/td><td>VSTOP<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u975e\u6b63\u898f\u6a21\u5f0f\u7684\u7279\u6b8a\u5b57\u5143TIME\u548cMIN\u5c0d\u65bc\u8f38\u5165\u5b57\u5143\u7684\u8655\u7406\u975e\u5e38\u91cd\u8981\uff0c\u6709\u4e0b\u52174\u7a2e\u7d44\u5408\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>\u7d44\u5408<\/td><td>\u8aaa\u660e<\/td><\/tr><tr><td>MIN=0,TIME=0<\/td><td>\u4ee5read()\u51fd\u6578\u8b80\u53d6\u4e32\u5217\u57e0\u5f8c\u7acb\u5373\u8fd4\u56de\uff0c\u82e5\u8b80\u53d6\u5230\u5b57\u5143\u5247\u50b3\u56de\u5b57\u5143\uff0c\u5426\u5247\u50b3\u56de0\u3002<\/td><\/tr><tr><td>MIN=0,TIME&gt;0<\/td><td>\u4ee5read()\u51fd\u6578\u8b80\u53d6\u4e32\u5217\u57e0\u5f8c\uff0c\u6703\u5728TIME\u6642\u9593\u5167\u7b49\u5f85\u7b2c\u4e00\u500b\u5b57\u5143\u3002\u82e5\u6709\u5b57\u5143\u50b3\u5165\u6216\u6642\u9593\u5230\uff0c\u7acb\u5373\u8fd4\u56de\u3002\u82e5\u8b80\u53d6\u5230\u5b57\u5143\u5247\u50b3\u56de\u5b57\u5143\uff0c\u5426\u5247\u50b3\u56de0\u3002<\/td><\/tr><tr><td>MIN&gt;0,TIME=0<\/td><td>\u4ee5read()\u51fd\u6578\u8b80\u53d6\u4e32\u5217\u57e0\u5f8c\u6703\u7b49\u5f85\u8cc7\u6599\u50b3\u5165\uff0c\u82e5\u6709MIN\u500b\u5b57\u5143\u53ef\u8b80\u53d6\uff0c\u50b3\u56de\u8b80\u53d6\u7684\u5b57\u5143\u6578\u3002<\/td><\/tr><tr><td>MIN&gt;0,TIME&gt;0<\/td><td>\u4ee5read()\u51fd\u6578\u8b80\u53d6\u4e32\u5217\u57e0\u5f8c\uff0c\u6703\u7b49\u5f85\u8cc7\u6599\u7684\u50b3\u5165\u3002\u82e5\u6709MIN\u500b\u5b57\u5143\u53ef\u8b80\u53d6\u6642\uff0c\u50b3\u56de\u8b80\u53d6\u5230\u7684\u5b57\u5143\u6578\u3002\u82e5TIME\u7684\u6642\u9593\u5230\uff0c\u5247read()\u50b3\u56de0\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u7279\u6b8a\u63a7\u5236\u5b57\u5143\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>VINTR<\/td><td>(003, ETX, Ctrl-C, or also 0177, DEL, rubout) Interrupt character. Send a SIGINT signal. Recognized when ISIG is set, and then not passed as input.<\/td><\/tr><tr><td>VQUIT<\/td><td>(034, FS, Ctrl-\\) Quit character. Send SIGQUIT signal. Recognized when ISIG is set, and then not passed as input.<\/td><\/tr><tr><td>VERASE<\/td><td>(0177, DEL, rubout, or 010, BS, Ctrl-H, or also #) Erase character. This erases the previous not-yet-erased character, but does not erase past EOF or beginning-of-line. Recognized when ICANON is set, and then not passed as input.<\/td><\/tr><tr><td>VKILL<\/td><td>(025, NAK, Ctrl-U, or Ctrl-X, or also @) Kill character. This erases the input since the last EOF or beginning-of-line. Recognized when ICANON is set, and then not passed as input.<\/td><\/tr><tr><td>VEOF<\/td><td>(004, EOT, Ctrl-D) End-of-file character. More precisely: this character causes the pending tty buffer to be sent to the waiting user program without waiting for end-of-line. If it is the first character of the line, the read() in the user program returns 0, which signifies end-of-file. Recognized when ICANON is set, and then not passed as input.<\/td><\/tr><tr><td>VMIN<\/td><td>Minimum number of characters for non-canonical read.<\/td><\/tr><tr><td>VEOL<\/td><td>(0, NUL) Additional end-of-line character. Recognized when ICANON is set.<\/td><\/tr><tr><td>VTIME<\/td><td>Timeout in deciseconds for non-canonical read.<\/td><\/tr><tr><td>VEOL2<\/td><td>(not in POSIX; 0, NUL) Yet another end-of-line character. Recognized when ICANON is set.<\/td><\/tr><tr><td>VSWTCH<\/td><td>(not in POSIX; not supported under Linux; 0, NUL) Switch character. (Used by shl only.)<\/td><\/tr><tr><td>VSTART<\/td><td>(021, DC1, Ctrl-Q) Start character. Restarts output stopped by the Stop character. Recognized when IXON is set, and then not passed as input.<\/td><\/tr><tr><td>VSTOP<\/td><td>(023, DC3, Ctrl-S) Stop character. Stop output until Start character typed. Recognized when IXON is set, and then not passed as input.<\/td><\/tr><tr><td>VSUSP<\/td><td>(032, SUB, Ctrl-Z) Suspend character. Send SIGTSTP signal. Recognized when ISIG is set, and then not passed as input.<\/td><\/tr><tr><td>VDSUSP<\/td><td>(not in POSIX; not supported under Linux; 031, EM, Ctrl-Y) Delayed suspend character: send SIGTSTP signal when the character is read by the user program. Recognized when IEXTEN and ISIG are set, and the system supports job control, and then not passed as input.<\/td><\/tr><tr><td>VLNEXT<\/td><td>(not in POSIX; 026, SYN, Ctrl-V) Literal next. Quotes the next input character, depriving it of a possible special meaning. Recognized when IEXTEN is set, and then not passed as input.<\/td><\/tr><tr><td>VWERASE<\/td><td>(not in POSIX; 027, ETB, Ctrl-W) Word erase. Recognized when ICANON and IEXTEN are set, and then not passed as input.<\/td><\/tr><tr><td>VREPRINT<\/td><td>(not in POSIX; 022, DC2, Ctrl-R) Reprint unread characters. Recognized when ICANON and IEXTEN are set, and then not passed as input.<\/td><\/tr><tr><td>VDISCARD<\/td><td>(not in POSIX; not supported under Linux; 017, SI, Ctrl-O) Toggle: start\/stop discarding pending output. Recognized when IEXTEN is set, and then not passed as input.<\/td><\/tr><tr><td>VSTATUS<\/td><td>(not in POSIX; not supported under Linux; status request: 024, DC4, Ctrl-T).<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u4e00\u822c\u7c21\u6613\u8a2d\u5b9a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int OpenTTY()\n{\n  struct termios Opt;\n\n  TTYfd = open(TTYDevName, O_RDWR | O_NOCTTY | O_NONBLOCK);\n  if ( TTYfd &lt; 0){\n    printf(\"open TTY Dev Name(%s) Fail\\n\", TTYDevName);\n    return FALSE;\n  }\n  tcgetattr(TTYfd, &amp;Opt);\n  Opt.c_iflag = 0; \n  Opt.c_lflag = 0; \n  Opt.c_oflag = 0;\n  memset(Opt.c_cc, 0, NCCS);\n  Opt.c_cc&#91;VTIME]\t\t= 1; \n  cfsetispeed(&amp;Opt, B115200);\n  cfsetospeed(&amp;Opt, B115200);\n  if (tcsetattr(TTYfd, TCSANOW, &amp;Opt) != 0){\n    printf(\"TTY Dev Baudrate Fail\\n\");\n    return FALSE;\n  } \n  tcflush(TTYfd, TCIOFLUSH);\n  return TRUE;\n}<\/code><\/pre>\n\n\n\n<p>\u7d42\u7aef\u6a5f\u76f8\u95dc\u51fd\u6578<\/p>\n\n\n\n<p>tcgetattr()\u7528\u4f86\u53d6\u5f97\u76ee\u524d\u7684\u4e32\u5217\u57e0\u53c3\u6578\u503c\u3002<br>int tcgetattr(int fd, struct termios *fp);<br>\u8aaa\u660e\uff1a<br>    tcgetattr()\u53d6\u5f97\u6a94\u6848\u63cf\u8ff0\u5b50fd\u5f8c\uff0c\u5c07\u5176\u5b58\u5165tp\u6240\u6307\u5411\u7684termios\u8cc7\u6599\u7d50\u69cb\u3002<br>\u50b3\u56de\u503c\uff1a<br>    \u6210\u529f\uff1a0\u5931\u6557\uff1a-1<br><br>tcsetattr()\u7528\u4f86\u8a2d\u5b9a\u4e32\u5217\u57e0\u53c3\u6578\u503c\u3002<br>|int tcsetattr(int fd, int action, const struct termios *tp);<br>\u8aaa\u660e\uff1a<br>    tcsetattr()\u57f7\u884c\u5f8c\u4f7f\u7528fp\u6307\u5411\u7684termios\u8cc7\u6599\u7d50\u69cb\uff0c\u91cd\u65b0\u8a2d\u5b9a\u6a94\u6848\u63cf\u8ff0\u5b50fd\uff0c\u5176\u4e2d\u5f15\u6578action\u53ef\u4ee5\u662f\u4e0b\u5217\u7684\u503c<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>action\u503c<\/td><td>\u8aaa\u660e<\/td><\/tr><tr><td>TCSANOW<\/td><td>\u7acb\u5373\u5c07\u503c\u6539\u8b8a<\/td><\/tr><tr><td>TCSADRAIN<\/td><td>\u7576\u76ee\u524d\u8f38\u51fa\u5b8c\u6210\u6642\uff0c\u5c07\u503c\u6539\u8b8a<\/td><\/tr><tr><td>TCSAFLUSH<\/td><td>\u7576\u76ee\u524d\u8f38\u51fa\u5b8c\u6210\u6642\uff0c\u5c07\u503c\u6539\u8b8a\uff1b\u4e26\u6368\u68c4\u76ee\u524d\u6240\u6709\u7684\u8f38\u5165\u3002<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>cfgetispeed()\u50b3\u56de\u4e32\u5217\u57e0\u7684\u8f38\u5165\u901f\u7387\uff1a<br>int cfgetispeed(struct termios *tp);<br>\u8aaa\u660e\uff1a<br>    \u5176\u4e2dtp\u70ba\u88ab\u8655\u7406\u7684termios\u7d50\u69cb\u3002<br><br>cgsetispeed()\u8a2d\u5b9a\u4e32\u5217\u57e0\u7684\u8f38\u5165\u901f\u5ea6\u3002<br>int cfsetispeed(struct termios *tp, speed_t speed);<br>\u8aaa\u660e\uff1a<br>    \u5176\u4e2dtp\u70ba\u88ab\u8655\u7406\u7684termios\u7d50\u69cb\uff0cspeed\u70ba\u9b91\u7387\uff0c\u53ef\u4ee5\u662f\u4ee5\u4e0b\u7684\u4e00\u500b\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>speed\u503c<\/td><td>speed\u503c<\/td><\/tr><tr><td>B0<\/td><td>B1800<\/td><\/tr><tr><td>B50<\/td><td>B2400<\/td><\/tr><tr><td>B75<\/td><td>B4800<\/td><\/tr><tr><td>B110<\/td><td>B9600<\/td><\/tr><tr><td>B134<\/td><td>B19200<\/td><\/tr><tr><td>B150<\/td><td>B38400<\/td><\/tr><tr><td>B200<\/td><td>B57600<\/td><\/tr><tr><td>B300<\/td><td>B115200<\/td><\/tr><tr><td>B600<\/td><td>B230400<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>cfgetospeed()\u50b3\u56de\u4e32\u5217\u57e0\u7684\u8f38\u51fa\u901f\u5ea6\u3002<br>int cfgetospeed(struct *tp);\u5176\u4e2dtp\u70ba\u88ab\u8655\u7406\u7684termios\u7d50\u69cb\u3002<br><br>cfsetospeed()\u8a2d\u5b9a\u4e32\u5217\u57e0\u7684\u8f38\u51fa\u901f\u5ea6\u3002<br>int cfsetospeed(struct termios *tp, speed_t speed)<br>\u8aaa\u660e\uff1a<br>    \u5176\u4e2dtp\u70ba\u88ab\u8655\u7406\u7684termios\u7d50\u69cb\uff0cspeed\u70ba\u9b91\u7387\u3002<br><br>tcdrain()\u7b49\u5f85\u6240\u6709\u8f38\u51fa\u5beb\u5230\u4e32\u5217\u57e0\u5f8c\uff0c\u624d\u8fd4\u56de\u547c\u53eb\u7684\u7a0b\u5f0f\u3002<br>tcdrain(int fd);<br>\u8aaa\u660e\uff1a<br>    \u5176\u4e2dfd\u70ba\u88ab\u8655\u7406\u7684\u4e32\u5217\u57e0\u3002<br><br>tcflush()\u6e05\u9664\u6240\u6709\u4f47\u5217\u5728\u4e32\u5217\u57e0\u7684\u8f38\u5165\u548c\u8f38\u51fa\u3002<br>int tcflush(int fd, int queue);<br>\u8aaa\u660e\uff1a<br>    \u5176\u4e2dfd\u70ba\u88ab\u8655\u7406\u7684\u4e32\u5217\u57e0\uff0c\u5f15\u6578queue\u70ba\u4e0b\u5217\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>queue\u503c<\/td><td>\u8aaa\u660e<\/td><\/tr><tr><td>TCIFLUSH<\/td><td>\u6e05\u9664\u8f38\u5165<\/td><\/tr><tr><td>TCOFLUSH<\/td><td>\u6e05\u9664\u8f38\u51fa<\/td><\/tr><tr><td>TCIOFLUSH<\/td><td>\u6e05\u9664\u8f38\u5165\u548c\u8f38\u51fa<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>tcflow()\u555f\u52d5\u6216\u505c\u6b62\u4e32\u5217\u57e0\u7684\u8cc7\u6599\u50b3\u9001\u6216\u63a5\u6536\u3002<br>int tcflow(int fd, int action);<br>\u8aaa\u660e\uff1a<br>    \u5176\u4e2dfd\u70ba\u88ab\u8655\u7406\u7684\u4e32\u5217\u57e0\uff0c\u5f15\u6578action\u70ba\u4e0b\u5217\u503c\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-stripes\"><table><tbody><tr><td>action\u503c<\/td><td>\u8aaa\u660e<\/td><\/tr><tr><td>TCOON<\/td><td>\u555f\u52d5\u8f38\u51fa<\/td><\/tr><tr><td>TCOOFF<\/td><td>\u505c\u6b62\u8f38\u51fa<\/td><\/tr><tr><td>TCION<\/td><td>\u555f\u52d5\u8f38\u5165<\/td><\/tr><tr><td>TCIOFF<\/td><td>\u505c\u6b62\u8f38\u5165<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u53c3\u8003\u8cc7\u6599<\/p>\n\n\n\n<p><a rel=\"noreferrer noopener\" href=\"http:\/\/shyuanliang.blogspot.com\/2010\/09\/linux-rs-232.html\" target=\"_blank\">http:\/\/shyuanliang.blogspot.com\/2010\/09\/linux-rs-232.html<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/blog.mbedded.ninja\/programming\/operating-systems\/linux\/linux-serial-ports-using-c-cpp\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/blog.mbedded.ninja\/programming\/operating-systems\/linux\/linux-serial-ports-using-c-cpp\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u7d42\u7aef\u6a5f\u4ecb\u9762 \u7d42\u7aef\u6a5f\u4ecb\u9762\u53c8\u7a31\u70baTTY\u4ecb\u9762\uff0c\u7528\u4f86\u8b93Linux\u7cfb\u7d71\u900f\u904eRS-232\u4e32\u5217\u57e0\u9023\u63a5\u8a2d\u5099\u3002\u7d42\u7aef\u6a5f\u4ecb\u9762\u6709\u4e8c\u7a2e\u6a21 &#8230; <a title=\"Linux RS232 \u8a2d\u8a08\" class=\"read-more\" href=\"https:\/\/www.yaj.com.tw\/wordpress\/?p=307\" aria-label=\"\u95b1\u8b80\u3008Linux RS232 \u8a2d\u8a08\u3009\u5168\u6587\">\u95b1\u8b80\u5168\u6587<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[17,9],"class_list":["post-307","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-linux","tag-openwrt"],"_links":{"self":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/307","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=307"}],"version-history":[{"count":11,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions"}],"predecessor-version":[{"id":403,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/307\/revisions\/403"}],"wp:attachment":[{"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yaj.com.tw\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}