nt 的 有趣的串口--之超级终端的玩法-2[转]
5. ASC II码表和转义字符
================================
我们输入的显示字符都是ASC II的标准,了解这个有必要。对于一些特殊的指令,ASC II则用转义字
符更为方便。
------------
Decimal Octal Hex Binary Value
10 08 16 02
------- ----- *--- ------ -----
000 000 000 00000000 NUL (Null char.)
001 001 001 00000001 SOH (Start of Header)发送文件首
002 002 002 00000010 STX (Start of Text)文本开始
003 003 003 00000011 ETX (End of Text)文本尾
004 004 004 00000100 EOT (End of Transmission)发送结束
005 005 005 00000101 ENQ (Enquiry)
006 006 006 00000110 ACK (Acknowledgment)确认
007 007 007 00000111 BEL (Bell)蜂鸣
008 010 008 00001000 BS (Backspace)退格
009 011 009 00001001 HT (Horizontal Tab)
010 012 00A 00001010 LF (Line Feed)换行
011 013 00B 00001011 VT (Vertical Tab)
012 014 00C 00001100 FF (Form Feed)换页/清屏
013 015 00D 00001101 CR (Carriage Return)回车
014 016 00E 00001110 SO (Shift Out)SHIFT 松开
015 017 00F 00001111 SI (Shift In)按下
016 020 010 00010000 DLE (Data Link Escape)清除
017 021 011 00010001 DC1 (XON) (Device Control 1)
018 022 012 00010010 DC2 (Device Control 2)
019 023 013 00010011 DC3 (XOFF)(Device Control 3)
020 024 014 00010100 DC4 (Device Control 4)
021 025 015 00010101 NAK (Negative Acknowledgement)
022 026 016 00010110 SYN (Synchronous Idle)
023 027 017 00010111 ETB (End of Trans. Block)
024 030 018 00011000 CAN (Cancel)
025 031 019 00011001 EM (End of Medium)
026 032 01A 00011010 SUB (Substitute)
027 033 01B 00011011 ESC (Escape)退出
028 034 01C 00011100 FS (File Separator)
029 035 01D 00011101 GS (Group Separator)
030 036 01E 00011110 RS (Request to Send/Record Separator)
031 037 01F 00011111 US (Unit Separator)
032 040 020 00100000 SP (Space)空格
033 041 021 00100001 ! (exclamation mark)
034 042 022 00100010 " (double quote)
035 043 023 00100011 # (number sign)
036 044 024 00100100 $ (dollar sign)
037 045 025 00100101 % (percent)
038 046 026 00100110 & (ampersand)
039 047 027 00100111 ' (single quote)
040 050 028 00101000 ( (left/opening parenthesis)
041 051 029 00101001 ) (right/closing parenthesis)
042 052 02A 00101010 * (asterisk)
043 053 02B 00101011 + (plus)
044 054 02C 00101100 , (comma)
045 055 02D 00101101 - (minus or dash)
046 056 02E 00101110 . (dot)
047 057 02F 00101111 / (forward slash)
048 060 030 00110000 0
049 061 031 00110001 1
050 062 032 00110010 2
051 063 033 00110011 3
052 064 034 00110100 4
053 065 035 00110101 5
054 066 036 00110110 6
055 067 037 00110111 7
056 070 038 00111000 8
057 071 039 00111001 9
058 072 03A 00111010 : (colon)
059 073 03B 00111011 ; (semi-colon)
060 074 03C 00111100 < (less than)
061 075 03D 00111101 = (equal sign)
062 076 03E 00111110 > (greater than)
063 077 03F 00111111 ? (question mark)
064 100 040 01000000 @ (AT symbol)
065 101 041 01000001 A
066 102 042 01000010 B
067 103 043 01000011 C
068 104 044 01000100 D
069 105 045 01000101 E
070 106 046 01000110 F
071 107 047 01000111 G
072 110 048 01001000 H
073 111 049 01001001 I
074 112 04A 01001010 J
075 113 04B 01001011 K
076 114 04C 01001100 L
077 115 04D 01001101 M
078 116 04E 01001110 N
079 117 04F 01001111 O
080 120 050 01010000 P
081 121 051 01010001 Q
082 122 052 01010010 R
083 123 053 01010011 S
084 124 054 01010100 T
085 125 055 01010101 U
086 126 056 01010110 V
087 127 057 01010111 W
088 130 058 01011000 X
089 131 059 01011001 Y
090 132 05A 01011010 Z
091 133 05B 01011011 [ (left/opening bracket)
092 134 05C 01011100 \ (back slash)
093 135 05D 01011101 ] (right/closing bracket)
094 136 05E 01011110 ^ (caret/cirumflex)
095 137 05F 01011111 _ (underscore)
096 140 060 01100000 `
097 141 061 01100001 a
098 142 062 01100010 b
099 143 063 01100011 c
100 144 064 01100100 d
101 145 065 01100101 e
102 146 066 01100110 f
103 147 067 01100111 g
104 150 068 01101000 h
105 151 069 01101001 i
106 152 06A 01101010 j
107 153 06B 01101011 k
108 154 06C 01101100 l
109 155 06D 01101101 m
110 156 06E 01101110 n
111 157 06F 01101111 o
112 160 070 01110000 p
113 161 071 01110001 q
114 162 072 01110010 r
115 163 073 01110011 s
116 164 074 01110100 t
117 165 075 01110101 u
118 166 076 01110110 v
119 167 077 01110111 w
120 170 078 01111000 x
121 171 079 01111001 y
122 172 07A 01111010 z
123 173 07B 01111011 { (left/opening brace)
124 174 07C 01111100 | (vertical bar)
125 175 07D 01111101 } (right/closing brace)
126 176 07E 01111110 ~ (tilde)
127 177 07F 01111111 DEL (delete)前32个控制字符的详细解释:
NUL (null)
SOH (start of heading)
STX (start of text)
ETX (end of text)
EOT (end of transmission) - Not the same as
ETBENQ (enquiry)ACK (acknowledge)
BEL (bell) - Caused teletype machines to ring a bell.
Causes a beep in many common terminals and terminal emulation programs.
BS (backspace) - Moves the cursor (or print head) move backwards (left)
one space.
TAB (horizontal tab) - Moves the cursor (or print head) right to the next tab stop.
The spacing of tab stops is dependent on the output device, but is often either 8 or 10.
LF (NL line feed, new line) - Moves the cursor (or print head) to a new line. On Unix
systems, moves to a new line AND all the way to the left.
VT (vertical tab)
FF (form feed) - Advances paper to the top of the next page (if the output device is a
printer).
CR (carriage return) - Moves the cursor all the way to the left, but does not advance to
the next line.
SO (shift out) - Switches output device to alternate character set.
SI (shift in) - Switches output device back to default character set.DLE (data link
escape)
DC1 (device control 1)
DC2 (device control 2)
DC3 (device control 3)
DC4 (device control 4)
NAK (negative acknowledge)
SYN (synchronous idle)
ETB (end of transmission block) - Not the same as
EOTCAN (cancel)EM (end of medium)
SUB (substitute)ESC (escape)
FS (file separator)
GS (group separator)
RS (record separator)
US (unit separator)
--------
常用的转义字符
在windows自带的超级终端中,如何清屏?
--通过发送0x0C(12)即可实现清屏。
--在“输入字符串”内容中输入 /x0c
\0 :字符串结束标志;
--- 在上面的程序里找到 while(*str != '\0') ,您就会知道 \0 的功能了。它可以表示字符串的结束
,您可以不用担心指针会溢出。
\n :换行(asc II码为10);
--- 一般和 \r 配合使用以代表回车。因为 \n 只是表示换行而并不让光标回到行首, \r 是实现了回行
首的功能。
\t :横向跳格;
\b :退格;
\r :回车(ascⅱ码为13);
--- 说过了。
\f :走纸换页;
\\ :字符\(ascⅱ码为92);
--- \ 是转义字符的表示符,当只想打一个 \ 时就可以打两个。
\' :单引号;
--- ' 在C里有特殊用法,所以这个表示只打印这个符号。
'\"':双引号;
---同上
\d05:用8进制表示字符;
\x1c:用16进制表示字符
--- 有一些功能没有转义字符,我们就可以用这个指令直接表示指令。\x1c 相当于发送 0x1c 对应的ASC
码功能。
6. 关于颜色
================================
在 ANSI 兼容终端里,可以用彩色显示文本而不仅仅是黑白。但是我们自己编写的程序能否输出彩色的字
符呢?当然答案是肯定的。下面的语句就输出高亮的黑色背景的绿色字。
printf("\033[1;40;32m good!!! \033[0m Hello,NSFocus \n");
\033 声明了转义序列的开始,然后是 [ 开始定义颜色。后面的 1 定义了高亮显示字符。然后是背景颜
色,这里面是40,表示黑色背景。接着是前景颜色,这里面是32,表示绿色。我们用 \033[0m 关闭转义
序列, \033[0m 是终端默认颜色。
通过上面的介绍,就知道了如何输出彩色字符了。因此,我就不再多说了。下面是对于彩色字符颜色
的一些定义:
前景 背景 颜色
---------------------------------------
30 40 黑色
31 41 紅色
32 42 綠色
33 43 黃色
34 44 藍色
35 45 紫紅色
36 46 青藍色
37 47 白色
代码 意义
-------------------------
0 终端默认设置(一般的默认为黑底白字)
1 高亮显示
4 使用下划线
5 闪烁
7 反白显示
8 不可见
7. 尾声
================================
在嵌入式系统开发上,超级终端是相当有用的家伙,许多DEBUG程序都是用串口的,这里了解的更多
的是ASC II码的一些特点。而了解这一些,使我们有许多想像。我们可以用超级终端来代替VB语言写的上
位机程序,电脑上不用安装,只要有串口就行。可以实现数据采集的电脑显示,或是用电脑串口控制下位
机的设备。任君想来!
文中如有错误欢迎批评、指正。一切为学,多多交流。
转载请注明源地。
杜洋 2006-11-14 深圳5楼