Sending int from c# to arduino

So i'm trying to send an int number from c# trackbar to arduino. I've tried this:

 private void trackBar1_Scroll(object sender, EventArgs e)
        {
           
            byte[] buffer = new byte[1];
            buffer[0] = (byte)trackBar1.Value;
            if (serialPort1.IsOpen)
            {
                serialPort1.Write(buffer, 0, 1);
                label1.Text = trackBar1.Value.ToString();
            }
        }

It's send 1 byte to arduino, but because of that the int number is limited from 0-255. Can someone help me to send any int number i want. Thanks

Break the int into bytes, send the bytes individually, receive them on the Arduino and reconstruct the int by bitshifting and masking

void setup()
{
  Serial.begin(2000000);
  while (!Serial);
  byte lowByte = 0b00000001;
  byte highByte = 0b10000000;
  int result = highByte << 7 | lowByte;
  Serial.println(result, BIN);
}

void loop()
{
}

Why not send the value as text - it makes debugging easier.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

…R

I’ve tried this before

C# code:

private void textBox2_TextChanged(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
                serialPort1.WriteLine("vs_kp" +textBox2.Text);
        }

Arduino Code:

void setup() {
Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void serialEvent() {
  while (Serial.available()) {
    // get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    if (inChar != '\n') {
      mySt += inChar;
    }
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    if (inChar == '\n') {
      stringComplete = true;
    }
  }

void loop() {
  if (stringComplete) {
    // clear the string when COM receiving is completed
    mySt = "";  //note: in code below, mySt will not become blank, mySt is blank until '\n' is received
    stringComplete = false;
  }
  if (mySt.substring(0,5) == "vs_kp"){
    kp = mySt.substring(5,mySt.length()).toFloat(); //get string after vs_kp
  }
}

It works on the Arduino UNO but when i switch to the Leonardo, if i type in the Serial Monitor anything the Serial Monitor stop working. I asked about this and someone told me to avoid using string so now i’m try to sent an int directly.

if i type in the Serial Monitor anything the Serial Monitor stop working. I asked about this and someone told me to avoid using string

They probably suggested that you do not use Strings (capital S) not strings(lowercase s) but I suspect that there was a much larger problem than that if typing into the Serial monitor it stopped working.

Note that Robin's solution uses strings (zero terminated arrays of chars) not Strings

dta1999:
It works on the Arduino UNO but when i switch to the Leonardo, if i type in the Serial Monitor anything the Serial Monitor stop working. I asked about this and someone told me to avoid using string so now i'm try to sent an int directly.

I think things are mixed up here.

Differences between the Leonardo and the Uno have nothing to do with the String class. The USB system is a little different on the Leonardo so you need this to allow time for the serial port to be created

Serial.begin(115200);
while (!Serial);   //  needed for Leonardo and Micro
// rest of the code

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

...R