Windows Form SerialPort communicating with Arduino, Reading ASCII istead of character
<p>I am trying to create a c++ windows form application  in Visual Studio, which is communicating with an Arduino and controlling three LEDs with three trackBars. I managed to write the code that sends message the Arduino. But i would like to ask the Arduino about the state of the LED's when I start the program, to set the sliders at the right position.</p>

<p>My solutuion was to program the Arduino that if it gets the 'x1' command, it sends back the values of the LEDs (from 0-255) separated by commas. The code on the Arduino should be all right, because I used the same code to do the same thing in Qt Creator, and it worked perfectly there.</p>

<p>My test code works like if I click a button, it sends 'x1' command the Arduino, and in the SerialPort's DataReceived event I put the received value in a label.</p>

<p>There are two problems.
The first one is, even if I use Convert::ToString() or not, i get the ASCII code of the numbers and the commas. (eg.: for 0,0,0, I get on the screen: 484448444844)</p>

<p>The second one is that I don't always get the whole message. Sometimes the last 2-3 character is missing, and if I click the button again it shows up at the beginning of the new message. (I clear the label every time I click the button and the DataReceived event appends to it).  I understand that sometimes some parts are delayed, but shouldn't theese parts trigger the DataReceived event when arrive, and show up at the end of the message?</p>

<p>How could I get rid of theese problems?</p>

<p>The code of the DataReceived event:</p>

<pre><code>private: System::Void serialPort1_DataReceived(System::Object^  sender,
  System::IO::Ports::SerialDataReceivedEventArgs^  e) 
{
    //Try to read from serial
    for (int i = 0; i &lt; serialPort1-&gt;BytesToRead; i++)
    {
        lblTest-&gt;Text += Convert::ToString(serialPort1-&gt;ReadByte());
    }

}
</code></pre>

<p>The code of the button:</p>

<pre><code>private: System::Void btnTest_Click(System::Object^  sender, 
  System::EventArgs^  e) {

    lblTest-&gt;Text = String::Empty;      
    serialPort1-&gt;Write("x1");

}
</code></pre>

<p>The Way I send message:</p>

<pre><code>private: void sendSerialData(String^ data)
{
    if (serialPort1-&gt;IsOpen)
    {
        serialPort1-&gt;Write(data);
        lblMessage-&gt;Text = "Message sent to device: " + data;
    }else {
        lblMessage-&gt;Text = "Message transmission failed!";
    }
}
</code></pre>

<p>And here is how i send these data from the Arduino to PC:</p>

<pre><code>  Serial.print(redVal);
  Serial.print(',');
  Serial.print(greenVal);
  Serial.print(',');
  Serial.print(blueVal);
  Serial.print(',');
  Serial.flush(); 
</code></pre>

<p>I would be very happy to find some tutorial about the use of SerialPort other than the MSDN website, because it is just listing the properties and the example code is not so easily understandable. And for some reason I cannot find too much for c++, only for c#, but I cannot switch to c# because of a later project.</p>

<p>Thanks in advance for any help.</p>