Why can't this C++ program read my arduino's Serial.write()? I made a super simple arduino uno sketch to send a serial byte once every second:

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

void loop(){
   Serial.write(12); // send a byte with the value 12
   delay(1000);
}


My arduino is hooked up to com 3.

On the other end of the line, I have a C++ program with the following read function:

int Serial::ReadData(char *buffer, unsigned int nbChar)
{
    //Number of bytes we'll have read
    DWORD bytesRead;
    //Number of bytes we'll really ask to read
    unsigned int toRead;

    //Use the ClearCommError function to get status info on the Serial port
    ClearCommError(this-&gt;hSerial, &amp;this-&gt;errors, &amp;this-&gt;status);

    //Check if there is something to read
    if(this-&gt;status.cbInQue&gt;0)
    {
        //If there is we check if there is enough data to read the required number
        //of characters, if not we'll read only the available characters to prevent
        //locking of the application.
        if(this-&gt;status.cbInQue&gt;nbChar)
        {
            toRead = nbChar;
        }
        else
        {
            toRead = this-&gt;status.cbInQue;
        }

        //Try to read the require number of chars, and return the number of read bytes on success
        if(ReadFile(this-&gt;hSerial, buffer, toRead, &amp;bytesRead, NULL) &amp;&amp; bytesRead != 0)
        {
            return bytesRead;
        }

    }

    //If nothing has been read, or that an error was detected return -1
    return -1;

}


This seems to check only one time if data is available, so obviously I have to loop through it until data comes in.  I did that in my main program:

#include "Serial.h"
#include &lt;iostream&gt;

using namespace std;

int main(void)
{
    Serial serial("COM3");

    char* c = "";
    int len = strlen(c);
    while(c == "")
    {
        serial.ReadData(c, len);
    }

    cout &lt;&lt; "\n\n";
    system("PAUSE");
}


When I do this, my C++ program gets stuck in an infinite loop waiting for data from the arduino, and I cannot figure out why this is happening.  My program never sees the data the arduino is sending.

This Serial class works and is configured properly because I can SEND all the data I want to the arduino without any problems.  My issues are only with reading back from the arduino.

Can someone please help me figure out why this is not working?
The problem is in your C++ program at the other end of the line (not the Arduino side):

char* c = "";
int len = strlen(c);
while(c == "")
{
    serial.ReadData(c, len);
}


Here, len is zero because that is the length of "" , the empty string! Thus, in your loop, you ask ReadData to read 0 bytes. Hence, the condition of your while loop is always true as, c never changes.

Also, the while condition seems incorrect as it is not recommended in C++ to compare 2 strings (defines as char * or char [...]) with ==. In your situation, what is important is the number of characters read by ReadData:

char c[LEN + 1];
int numBytes = 0;
while(true)
{
    numBytes = serial.ReadData(c, LEN);
    if (numBytes != -1)
    {
        // Terminate the string if we want to use c variable as a string
        c[numBytes] = 0;
        break;
    }
}


in the code above, LEN is the number of characters you want to read at one time, in you example it should be set to 1, either as a #define or as a const variable:

// either:
#define LEN 1
// or:
const int LEN = 1;


The code above also ensures that c can be used as a normal string, by adding a 0 at the end. This is mandatory only if you want to use string functions on c such as strlen, strcpy, strchr... otherwise numBytes is enough to manage the number of characters read into c.