Multiple Serial Commands c#

I'm having trouble reading serial commands that I send with my computer using serial.

this is the arduino code.

if (Serial.available() > 6)
      Serial.readBytesUntil('/', buffer, 120);
      int v =; //green
      int w =;//red
      int x =; //green
      int y =;//red
      int z =;//blue
      int q =;//blue
      analogWrite(6, x);
      analogWrite(5, y);
      analogWrite(3, z);
      analogWrite(11, q);
      analogWrite(10, v);
      analogWrite(9, w);

here is the c# code that sends the data

public void serialwrite()
            slider1 = BitConverter.GetBytes(trackBar4.Value);
            slider2 = BitConverter.GetBytes(trackBar5.Value);
            slider3 = BitConverter.GetBytes(trackBar6.Value);
            slider4 = BitConverter.GetBytes(trackBar7.Value);
            slider5 = BitConverter.GetBytes(trackBar8.Value);
            slider6 = BitConverter.GetBytes(trackBar9.Value);
            serialPort1.Write(slider1, 0, 1);
            serialPort1.Write(slider2, 0, 1);
            serialPort1.Write(slider3, 0, 1);
            serialPort1.Write(slider4, 0, 1);
            serialPort1.Write(slider5, 0, 1);
            serialPort1.Write(slider6, 0, 1);

Any help that you could provide would be greatly appreciated.

I'm looking to get the arduino to recieve a value from the trackbar between 0-255 and then set those value to the leds. The problem i have is that an led will randomly turn on and then off quickly.

You check to see if there is at least one byte to read. Then, you try to read at least 7 bytes. Clearly, that won’t work. You should use start and end markers for your packets, then read and store all the data until the end of packet marker arrives. Only then should you try to parse the data.

#define SOP '<'
#define EOP '>'

bool started = false;
bool ended = false;

char inData[80];
byte index;

void setup()
   // Other stuff...

void loop()
  // Read all serial data available, as fast as possible
  while(Serial.available() > 0)
    char inChar =;
    if(inChar == SOP)
       index = 0;
       inData[index] = '\0';
       started = true;
       ended = false;
    else if(inChar == EOP)
       ended = true;
      if(index < 79)
        inData[index] = inChar;
        inData[index] = '\0';

  // We are here either because all pending serial
  // data has been read OR because an end of
  // packet marker arrived. Which is it?
  if(started && ended)
    // The end of packet marker arrived. Process the packet

    // Reset for the next packet
    started = false;
    ended = false;
    index = 0;
    inData[index] = '\0';

This code will read and store all the data, if you use < and > as the start and end markers.

Where it says “Process the packet” is where you would try to make use of the data received.[/code]

make sure your pc code does not open/close the comport each time it sends a command. doing so will cause the arduino to reset each time. simple serial monitor test code.

// zoomkat 7-30-11 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later

String readString;

void setup() {
  Serial.println("serial test 0021"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    delay(2);  //delay to allow byte to arrive in input buffer
    char c =;
    readString += c;

  if (readString.length() >0) {


Thank you PaulS. Your code helped me get my program to work. :slight_smile: