Recieve an array via serial

Hey, I am trying to send an array of intergers with 35 elements in it from a c# program to an arduino via serial communication and I am stuck. c# is making me convert each int in the array to a string.

my c# code is

if (serialPort1.IsOpen)
                {
                    for (int i = 0; i < 35; i++)
			        {
                        serialPort1.Write(guide[i].ToString());
                    }
                }

what is the best way to read in the array and convert it back to an int?

Maybe add a line after this: serialPort1.Write(guide*.ToString());* I don't use C# but you would say serialPort1.Write('\0'); or something so arduino will assemble all bytes read into a number when it reads a '\0', then it will buffer the next number. You have to have an escape character.

You need to send binary data. Looking for null characters in binary data is certain to succeed -- but in far too many places.

There should be a "write" function that allows sending binary data. However, you will need a "protocol" -- no matter how simple it is.

In other words you will need to send "blocks" of data and know exactly how many bytes of data were sent. The arduino program will need to behave the same way -- only in reverse. It will need to know exactly what data will be sent -- and how much.

Here is a routine to look at…

Sending binary data is different… Now you will have to recast each integer as an array of bytes – keeping in mind thatVisual C# seems to use 4 byte integers as the standard… Arduino uses two bytes – so you receive at the arduino as bytes – that you stuff into “long” variables…

Simple really if you think about it a bit… remember this array is just bytes… so fill it up with data and get it sending to the Arduino, get the Arduino to receive 35 bytes – then create your set of integers – and send each one as four bytes…

You will always have to creat a routine which sends “the right amount” and at the arduino “it knows” exactly what to receive.

Alternatively you can make a protocol which sends say…“ARRAY LONG,5,7” to the arduino – which then has a routine to receive a 5 X 7 array of “longs” i.e c# integers. That is the “right way”. You could even make the code as simple as “AL57” – and the arduino would “know” what that meant because as soon as you decoded that string from the serial port you would go to the right routine.

A Protocol is what YOU decide it should be!

            byte[] guide = new byte[35];
             
            if (serialPort1.IsOpen)
            {
                for (int i = 0; i < 35; i++)
                {
                    serialPort1.BaseStream.WriteByte(guide[i]); 
                      //or  .BeginWrite(guide, 0, 35, mymeth, myobj); for a block with callback etc...

                }
            }

Thank you for your response WillR. I believe I understand how to send the data in C# to the arduino, but I'm confused on how to read it. Could you please give me an example of the best way to read the data in processing? Also do you cast in processing the same way you do in c#?

Thanks

I'd say using escape characters is the easiest way to understand. Also the Arduino side can just do received_character-'0' to get a digit, then multiple power of 10 to restore to number

Say if you send an integer of 235, you can just do your serial write with the toString(), then send a '\0'. Arduino receives '2', '3', and '5', then '\0'. Arduino does while (received()!=escape_character) {digit=readfromSerial()-'0';} Then multiply by 10.

The binary send would be a couple times quicker theoretically but if you don't produce a lot of data, the usb serial driver may delay send until timeout or buffer fills :)