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?