Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 从VS WPF到Arduino通过串行通信时的奇怪行为_C#_Wpf_Visual Studio_Arduino_Arduino Due - Fatal编程技术网

C# 从VS WPF到Arduino通过串行通信时的奇怪行为

C# 从VS WPF到Arduino通过串行通信时的奇怪行为,c#,wpf,visual-studio,arduino,arduino-due,C#,Wpf,Visual Studio,Arduino,Arduino Due,基本概述。由于WPF应用,我正在从Arduino发送串行数据。到目前为止,这一切都运作良好。今天,我在Arduino代码中实现了一个循环,它在串行端口中查找“Y”(ascii 89),如果收到,它将离开循环并返回到我所称的脱机模式,并通过online=false停止发送数据 现在奇怪的是 在这个循环之前它工作得很好,所以它必须与在离开“在线循环”后尝试重新发送新数据有关 它可以从Arduino串行监视器上完美地工作,这表明这是WPF问题,尽管上传部分的代码没有更改 这两个程序的代码都相当大,所以

基本概述。由于WPF应用,我正在从Arduino发送串行数据。到目前为止,这一切都运作良好。今天,我在Arduino代码中实现了一个循环,它在串行端口中查找“Y”(ascii 89),如果收到,它将离开循环并返回到我所称的脱机模式,并通过online=false停止发送数据

