[英]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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.