简体   繁体   English

Arduino 不总是收到来自 Visual Studio 的消息

[英]Arduino does not always receive message from Visual Studio

I have an Arduino Uno which in the Arduino IDE runs code which should receive a message from Visual Studio via Serial Port.我有一个 Arduino Uno,它在 Arduino IDE 运行的代码应该通过串行端口从 Visual Studio 接收消息。 If the message is "ON" then lights are on if the message is "OF" then the lights are off.如果消息为“ON”,则灯亮;如果消息为“OF”,则灯熄灭。

Sometime it works but sometimes it does not and I don't understand why that is the case.有时它有效,但有时却无效,我不明白为什么会这样。

Here is my Arduino code:这是我的 Arduino 代码:

   if (Serial.available())
   {
   
    
    message = Serial.readStringUntil('\n');// reading the message from c#

    
    
    if (message == "ON") // if message is on we go to hazard mode
    {
      state = 1;
      alarmOn = true;
    }
    else if ( message == "OFF") // otherwise we are in the normal mode
    {
      led_state = LOW;
      state = 0;
      alarmOn = false;
    }
  }

Here is my code from Visual Studio这是我在 Visual Studio 中的代码

            toggleLEd = !toggleLEd;
            string text = string.Empty;
            if (toggleLEd == true)
            {
                serialPort1.WriteLine("ON");
                Console.WriteLine("ON");
                text = "AlarmOn";
                lbDisplay.Items.Add(text);
            }
            else if(toggleLEd==false)
            {
                serialPort1.WriteLine("OFF");
                Console.WriteLine("OFF");
                text = "AlarmOff";
                lbDisplay.Items.Add(text);

            }
  1. is the data transfer rate the same?数据传输速率是否相同?
  2. are you checking whether the port is open?你在检查端口是否打开? serialPort1.WriteLine("OFF"); serialPort1.WriteLine("OFF");
  3. add it to the Arduino script Serial.println(message);将它添加到 Arduino 脚本 Serial.println(message); read in VS from serial port and it will be clear to you whether the message was delivered or not从串口读入VS,消息是否送达你就清楚了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM