C# interfacing with arduino

Hello

I am trying to send an integer array to arduino. The integer values varries from 0 to 255. I tried many ways and nothing seems to work. can you please help. i was trying to convert the integer value to byte array and then send it but for a reason, the method did not work.

this is the c# code

int[] values = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
string[] bytes = new string[20];
byte pwm;
for (int i = 0; i < 20; i++)
{
    pwm = Convert.ToByte(value[i]);
    data[0] = pwm;
    serialPort1.Write(data, 0, 1);
}

and this is the arduino code

byte Csharp;
int value[20];
...
for(int i = 0;i<20;i++){
    Csharp= Serial.read();
    value[i] = int(Csharp);
}

The integer values varries from 0 to 255.

Do they really?

int[] values = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};

If the values really ARE byte sized, why is the array 4 times that big?

What’s this for?

string[] bytes = new string[20];

It’s never used, and is the wrong type to be useful, anyway.

    data[0] = pwm;

It would be nice to know how data is declared.

i was trying to convert the integer value to byte array and then send it but for a reason, the method did not work.

There are so many things that “the method did not work” could cover that it is not even reasonable to expect us to guess what it means.

for(int i = 0;i<20;i++){
    Csharp= Serial.read();
    value[i] = int(Csharp);
}

Does it not matter that the values might not have arrived yet? I guess not.

Hi PaulS

int[] values = {255,0,100,0,0,0,0,0,99,0,0,0,0,0,0,0,0,0,50,0};

These values are send to the arduino as bytes.
The conversion is done as follows

for (int i = 0; i < 20; i++)
{
    byte[] intBytes = BitConverter.GetBytes(values[i]);
    byte[] result = intBytes;
    serialPort2.Write(result, 0, 1);
}

On the Arduino side, the received_value is used to store in the received values

int received_value[]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void loop()
{
    if (Serial.available() > 0) {  
        for(int i = 0;i<20;i++){
            Csharp= Serial.read();
            received_value[i] = Csharp;
        }
... // here is where that data is transfered
    }
}

We are trying to send values to control daisy chained digital potentiometers. We expect the values to control the setting of these devices but they have no effect. We know that it is not a hardware problem because the data can be sent when the c# program is not used.

    byte[] intBytes = BitConverter.GetBytes(values[i]);

What is really in this array?

    byte[] result = intBytes;

Why do you need to make a copy of the first array?

serialPort2.Write(result, 0, 1);

What is the value in result[0]?

    if (Serial.available() > 0) {  
        for(int i = 0;i<20;i++){
            Csharp= Serial.read();

If there is a byte to read, read all 20 of them. I told you before that that will NOT work. You ignored that advice.

Why isn’t values on the PC byte sized? Why do you need to store byte sized values in an int array?

Ehhhh, don't worry we have it working just fine now, thanks.

we have it working just fine now

But, just in case anyone else is having the same problems, we won't tell you want we needed to do to make it work.