SoftwareSerial: listen()

[SoftwareSerial]

描述

Enables the selected software serial port to listen. Only one software serial port can listen at a time; data that arrives for other ports will be discarded. Any data already received is discarded during the call to listen() (unless the given instance is already listening).

語法

// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

mySerial.listen()

參數

mySerial: the name of the instance to listen

回傳

None

範例

#include <SoftwareSerial.h>
 
// software serial : TX = digital pin 10, RX = digital pin 11
SoftwareSerial portOne(10, 11);
 
// software serial : TX = digital pin 8, RX = digital pin 9
SoftwareSerial portTwo(8, 9);
 
void setup()
{
  // Start the hardware serial port
  Serial.begin(9600);
 
  // Start both software serial ports
  portOne.begin(9600);
  portTwo.begin(9600);
 
}
 
void loop()
{
  portOne.listen();
 
  if (portOne.isListening()) {
    Serial.println("Port One is listening!"); 
  } else {
    Serial.println("Port One is not listening!"); 
  }
 
  if (portTwo.isListening()) {
    Serial.println("Port Two is listening!"); 
  } else {
    Serial.println("Port Two is not listening!"); 
  }
 
}

參考


函式庫參考主頁面

86Duino 參考的文本是根據 Creative Commons Attribution-ShareAlike 3.0 License,部分文本是從 the Arduino reference 修改的。 參考中的代碼示例已發佈到公共領域。

發表評論

上部へスクロール