列奥纳多串行通讯

ale*_*z78 0 serial-port arduino

我编写了这个示例草图来测试与 Arduino Leonardo 的串行通信(在 Windows 7 上使用 Arduino IDE 1.0.5):

int delayTime = 10000;
long lastExec = 0;

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop()
{
  long t = millis();
  if (t - lastExec >= delayTime) {
    if(Serial.available() > 0){
      Serial.println('Hello world');
    }  
    lastExec = t;
  }
}
Run Code Online (Sandbox Code Playgroud)

选择的串行端口似乎工作,因为草图上传正确。

但是我在串行监视器窗口中没有得到任何东西。为什么?

lad*_*las 6

您首先需要向 Arduino 发送一个字符。

因为你有if(Serial.available() > 0)Serial.println("Hello World");除非Serial buffer.

您可能还想减少delayTime很长的时间。

总之,您可以通过在串行监视器中输入一些内容来尝试:

void setup()
{
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
}

void loop()
{
  if(Serial.available() > 0){
    Serial.println('Hello world');
  }  
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你!:)