0

I'm trying to send an int array from C# to Arduino using the serial port. In C#, first I have the input string

input = "98;1;5;160;0;255;421;101";

then, I convert it to an int array

int [] sendint = input.Split(';').Select(n => Convert.ToInt32(n)).ToArray(); 
//this array is what I need to send to Arduino

then, I convert it to a byte array to send via the serial port

byte[] sendbytes = sendint.Select(x => (byte)x).ToArray();
// because Write() requires byte, not int

and finally, I send it

serialport.Write(sendbytes,0,sendbytes.Length); 
// port is open, baudrate is OK, portname is OK

Then, it should be received by my Arduino

int recdata[10];
int bytes = 0;
if(Serial.available())
{     
  while(Serial.available())
  {
    recdata[bytes]=Serial.read();
    bytes++;
  }
  checkdata(); //function which checks the received data
}

so recdata should be an int array

recdata = {98,1,5,160,0,255,421,101};

but it isn't. When I print it to another serial port to check..

for(int i = 0; i < 10; i++) //called befory checkdata() function in code above
{
  Serial1.print(recdata[i] + "  ");
}

I get 3 outputs, instead of 1, as if the serialport sends first one int, then second and then the rest.

98  0  0  0  0  0  0  0  0  0  1checkfail //1checkfail is from function checkdata()
1  0  0  0  0  0  0  0  0  0  1checkfail //and it's saying, that data
5  160  0  255  165  101  0  0  0  0  1checkfail//are wrong

98 1    5  160   0   255 421 101 0  0 1checkok //this is how it should like
                      //421 and 165 - i know, that i'm trying to save 421 in byte, which has a limit of 256, so this is another problem

Does anyone have a suggestion to this problem?

Patrick Magee
  • 2,951
  • 3
  • 33
  • 50
parman
  • 127
  • 1
  • 3
  • 11

2 Answers2

1

We should see what your checkdata() function does, but I'm prtty sure that you don't check the bytes variable.

What happens is that.... You are using a SERIAL communication. Which means that you don't get all your data at once, but instead you get it one byte at a time. If you send 8 bytes from the PC but check the Serial.available() function after two bytes have been received, you will get just 2 as an answer. I think that if you modify your code in this way

// Move these OUTSIDE the loop function
int recdata[10];
int bytes = 0;

// This is in the loop function
if(Serial.available())
{     
  while(Serial.available())
  {
    recdata[bytes]=Serial.read();
    bytes++;
  }
  if (bytes >= 8)
  {
    checkdata(); //function which checks the received data
    bytes = 0;
  }
}

it will work properly...

Otherwise... Write your checkdata function and we'll see.

By the way... I'll put a check in the reading part, like

while(Serial.available())
{
  if (bytes < 10)
  {
    recdata[bytes]=Serial.read();
    bytes++;
  }
}

So you can avoid memory corruption if you receive 12 bytes instead of 10...

frarugi87
  • 2,826
  • 1
  • 20
  • 41
0

The Arduino loop is catching up to the serial stream, so the loop is entered three times instead of just once.

You can use Serial.readBytes() instead of Serial.read(). It will keep filling your buffer until it times out. Use Serial.setTimeout() to choose a reasonable timeout instead of the default 1000 milliseconds.

char recdata[10];
int bytes = 0;
if(Serial.available())
{     
  bytes = Serial.readBytes(recdata, MAX_LENGTH);
  checkdata(); //function which checks the received data
}

For the problem of converting ints to bytes, look at this question.

Community
  • 1
  • 1
UncleO
  • 8,299
  • 21
  • 29
  • readBytes hot working with int[](no matching function for vall to hardwareserial::readbytes(int[10], int). When I modify it, still not working(without modifying timeout). I think, that the c# is wrong and it has a delay while sending. When I add `delay(1); ` between `if(Serial.available())` and reading, it works. But mine still not, only yours – parman Dec 11 '14 at 16:33
  • @parman Sorry, I just copied your code, and didn't see the error. `readBytes()` needs an array of char to place the data in. (char is the C term for a byte.) – UncleO Dec 11 '14 at 21:37