title: Serial port ReadFile reads 0 bytes and returns true
body: <p>I'm trying to read in data from a serial port in Windows 7 using the Windows API. When I try to read in data, the <code>WaitCommEvent()</code> fires just fine and the <code>ReadFile()</code> call returns 1 as the status, but no data is read in. In the the <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa365467(v=vs.85).aspx" rel="nofollow">ReadFile documentation</a> it says that:</p>
  
  <blockquote>
    <p>When a synchronous read operation reaches the end of a file, <code>ReadFile</code> returns <code>TRUE</code> and sets <code>*lpNumberOfBytesRead</code> to zero.</p>
  </blockquote>
  
  <p>However, I'm sure there are no <code>EOT</code> characters in the data being sent over the serial port. </p>
  
  <p>I currently have two USB cables plugged into my computer and connected to each other. I know that they can send and receive data as I have tested them with Putty. </p>
  
  <p>Why won't <code>ReadFile()</code> read in any data? </p>
  
  <p>My code is below. </p>
  
  <p>Header:
  </p>
  
  <pre><code>typedef struct uart_handle
  {
     uint8_t port_num;
     char port_name[10];
     uint32_t baud_rate;
     uint8_t byte_size;
     uint8_t stop;
     uint8_t parity;
     int32_t error;
     HANDLE handle;
  } uart_handle;
  </code></pre>
  
  <p>Main file:
  </p>
  
  <pre><code>uart_handle* serial_comm_init(uint8_t port_num, uint32_t baud_rate, uint8_t byte_size, uint8_t stop, uint8_t parity)
  {
     uart_handle*  uart;
     DCB           uart_params = { 0 };
     COMMTIMEOUTS  timeouts    = { 0 };
     int           status;
  
     uart         = (uart_handle*) malloc(1 * sizeof(uart_handle));
     status       = 0;
  
     // Set port name
     if (port_num &gt; 9)
     {
        sprintf(uart-&gt;port_name, "\\\\.\\COM%d", port_num);
     }
     else
     {
        sprintf(uart-&gt;port_name, "COM%d", port_num);
     }
  
     // Set baud rate
     uart-&gt;baud_rate = baud_rate;
  
     // Set byte size
     uart-&gt;byte_size = byte_size;
  
     // Set stop bit
     uart-&gt;stop = stop;
  
     // Set parity
     uart-&gt;parity = parity;
  
     // Set up comm state
     uart_params.DCBlength = sizeof(uart_params);
     status = GetCommState(uart-&gt;handle, &amp;uart_params);
     uart_params.BaudRate = uart-&gt;baud_rate;
     uart_params.ByteSize = uart-&gt;byte_size;
     uart_params.StopBits = uart-&gt;stop;
     uart_params.Parity   = uart-&gt;parity;
     SetCommState(uart-&gt;handle, &amp;uart_params);
  
     // Setup actual file handle
     uart-&gt;handle = CreateFile(uart-&gt;port_name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
     if (uart-&gt;handle == INVALID_HANDLE_VALUE) {
        printf("Error opening serial port %s.\n", uart-&gt;port_name);
        free(uart);
        return NULL;
     }
     else {
        printf("Serial port %s opened successfully.\n", uart-&gt;port_name);
     }
  
     // Set timeouts
     status = GetCommTimeouts(uart-&gt;handle, &amp;timeouts);
     timeouts.ReadIntervalTimeout         = 50;
     timeouts.ReadTotalTimeoutConstant    = 50;
     timeouts.ReadTotalTimeoutMultiplier  = 10;
     timeouts.WriteTotalTimeoutConstant   = 50;
     timeouts.WriteTotalTimeoutMultiplier = 10;
     status = SetCommTimeouts(uart-&gt;handle, &amp;timeouts);
     if (status == 0) {
        printf("Error setting comm timeouts: %d", GetLastError());
     }
  
     return uart;
  }
  
  int32_t serial_comm_read(void* handle, uint8_t* msg, uint32_t msg_size, uint32_t timeout_ms, uint32_t flag)
  {
     uart_handle*  uart;
     uint32_t      num_bytes_read;
     uint32_t      event_mask;
     int32_t       status;
  
     uart            = (uart_handle*) handle;
     num_bytes_read  = 0;
     event_mask      = 0;
     status          = 0;
  
     memset(msg, 0, msg_size);
  
     // Register Event
     status = SetCommMask(uart-&gt;handle, EV_RXCHAR);
  
     // Wait for event
     status = WaitCommEvent(uart-&gt;handle, &amp;event_mask, NULL);
  
     printf("Recieved characters.\n");
  
     do {
        status = ReadFile(uart-&gt;handle, msg, msg_size, &amp;num_bytes_read, NULL);
        printf("Status: %d\n", status);
        printf("Num bytes read: %d\n", num_bytes_read);
        printf("Message: %s\n", msg);
  
     } while (num_bytes_read &gt; 0);
  
     printf("Read finished.\n");
  
     return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Serial port COM9 opened successfully.
  Recieved characters.
  Status: 1
  Num bytes read: 0
  Message:
  Read finished.
  </code></pre>