现在奇怪的是

  • 在这个循环之前它工作得很好,所以它必须与在离开“在线循环”后尝试重新发送新数据有关

  • 它可以从Arduino串行监视器上完美地工作,这表明这是WPF问题,尽管上传部分的代码没有更改

  • 这两个程序的代码都相当大,所以我会尽量保持简洁,同时提供所有必要的信息

    void loop() {
      // Check to see if the testbench is in offline mode and run the respective code.
      if (Online == false) {
        OfflineMode();
      }
      // Check to see if the testbench is in online mode and run the respective code.
      if (Online == true) {
        OnlineMode();
      }
    }
    
    void OfflineMode() {
      while (Serial.available())
        processlncomingByte(Serial.read());
      }
    
    然后我有开关盒来处理传入的设置-我知道这很好,因为它也会在Arduino重置后上传

    void processlncomingByte (const byte c) {
      if (isdigit (c)) {
        currentValue *= 10;
        currentValue += c - '0';
      } else {
        // end of digit
        // The end of the number signals a state change
        handlePreviousState ();
        // set the new state, if we recognize it
        switch (c) {
          case 'A':
            state = GOT_A;
            break; 
    etc...
    
    在线模式

    void OnlineMode() {
      CheckForStop();
      SendSerialData();
    }
    
    void CheckForStop() {
      //Serial.println("...");
      if (Serial.available() > 0) {
        //Serial.println("getting something");
        ch = (char)Serial.read();
        inputString = ch;
        if (ch == 89) {
          //Serial.end();
          Online = false;
          //Serial.begin(9600);
          exit;
          //return;
        }
      } else
        delay(5);
    }
    
    SendSerialData()只包含一系列的
    serial.print
    ,输出为一个大字符串,供WPF处理

    正如您将从上面的链接中看到的,监视器输出大量数据,在我发送Y时停止,最后我向“问题”发送Q,询问Arduino是否准备好接收设置,S表示是。真是太棒了

    但是,正如您从下面的链接中看到的,WPF中的情况并非如此。对不起,我现在只能上传2张图片,所以我不得不合并它们

    这是它目前陷入的循环

    private bool checkArduinoisReady() {
      Stopwatch Uploadtimer = new Stopwatch();
      if (!myPort.IsOpen)
        return false;
      // Debug.Print("port is ready to be opened");
      string tempdata;
      Uploadtimer.Start();
      myPort.DiscardInBuffer();
      Start:
      myPort.WriteLine("Q" + Environment.NewLine);
      Debug.Print("sent Q");
      tempdata = myPort.ReadExisting();
      Debug.Print("tempdata_" + tempdata.ToString());
      if (Uploadtimer.ElapsedMilliseconds > 5000)
        return false;
      if (tempdata.Contains("S"))
        return true;
      else
        goto Start;
    }
    
    在另一页上,这是我如何停止传入数据的

    private void StopTest(object sender, RoutedEventArgs e) {
      MessageBoxResult StopConfirm = MessageBox.Show("Are you sure you want to stop the test?", "Stop the test", MessageBoxButton.YesNo, MessageBoxImage.Question);
      if (StopConfirm == MessageBoxResult.Yes) {
        Timer.Stop();
        Debug.Print("Timer Stopped");
        myPort.DiscardInBuffer();
        Start:
        for (int i = 0; i < 100; i++) {
          myPort.WriteLine("Y");
        }
        string tempData = myPort.ReadExisting();
        Debug.Print("Checking...");
        Debug.Print("tempData_" + tempData);
        if (string.IsNullOrWhiteSpace(tempData)) {
          Debug.Print("Its null!!");
          comments_textbox.Text = comments_textbox.Text + "Test Aborted";
          MessageBoxResult SaveCurrentData = MessageBox.Show("Would you like to save the data collected up until this point?", "Save", MessageBoxButton.YesNo, MessageBoxImage.Question);
          if (SaveCurrentData == MessageBoxResult.Yes) {
            SaveFile();
          }
          if (SaveCurrentData == MessageBoxResult.No) {
            myPort.Close();
            NavigationService.Navigate(new Uri("testSettings.xaml", UriKind.RelativeOrAbsolute));
          }
        } else {
          Debug.Print("Still going...");
          goto Start;
        }
      }
    }
    
    private void StopTest(对象发送方,路由目标){
    MessageBoxResult StopConfirm=MessageBox.Show(“您确定要停止测试吗?”,“停止测试”,MessageBoxButton.YesNo,MessageBoxImage.Question);
    if(StopConfirm==MessageBoxResult.Yes){
    Timer.Stop();
    调试打印(“计时器停止”);
    myPort.DiscardInBuffer();
    开始:
    对于(int i=0;i<100;i++){
    myPort.WriteLine(“Y”);
    }
    字符串tempData=myPort.ReadExisting();
    调试。打印(“检查…”);
    Debug.Print(“tempData”+tempData);
    if(string.IsNullOrWhiteSpace(tempData)){
    Print(“它的null!!”);
    comments\u textbox.Text=comments\u textbox.Text+“测试中止”;
    MessageBoxResult SaveCurrentData=MessageBox.Show(“是否保存到目前为止收集的数据?”,“保存”,MessageBoxButton.YesNo,MessageBoxImage.Question);
    if(SaveCurrentData==MessageBoxResult.Yes){
    SaveFile();
    }
    if(SaveCurrentData==MessageBoxResult.No){
    myPort.Close();
    NavigationService.Navigate(新Uri(“testSettings.xaml”,UriKind.RelativeOrAbsolute));
    }
    }否则{
    Debug.Print(“仍在运行…”);
    转到开始;
    }
    }
    }
    
    对我来说,最大的绊脚石是为什么它可以在串行监视器上工作,而不能在应用程序中工作。只要我重置Arduino,它也能工作。我也在Arduino中尝试了
    resetFunc()
    ,但这也没有帮助


    提前感谢。

    原来我的开关盒中还有一个resetFunc(),它阻止了串行监视器继续发送数据

    原来我的开关盒中还有一个resetFunc(),它阻止串行监视器继续发送数据

    首先,我在发送和readexisting函数之间设置了一个小延迟。然后,串行端口也可以返回部分消息,因此写入“tempdata=myPort.ReadExisting();”是不正确的(应该追加)。最后,我将使用串行端口作为异步设备,而不是同步设备。您可以使用1)计时器每秒发送Q字节(例如),然后当您收到正确的字符串时,您可以停止它。另一个计时器可以检测超时(如果您收到正确的数据,只需在触发之前停止它)。最后,通过SerialPort类的。记住总是附加到缓冲区,然后在完成后从缓冲区中删除。试试这个,我认为它会起作用(这种方法通常可以在串行端口运行不正常时节省我的时间)嗨@frarugi87,谢谢你的回复!我已经实现了定时器,这对一切都有很大帮助,它更好!RE-myPort.ReadExisting不正确。我知道你是从哪里来的,但是读取和追加一个字节(例如)目前不起作用,因为Arduino似乎没有发送任何东西,就软件而言。因此,应用程序没有接收到任何东西,而且绝对不是一个“S”,结果是,在我的交换机案例中有一个resetFunc(),它阻止串行监视器继续发送数据!总是有一条线让你站不住脚!好吧,一切都很好,结局很好:-)很高兴你能找到问题的解决方案。首先,我在发送函数和readexisting函数之间加了一点延迟。然后,串行端口也可以返回部分消息,因此写入“tempdata=myPort.ReadExisting();”是不正确的(应该追加)。最后,我将使用串行端口作为异步设备,而不是同步设备。您可以使用1)计时器每秒发送Q字节(例如),然后当您收到正确的字符串时,您可以停止它。另一个计时器可以检测超时(如果您收到正确的数据,只需在触发之前停止它)。最后,通过SerialPort类的。记住总是附加到缓冲区,然后在完成后从缓冲区中删除。试试这个,我认为它会起作用(这种方法通常可以在串行端口运行不正常时节省我的时间)嗨@frarugi87,谢谢你的回复!我已经实现了定时器,这对一切都有很大帮助,它更好!RE-myPort.ReadExisting不正确。我知道你在哪里