0

Would like to seek help. I am new with C# and want to integrate arduino. Would like to know on how to convert the code i sent from arduino to C#. I want to assign the data into an array int. Here's the sample i want to send from arduino.

Serial.println(1);
Serial.println(2);
Serial.println(3)

and my code in c#

int[] data = new int[3];
data[3] = Int32.Parse(serialPort.ReadLine());
data[2] = Int32.Parse(serialPort.ReadLine());
data[1] = Int32.Parse(serialPort.ReadLine());

Kindly please advise

1 Answer 1

3

array's indexes in c# start from 0.

this code line is your problem and the reason to the failure:

data[3] = Int32.Parse(serialPort.ReadLine());

change your code to:

int[] data = new int[3];
data[2] = Int32.Parse(serialPort.ReadLine());
data[1] = Int32.Parse(serialPort.ReadLine());
data[0] = Int32.Parse(serialPort.ReadLine());

Edit: (answer to the comment...)

This class provide you 2 extensions method one for unknown sequence length(but you have to create a stop condition) use the second method if you know the exact number of ints.

Note: 4 years ago i used SerialPort with a graphical tablet.
I used the event: DataReceived as a reading trigger. for more information see this example.

//to use extension methods do: serialPort.ReadKnownLength(3);

public static class SerialPortExtensions
{
    public static int[] ReadUnknownLength(this SerialPort serialPort)
    {
        var list = new List<int>();

        while (true)
        {
            try
            {
                var input = serialPort.ReadLine();

                //change the condition to your own break condition
                if (String.IsNullOrEmpty(input))
                    break;

                list.Add(int.Parse(input));
            }
            catch (TimeoutException)
            {
            }
        }

        // if you don't need to reorder
        //return list.ToArray();

        var result = new int[list.Count];

        for (var i = 0; i < result.Length; i++)
        {
            //reorder the input(you did it in your question...)
            result[i] = list[list.Count - (i + 1)];
        }

        return result;
    }

    //match your question behavior
    public static int[] ReadKnownLength(this SerialPort serialPort, int length)
    {
        var result = new int[length];

        for (int i = length - 1; i >= 0; i--)
        {
            result[i] = Int32.Parse(serialPort.ReadLine());
        }

        return result;
    }

}
Sign up to request clarification or add additional context in comments.

1 Comment

thanks.. yes i forgot. i got the values i want.. How about if i have long list of data not only three. Is there any other way to code it in a simple manner?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.