3
votes

I'm trying to put together a proof of concept for reading/writing to a serial port and I am having problems with design/concept. I don't want to poll, so I'm trying to use Overlapped I/O. In this case, it's not a multi-threaded application - it just uses efficient waits.

  1. Open port (with OVERLAPPED)
  2. SetCommState (set port baud, parity, CTS/RTS, etc)
  3. SetCommMask (EV_BREAK, EV_CTS, EV_DSR,..., EV_RXCHAR, EV_RXFLAG, EV_TXEMPTY)
  4. Create OVERLAPPED structures and Events
  5. WriteFile (to port)
  6. WaitCommEvent (due to write)
  7. WaitCommEvent (due to read)
  8. ReadFile (from port)
  9. Check read buffer

The MSDN example is kind of lame (Monitoring Communications Events). I'm currently using two events, but the ReadFile step is failing with ERROR_INVALID_PARAMETER.

In the steps above, do I need one or two overlapped structures? If one OVERLAPPED structure, are the steps WriteFile, WaitCommEvent (for write), reset event, WaitCommEvent (for read), ReadFile?

1
You need to show your actual code, not a description of your code, if you expect anyone to be able to help you. Without seeing what parameters you are actually passing to ReadFile(), how do you expect to get help figuring out why it is complaining about them?Remy Lebeau

1 Answers

9
votes

Have a look at this article, it is much more thorough about how to use overlapped I/O with serial ports:

Serial Communications