Help with serial data from C#

HI,

I'm working on a hobby project where I want some LEDs to turn ON/OFF and run a motor from a C# program.

This all works fine, the problem I'm struggling with is when I introduce an "TrackBar" in C# to send a numeric value thru the serial port. I get the numbers, but how do I "split" the value from the others on serial port and send it to "AnalogWrite"?

inChar = Serial.Read();
AnalogWrite(6,inChar);
String command;
void setup()
{
    Serial.begin(9600);
    pinMode(8, OUTPUT);  
    
}

void loop()
{
    if ( Serial.available() > 0 ) 
  {
      
      char inChar;
      inChar = Serial.read();

      if( inChar != '\n' )
      {
          command.concat((String)inChar);
      }
      else
      {
          switch( command[0] )
          {
              case '#' :
                  Serial.println( "Your connected!" );
                  command = "";
              break;
              
              case '+' :        
                Serial.println( "LED ON");
                digitalWrite(8,HIGH);
                 command = "";
              break;
                
              case '-' :
                Serial.println( "LED OFF" );
                digitalWrite(8,LOW);
                 command = "";
              break;

              default:
              break;
          }
      }
  }  
}

This all works fine, the problem I'm struggling with is when I introduce an "TrackBar" in C# to send a numeric value thru the serial port.

Are you really sending a numeric value (47, for instance), as opposed to a string representation of the numeric value ("47", for instance)?

fsoender: I get the numbers, but how do I "split" the value from the others on serial port?

Which 'others'?

PaulS: Are you really sending a numeric value (47, for instance), as opposed to a string representation of the numeric value ("47", for instance)?

Yes, I get value from 0-2000 from the slider, and the character "+" and "-" from the buttons. How can I make sure the value from slider goes to "AnalogWrite", and the button characters to digital out?

It might be a lot easier if your 'protocol' is purely text based and not a mix of binary values and text. I guess it requires a conversion of the trackbar value using toString() at the C# side. Maybe read serial input basics to get some ideas.

1) read data byte 2) if '+' or '-', it's the start of your message (and you know if it's plus or minus) 3) if not '+' or '-', ignore till such time that you have a '+' or a '-' 4) read data bytes and store in buffer till you receive '\n' 5) use something like atoi to convert the text to an integer value

Have a look at the examples in Serial Input Basics. There is also a parse example.

I agree with @sterreje that sending the data as human-readable characters will make the debugging very much easier. I would only send binary data if I needed the extra speed.

...R

Not only for debugging, also for parsing ;)

Thanks guys, will have a look :)