port.ReadLine() - 不更新

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了port.ReadLine() - 不更新相关的知识,希望对你有一定的参考价值。

我真的需要你的帮助。

我正在用Arduino Uno和一些磁传感器建立一个项目。我希望打印传感器的结果输出,然后在我的asp.net项目(Visual Studio)上读取它。

我使用Serial.printSerial.println打印字符串和port.ReadLine()来读取字符串。

问题是,当我读取字符串时,它不会像它应该更新(在Arduino的串行监视器上,我可以看到它正在更新)。

当我试图将ReadLine更改为Read时,它也无法正常工作。

Serial monitor

enter image description here

“data_arduino”未获得更新。

你能帮助我吗?

这是完整的可视代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO.Ports;
using System.Threading.Tasks;

namespace ConsoleApp2 {
    class Program {
        static void Main(string[] args) {
            SerialPort port = new SerialPort();
            port.BaudRate = 9600;
            port.PortName = "COM3";
            port.DtrEnable = true;
            port.Open();
            bool status1 = true;
            bool status2 = true;
            bool status3 = true;
            char[] arr = new char[4];
            while (true) {
                String data_arduino = port.ReadLine();
                for (int i = 0; i < arr.Length; i++) {
                    char first = data_arduino[i];
                    arr[i] = first;
                }
                int space = arr[0] - 48;
                if (arr[1] == 48)
                    status1 = false;
                if (arr[2] == 48)
                    status2 = false;
                if (arr[3] == 48)
                    status3 = false;
            }
        }
    }
}
答案

这里有多余的port.ReadLine()语句:

char first = data_arduino[i];
arr[i] = first;
data_arduino = port.ReadLine(); //Here you read the value you don`t use

删除它并在while循环开始时只留下port.Read()方法调用

以上是关于port.ReadLine() - 不更新的主要内容,如果未能解决你的问题,请参考以下文章

导致活动不工作的片段中的按钮(更新)

回归 | js实用代码片段的封装与总结(持续更新中...)

如何更新所有片段? onProgressUpdate 不更新所有片段

刷新片段不再起作用?

ForYouFragment 片段不更新

在常规(支持)片段中使用 AsyncTaskLoader 更新适配器不更新 ListView