First Arduino C++ Interface

Hello, I am very new to the Arduino world and am having trouble getting the C++ interface to work properly. The code for the arduino is

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println("Hello World");
  delay(1000);
}

This just outputs the phrase "Hello World" over and over and over again, once ever second. I'm trying to write a C++ program to input data from the USB port, such that it displays "Hello World" once a second, synced with the Arduino. I'm getting a return of -1 for status, and I'm not reading the data. I'm trying to use the C++ library availible from the Software Interfacing section of Playground. Here's the C++ snippet, hopefully someone can tell me what I'm doing wrong. It's probably a very nooby mistake.

      char *buffer = "";
      int status = 0;
      Serial test(LPCWSTR("COM4"));
      while(1)
      {
            status = test.ReadData(buffer, 11);
            printf("Port Says: %s\n", buffer);
            printf("Status = %i", status);
            Sleep(1000);
      }

Unfortunately, I cannot help much with the specific problem but I have some general comments...

  • Can you get "Hello World" to show up in the Arduino IDE Monitor? How about a terminal program (like Hyperterminal)?
  • buffer needs to be declared to allocate storage. (example below)
  • Before calling printf, the string in buffer will need to be terminated. (example below)
  • Casting the parameter to the Serial constructor is not necessary. Remove "LPCWSTR". (example below)
  • Step into ReadData. Is the ReadFile executed?

Example...

  char buffer[40];
  int status = 0;
  Serial test("COM4");

  while(1)
  {
    status = test.ReadData(buffer, 11);
    if ( status <> -1 )
    {
      buffer[status] = 0;
      printf("Port Says: %s\n", buffer);
    }
    printf("Status = %i", status);
    Sleep(1000);
}

Hmm...I was recently struggling with the same problem. I didn't know of the serial class on the playground. That may have saved me some time. However I found this wrapper and got it to work.

I posted a simple test application here. Perhaps it may be of use.