Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32, Serial Port Read/Write and OVERLAPPED

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?

like image 263
jww Avatar asked Dec 27 '22 17:12

jww


1 Answers

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

Serial Communications

like image 66
Remy Lebeau Avatar answered Feb 05 '23 16:02

Remy Lebeau