user3386: 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->hSerial, &this->errors, &this->status);

    //Check if there is something to read
    if(this->status.cbInQue>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->status.cbInQue>nbChar)
        {
            toRead = nbChar;
        }
        else
        {
            toRead = this->status.cbInQue;
        }

        //Try to read the require number of chars, and return the number of read bytes on success
        if(ReadFile(this->hSerial, buffer, toRead, &bytesRead, NULL) && 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 <iostream>

using namespace std;

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

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

    cout << "\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?

user4202: char* c = "";
while(c == "")

The variable named c is a misnomer, because it is a pointer to a string, not a character.
For example c = 0x123456.
You should use a more explicit name for your variable.
This first line assigns a pointer to an empty string to the c pointer.
So, the value stored at address 0x123456 == whatever pointer to a static memory address containing '\0'
Note that no memory has been allocated to hold characters. Only a pointer to char has been allocated. Thus you cannot store anything in your string!!
You could write something like:
char data[2] = "";
That would allocate a pointer, and a string to hold 2 characters

The second line, while(c == "") compares a real memory pointer (e.g. 0x123456) with a compiler pointer.
"" is not a real application pointer, you should not manipulate it.
So that second line relies on a compiler behavior.

If I were you I would wrap the multi-char routine into a single-char routine. For example:
int ReadChar(char *c)
{
  return ReadData(c, 1);
}

Then:
main()
{
  char c;
  ReadChar(&c);
}


On a different note, I think the compiler should catch such error.
The line c == "" would never do anything useful and should be caught.

user84: 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.

3386: <p>I made a super simple arduino uno sketch to send a serial byte once every second:</p>

<pre><code>void setup(){
  Serial.begin(9600);
}

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

<p>My arduino is hooked up to com 3.</p>

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

<pre><code>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;

}
</code></pre>

<p>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:</p>

<pre><code>#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");
}
</code></pre>

<p>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.</p>

<p>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.</p>

<p>Can someone please help me figure out why this is not working?</p>
 4202: <pre><code>char* c = "";
while(c == "")
</code></pre>

<p>The variable named c is a misnomer, because it is a pointer to a string, not a character.
For example c = 0x123456.</p>

<p>You should use a more explicit name for your variable.</p>

<p>This first line assigns a pointer to an empty string to the c pointer.
So, the value stored at address 0x123456 == whatever pointer to a static memory address containing '\0'</p>

<p>Note that no memory has been allocated to hold characters. Only a pointer to char has been allocated. Thus you cannot store anything in your string!!</p>

<p>You could write something like:
<code>char data[2] = "";</code>
That would allocate a pointer, and a string to hold 2 characters</p>

<hr>

<p>The second line, <code>while(c == "")</code> compares a real memory pointer (e.g. 0x123456) with a compiler pointer.
<code>""</code> is not a real application pointer, you should not manipulate it.</p>

<p>So that second line relies on a compiler behavior.</p>

<hr>

<p>If I were you I would wrap the multi-char routine into a single-char routine. For example:</p>

<pre><code>int ReadChar(char *c)
{
  return ReadData(c, 1);
}
</code></pre>

<p>Then:</p>

<pre><code>main()
{
  char c;
  ReadChar(&amp;c);
}
</code></pre>

<hr>

<p>On a different note, I think the compiler should catch such error.
The line <code>c == ""</code> would never do anything useful and should be caught.</p>
 84: <p>The problem is in your C++ program at the other end of the line (not the Arduino side):</p>

<pre><code>char* c = "";
int len = strlen(c);
while(c == "")
{
    serial.ReadData(c, len);
}
</code></pre>

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

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

<pre><code>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;
    }
}
</code></pre>

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

<pre><code>// either:
#define LEN 1
// or:
const int LEN = 1;
</code></pre>

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