Reputation: 63
I really need your help.
I am building a project with an Arduino Uno and some magnetic sensors. I want the result output of the sensors to be printed and then to read it on my asp.net project (Visual Studio).
I am using Serial.print
and Serial.println
to print the string and port.ReadLine()
to read the string.
The problem is that when I read the string it does not update like it should be (on the Serial monitor in Arduino I can see that it's updating well).
When I am trying to change the ReadLine to Read it's not working either.
"data_arduino" is not getting updated.
Can you help me?
This is the full Visual code:
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;
}
}
}
}
Upvotes: 1
Views: 373
Reputation: 359
You have excess port.ReadLine() statement here:
char first = data_arduino[i];
arr[i] = first;
data_arduino = port.ReadLine(); //Here you read the value you don`t use
Remove it and leave only the port.Read() method call at the while loop beginning
Upvotes: 1