首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >port.ReadLine() -未更新

问port.ReadLine() -未更新
EN

Stack Overflow用户
提问于 2017-12-28 17:38:27
回答 1查看 167关注 0票数 1

我真的需要你的帮助。

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

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

问题是,当我读取字符串时,它没有按照应该的方式更新(在Arduino的Serial监视器上,我可以看到它更新得很好)。

当我尝试将ReadLine更改为Read时,它也不起作用。

​

​

​

​

"data_arduino“未更新。

你能帮帮我吗?

这是完整的可视化代码:

代码语言:javascript
复制
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;
            }
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-12-28 17:48:36

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

代码语言:javascript
复制
char first = data_arduino[i];
arr[i] = first;
data_arduino = port.ReadLine(); //Here you read the value you don`t use

删除它,只将port.Read()方法调用保留在while循环开始处

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48005011

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档