port.ReadLine() - 不更新

问题描述 投票:0回答:1

我真的需要你的帮助。

我正在用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;
            }
        }
    }
}
c# asp.net arduino serial-port
1个回答
1
投票

这里有多余的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()方法调用

© www.soinside.com 2019 - 2024. All rights reserved.