port.ReadLine() - Not reading

Hey!

I really need your help.

I am building a project with 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).

Im using Serial.print & Serial.println to print the string and port.ReadLine() to read the srtring.

The problem is that when I read the string its not update like at should be (on the Serial monitor in arduino I can see that its updating well).

http://www.siz.co.il/my.php?i=gmktrodt5yhn.png

http://www.siz.co.il/my.php?i=1ya3dytnij0n.png

Can you help me?

In your c# you read the serial port line and then do nothing with the result of the read.

This is the full code on visual :

           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;
                        data_arduino = port.ReadLine();
                    }
                    int space = arr[0] - 48;
                    if (arr[1] == 48)
                        status1 = false;
                    if (arr[2] == 48)
                        status2 = false;
                    if (arr[3] == 48)
                        status3 = false;

                }

        }
    }
}

            }

I'm obviously missing something. That is not the full code, it is just a fragment. I don't understand the processing of the input line and I don't see where you use the data once read and processed.

Edit: So now my post looks silly since you updated the code section I was commenting on.

You right I've edit it.

What I want to do is to read the line that the arduino is printing ("5111" / "4111")
and then to the the first char and save it on "space" and then on..

Think about

for (int i = 0; i < arr.Length; i++)
                    {
                        char first = data_arduino[i];
                        arr[i] = first;
                        data_arduino = port.ReadLine();
                    }

Each time you round the loop you overwrite the content of data_arduino with the next line so at best the array 'arr' witll have the first char from the first line, the first char from the second line in the second position and so on. And I still don't see where you do anything with the resulting content of 'arr' and I still don't understand the processing used to generate 'arr' .

UPDATE:

for (int i = 0; i < arr.Length; i++)
                    {
                        char first = data_arduino[i];
                        arr[i] = first;
                       // data_arduino = port.ReadLine();  [b]Ive delete this line[/b]
                    }

but its still not working

What I need it to the each of the components of the printed line "5111" and sace every one of them in argument.

int space = arr[0] --> the first number (5 in this example)

bool status1 = arr[1] --> the second number (1 in this example)

and go on..

but my problem is that the "data_arduino" is not getting update

In your first post you say

The problem is that when I read the string its not update like at should be (on the Serial monitor in arduino I can see that its updating well).

but I don't see in your c# code where you are displaying the result of the processing so you can tell that the data is not updating as you expect.

Try to walk before you try to run. Just print out on the c# console the content of each line you read.

Tamirh98:
but my problem is that the "data_arduino" is not getting update

How do you know. Where do you test it or print it or display it?

Ive put add watch on it .

![](http://<a target=)

">

Tamirh98:
Ive put add watch on it .

Then you need to simplify the program to the point where you are just concerned with reading each line and not doing any post read processing of the line. You also need to make sure you have the correct port and baud rate. Com back when you have done this.

Thank you !! It's work!!!

I have another questionfor U -

My web-site is on the server (with his database) , and the arduino is connected to my computer.

I want that this code will update the database (that on the server) without using Ethernet shield / Wifi shield.

It is possible?

Tamirh98:
Thank you !! It's work!!!

So what was wrong?

I have another questionfor U -

My web-site is on the server (with his database) , and the arduino is connected to my computer.

I want that this code will update the database (that on the server) without using Ethernet shield / Wifi shield.

It is possible?

Too many unknowns for me to guess at.