send data to the Serial port with visual studio, and use it in Arduino IDE

Hello folks!

I created a visual studio c# winform to calculate a game, and now I'm trying to send the data (position of pixels) to the Serial, so the Arduino can use the location of the pixels(Byte x-position, byte y-position) to generate an analog component video signal. Everything is working perfectly, despite the fact that I can't read the serial data in a correct way. (I haven't got allot of experience in Serial.read() function).

I can send out any string of text I want, but with arduino's Serial.read() function I can only read one character at a time, which is very confusing for me. :/

How would you do this?

but with arduino’s Serial.read() function I can only read one character at a time, which is very confusing for me. :confused:

Try:

void read_serial (String &serial_indata) {
  serial_indata="";

  if (Serial.available() > 0) {
    int h=Serial.available();   
    // if you are getting escape -characters try h--; here
    for (int i=0;i<h;i++){
      serial_indata += (char)Serial.read();
    }
    // if you are getting escape -characters try Serial.read(); here
  }
  
  // trim the white space off the string:
  serial_indata.trim();
}

Yours,
Stefan

Wow, thanks for the quick reply!

Could you explain some things in more detail?

Like:

String &serial_indata

and the meaning of "escape -characters"

Thanks allot!

Maybe this will help. I think it does what you want.

/*
    Simple, basic, non blocking serial decimal input scheme.

    This is case sensitive, x != X.

 */

int inVal = 0;

void setup ()
{

    Serial.begin ( 9600 );

    Serial.println ( " Simple serial input for two decimal variables." );
    Serial.println ( );
    Serial.println ( " Example: 150x250y, as one line. Or 150x,  250y as two lines." );
    Serial.println ( );


} // setup

void loop () 
{

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

        switch ( inChar ) 
        {
        case '0' ... '9': // capture only the numeric input

            inVal = ( inVal * 10 ) + ( inChar - '0' ); // ascii -> decimal

        break;

        case 'x': // use as x value

            // print to serial out, for demo
            Serial.print ( "Input x = " );
            Serial.print ( inVal, DEC);
            Serial.println ();

            inVal = 0; // reset for new input

        break;

        case 'y': // use as y value

            // print to serial out, for demo
            Serial.print ( "Input y = " );
            Serial.print ( inVal, DEC);
            Serial.println ();

            inVal = 0; // reset for new input

        break;        

        }
    }

    // other loop stuff

} // loop

tf