Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Arduino并不总是从Visual Studio接收消息_C#_Arduino_Serial Port_Arduino Uno - Fatal编程技术网

C# Arduino并不总是从Visual Studio接收消息

C# Arduino并不总是从Visual Studio接收消息,c#,arduino,serial-port,arduino-uno,C#,Arduino,Serial Port,Arduino Uno,我有一个Arduino Uno,它在Arduino IDE中运行代码,该代码应该通过串行端口从Visual Studio接收消息。如果信息为“开”,则灯亮;如果信息为“开”,则灯熄灭 有时有效,但有时无效,我不明白为什么会这样 这是我的Arduino代码: if (Serial.available()) { message = Serial.readStringUntil('\n');// reading the message from c#

我有一个Arduino Uno,它在Arduino IDE中运行代码,该代码应该通过串行端口从Visual Studio接收消息。如果信息为“开”,则灯亮;如果信息为“开”,则灯熄灭

有时有效,但有时无效,我不明白为什么会这样

这是我的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;
    }
  }
这是我来自VisualStudio的代码

            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.写线(“关闭”)
  • 将其添加到Arduino脚本Serial.println(消息); 从串行端口读入VS,您就会清楚消息是否已传递

  • 你能登录arduinos端吗?如果
    Serial.available()?那你的
    led_state=LOW怎么办,当我在“
    @nilsK”上尝试时,您从未将其设置为另一个状态。什么不起作用?
    Serial.available()
    标准是否始终为真?两者都不工作,即关闭/打开或仅其中一个?您的调用方法无效还是arduino接收器无效?请更准确地描述你的问题。请编辑您的问题。Serial.available()始终为真,但有时,如果消息关闭,指示灯不会熄灭,反之亦然。您是否尝试交换串行电缆?端口已打开,消息已发送。Arduino:byte incomingByte=Serial.read();if(incomingByte=='0'){}else if(incomingByte=='1'){}VS:var bytes=BitConverter.GetBytes('1')_currentPort.Write(字节,0,字节.长度);