available()
描述
透过串列埠,取得可供读取的 byte (字元) 个数。这些可供读取的资料已经被串列埠读取进来并且储存在记忆体中 (最多 4096 bytes)。available()
是继承于 Stream 类别。
语法
适用所有板子:
Serial.available()
Serial1.available()
适用 86Duino ONE:
Serial2.available()
Serial3.available()
Serial485.available()
适用 86Duino EduCake:
Serial2.available()
Serial3.available()
Serial232.available()
参数
回传
可供读取的 byte 数
范例
int incomingByte = 0; // 给串列资料使用的变数 void setup() { Serial1.begin(9600); // 打开串列埠,并且设定鲍率为 9600 } void loop() { // 只在有收到资料的时候送值 if (Serial1.available() > 0) { // 读取收到的值 incomingByte = Serial1.read(); // 告诉对方说你收到了什么 Serial1.print("I received: "); Serial1.println(incomingByte, DEC); } }
其他范例:
void setup() { Serial.begin(9600); Serial1.begin(9600); } void loop() { // 从 port 0 收资料,从 port 1 送资料: if (Serial.available()) { int inByte = Serial.read(); Serial1.print(inByte, BYTE); } // 从 port 1 收资料,从 port 0 送资料: if (Serial1.available()) { int inByte = Serial1.read(); Serial.print(inByte, BYTE); } }
See also
- Serial
- begin()
- end()
- find()
- findUntil()
- flush()
- parseFloat()
- parseInt()
- peek()
- print()
- println()
- read()
- readBytes()
- readBytesUntil()
- setTimeout()
- write()
- serialEvent()
- Stream.available()
The text of the 86Duino reference is a modification of the Arduino reference, and is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.