arduino 中print()的使用方法
(2012-09-05 13:25:56)
标签:
arduinoit |
Serial.print(data)
Description描述
Prints data to the serial port.
从串行端口输出数据。
Parameter参数
data: all integer types, including char; truncates floating
point numbers to integers
数据:所有的整数类型,包括char; 截去浮点的整数。
Syntax语法
This command can take many forms:
这个命令有多种语法形式。
Serial.print(b) with no format specified,
prints b as a decimal number in an ASCII string. For example,
Serial.print(b) 不连接其它格式,以十进制浮点形式输出b的ASCII值。例如:
int b = 79; Serial.print(b);
prints the ASCII string "79".
输出 ASCII字符串“79”
Serial.print(b, DEC) prints b as a decimal
number in an ASCII string. For example,
Serial.print(b, DEC) 以十进制浮点形式输出b的ASCII值。例如:
int b = 79; Serial.print(b, DEC);
prints the string "79".
输出字符串“79”
Serial.print(b, HEX) prints b as a hexadecimal
number in an ASCII string. For example,
Serial.print(b, HEX) 以十六进制形式输出b的ASCII值。例如:
int b = 79; Serial.print(b, HEX);
prints the string "4F".
输出字符串“4F”
Serial.print(b, OCT) prints b as an octal
number in an ASCII string. For example,
Serial.print(b, OCT) 以八进制形式输出b的ASCII值。例如:
int b = 79; Serial.print(b, OCT);
prints the string "117".
输出字符串”117”
Serial.print(b, BIN) prints b as a binary
number in an ASCII string. For example,
Serial.print(b, BIN)以二进制形式输出b的ASCII值。例如:
int b = 79; Serial.print(b, BIN);
prints the string "1001111".
输出字符串“1001111”
Serial.print(b, BYTE) prints b as a single
byte. For example,
Serial.print(b, BYTE) 以单个字节输出b。例如:
int b = 79; Serial.print(b, BYTE);
returns the string "O", which is the ASCII character represented
by the value 79. For more information see the ASCII
table.
返回字符串“O” , 其 ASCII编码值为79。更多的资料请见 ASCII table。
Serial.print(str) if str is a string or an
array of chars, prints str as an ASCII string. For example,
Serial.print(str)如果 str是一个字符串或数组,输出整个 str的
ASCII编码字符串。例如:
Serial.print("Hello World!");
prints the string "Hello World!".
输出字符串 "Hello World!"
Parameters参数
b: the byte to print, or
b:需要输出的字节。
str: the string to print
str:需要输出的字符串。
Returns返回
None 无
Example:范例
int analogValue = 0; // variable to hold the analog value
// 需要保留的模拟值 void setup() { // open the serial port at 9600 bps:
//在9600 bps打开串行端口: Serial.begin(9600); } void loop() { // read the analog input on pin 0:
// 在0号模拟输入插口读取值 analogValue = analogRead(0); // print it out in many formats:
// 以多种格式输出 Serial.print(analogValue); // print as an ASCII-encoded decimal
// 以ASCII编码十进制浮点值输出
Serial.print("\t"); // print a tab character
// 输出TAB
Serial.print(analogValue, DEC); // print as an ASCII-encoded decimal
// 以ASCII编码十进制浮点值输出 Serial.print("\t"); // print a tab character
// 输出TAB Serial.print(analogValue, HEX); // print as an ASCII-encoded hexadecimal
// 以ASCII编码十六进制输出 Serial.print("\t"); // print a tab character
// 输出TAB Serial.print(analogValue, OCT); // print as an ASCII-encoded octal
// 以ASCII编码八进制输出 Serial.print("\t"); // print a tab character
// 输出TAB Serial.print(analogValue, BIN); // print as an ASCII-encoded binary
// 以ASCII编码二进制输出 Serial.print("\t"); // print a tab character
// 输出TAB Serial.print(analogValue/4, BYTE); // print as a raw byte value (divide the // value by 4 because analogRead() returns numbers // from 0 to 1023, but a byte can only hold values // up to 255)
// 输出原始数据(取其1/4,因为analogRead()返回的值为0-1023,但1byte仅能保存到255的值) Serial.print("\t"); // print a tab character
// 输出TAB Serial.println(); // print a linefeed character
// 输出换行符 // delay 10 milliseconds before the next reading:
// 在下次读取之前延迟10毫秒 delay(10); }
Programming Tips / Known Issues
Serial.print() truncates floats into integers, losing any
fractional values. It is sometimes useful to multiply your float by
a power of ten, to preserve some of this fractional
resolution.
Serial.print()丢弃浮点保留整数,这将损失小数值。有时你可以将浮点值乘以10,即可保护这些浮点值。
Be careful about doing math inside the brackets e.g.
在括号内进行数学计算是需要注意,例如:
Serial.print(x-2, DEC);
The unsigned char data type, and byte data type will yield
incorrect results and act as though they are signed types i.e. type
char.
无符号字符数据类型和二进制数据类型将产生错误的计算结果,会当成有符号的数据类型进行计算。
The Serial.Print function puts data into a buffer. It will wait
for one character to send, before going on to the next character.
However the function returns before sending the last
character.
Serial.Print函数把数据储存至缓冲器,在处理下一个字符前将等待至上一个字符已经被发送,无论如何,这个函数将在发送最后一个字符之前返回。
See also
- ASCII chart
- Serial.begin(speed)
- Serial.available()
- Serial.read()
- Serial.println(data)
http://www.burncg.cn/burnblog/article.asp?id=47