Windows Serial C++ using playground example.

I'm continuing to work with the Arduino, but have run into another problem with Serial communications. I'm using the example on the Arduino Playground linked from another thread (http://www.arduino.cc/playground/Interfacing/CPPWindows). But I can't seem to get it to display what the Arduino is showing. I've tried several iterations of my C++ program and get different (strange) results each time. In its current form, it just prints out 1 over and over again, even when the Arduino is sending the data. I've tested and it is definitely connecting to the Arduino, and the Arduino's TX LED lights up when it's sending.

void setup() 
 {
  Serial.begin(9600); 
  pinMode(44, INPUT);  
 }
 void loop() 
 {
   if(digitalRead(44)==HIGH) //if a button is pressed
   {
     Serial.print(5); //prints the number 5 to the Serial port
   }    
 }
int main()
{
      Serial ard("COM4"); //construct the Serial class for COM4
      char *buffer = ""; //initialize buffer
      while(ard.IsConnected()) //While there is a connection
      {
            cout<<"connected"<>bytes; //an input so the program stops and waits
                  bytes = 0; //resets bytes
            }
      }      
}

Thanks for any help in solving what I'm doing wrong. This is my first time programming something with a communications connection so it's probably something I've overlooked. Thanks in advance, Matt

The array that is passed into ReadData as the first argument needs to be defined larger enough to hold the number of characters passed in as the 2nd argument. The variable you are specifying as the first argument is sized to hold one character. Try defining buffer like this:

char buffer[256];

Then, change the 256 in the ReadData call to 255.

Thanks a lot! That seems to have solved the immediate problem. Now I have two more: the first is the Arduino was writing to the serial port as long as the button was down, which from the Arduino's perspective was several full cycles I guess so I ended up with lots of the data printed to the buffer array. I solved it by adding a "reset" button that until pushed wouldn't allow the further transmission of serial data.

The second problem however is what to do with the junk in the buffer array. When I print it to the console, I get weird characters except for what my sent data replaced. Is there a way using this Serial class to only read a certain number of bytes or just the received bytes? Or should I send the data from the Arduino as and just read the buffer from the < to the > after it's received? I unfortunately a not a Windows programmer and so am stumbling around a bit with the Serial class and am a tad afraid to touch anything lest it stop working or I can't put it back the way it was. Thanks again for the help! Matt

Isn't it telling you how many bytes it read?

bytes = ard.ReadData(buffer, 256)

Add

buffer[bytes] = '\0';

in the if block to NULL terminate the array.

I unfortunately a not a Windows programmer and so am stumbling around a bit with the Serial class and am a tad afraid to touch anything lest it stop working or I can't put it back the way it was.

Make a change. Test it. If it works, make a backup. If not, restore the previous code.