Interfacing Arduino with Windows via C++

So I recently "modded" my computer desk with some RGB LED strips under it to light up in colors. Right now, I have it going through the colors of the spectrum in a little program I wrote using the FastSPI library.

Now I want to challange my self a little bit more. I decided that I want to be able to control the light and different modes from my computer. I am a rpetty advanced C++ programmer, and want to use it along with SFML (for graphics) in order to talk to the Arduino via USB-Serial using the program I'm currently making.

I found a video on YouTube, this one. I walked through how to get the external header files and source into place. Easy enough. Then, Visual Studio 2013 gave me some problems but I fixed them and got it working. I could finnaly communicate though C++! Now I just have one problem. In the video and the example code, it only sends one character. As I need full 0-255 RGB, I am required to send 9 chars from the C++ program to the Arduino. An example of what I would be sending is this: '2' '5' '5' '0' '0' '0' '2' '5' '5'. Thats some 2's, some 5's and some 0's, which would make 255 red (full red light), 0 green (no green light) and 255 blue (full blue light), which would make the RGB LED strip light up in a dark purple color.

So 9 chars in total. I've tried doing something like this in the C++ side of things, which would send all 9 items in the colorData array.

for (int i = 0; i < 9; i++){
		comm.send_data(colorData[i]);
	}

and on the Arduino, this is where I think I have messed up. I'm not really sure how I am going to collect all this serial data. This is what the code looks like

while(Serial.available() > 9){
    for(int i=0; i<9; i++)
      colorData[i] = Serial.read();  
    }

which theoretically should read the Serial data one at a time and put that into the colorData array on the Arduino.

Any help would be apprechiated, thanks!

That is not the best way to do it, but for sure there is a problem here:

while(Serial.available() > 9){

You are sending in "packets" of 9 bytes but in this statement you are looking for at least ten bytes before you try to read the "packet"
Try this:

while(Serial.available() >= 9){

Pete

Ah, yes. I was just in the mind of arrays all day so I was thinking that it started with 0.

If this isn't the best way, how do you recommend doing it?

When you start reading on the Arduino, you can't be certain that the first character you see was the first one sent by the PC. There may, for example, have been a byte or two of noise when the connection is first established. A simple way to fix it would be to make each "packet" ten bytes long by preceding the 9 chars with one that is easily distinguishable from those in the actual data that you want. In your case, since the 9 chars are all numeric you can pick, for example, the tilde character ~.
Your packets would be in this format: ~255000255
Change your code so that you don't start storing characters in the colorData array until you have seen ~. If you see ~ while you are reading the next 9 characters into colorData, you give up on the current packet and read the next one. This way you will only act upon valid packets.

Pete

Thank you! I got it working now and it doesn't mess up like before. You're a hero :kissing:

The examples in serial input basics are simple reliable wasy to receive data. There is also a parse example.

...R