简体   繁体   中英

c# send int array via serial port to arduino

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?

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...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM