Sending multiple values from c# to arduino leonardo

Hi, i'm trying to send multiple values from a c# program to arduino trought serial, but i only know how to send a string.

An example: i have two Int's in my c# program (X and Y), and i want to send this to arduino, and convert again in two Int's.

c# code example: (assuming there is no way to send it directly as an integer)

            int x = 50;
            int y = 100;
            SerPort.Write(String.Format("{0}{1}", x, y));

and I don't know how to take these values ​​in arduino separately, and convert them to integers again

See Serial input basics - updated or experiment with the Serial.parseInt() function

a common approach is to received a complete line of information, terminated with a specific character such as a newline, '\n' (i.e. readBytesUntil()). of course values/fields should be separated in that line by a non-value character (e.g., space, comma)

sscanf can then be used to extract specific values from the complete line

If you add a comma, you can separate x and y. Else you wil never know if e.g. 123 means x = 1 and y = 23 or x = 12 and y = 3.

And to make use of the serial input basics example with startmarker and endmarker

SerPort.Write(String.Format("<{0},{1}>", x, y));

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.