Is it possible to use IO Completion Ports for Serial I/O? According to Windows via C/C++ it is alluded to that it is possible, and does give an example of using IOCP with physical files showing work with CreateFile, ReadFile, WriteFile, etc. However can this actually work with serial comms - has anyone got it working?
I can't find any examples of this on the web, but I cannot be the first to attempt it?
-
Yes, using I/O Completion Ports for Serial I/O works fine. There is some setup work needed to create a file handle for a serial port that is appropriate for IOCP. But once the setup is done, you can do asynchronous
ReadFile()andWriteFile()operations just like with regular file handles and socket handles.The setup is basically:
- Open serial port with
CreateFile()passing in theFILE_FLAG_OVERLAPPEDvalue as thedwFlagsAndAttributesparameter. - Modify the serial port state as desired using
GetCommState()andSetCommState(). Do this just like you would do when not using IOCP. - Use
GetCommTimeouts()andSetCommTimeouts()to turn off total-timeouts for read operations, since it typically doesn't make sense to have timeouts for asynchronous operations. (You would instead explicitly callCancelIO()to cancel a read operation instead.) Turning off total-timeouts is done by setting theReadTotalTimeoutMultiplierandReadTotalTimeoutConstantfields of theCOMMTIMEOUTSstructure to zero.
Now you can use the handle with IOCP just like you would do with regular file handles and socket handles. I.e. attach the handle to a completion port using
CreateIoCompletionPort(), initiate I/O operations withReadFile()orWriteFile()using anOVERLAPPEDstructure, dequeue completed, failed or canceled operations from the completion port using theGetQueuedCompletionStatus()function.Additional serial port specific events can also be retrieved asynchronously using the
WaitCommEvent()function.graham.reeds : Ahh. I wasn't using GetCommTimeouts which was probably where I was going wrong - I could form a connection but not pass anything across.Gili : CancelIo() is completely broken. See http://stackoverflow.com/questions/3921111/how-to-find-out-when-cancelio-is-done for more information. - Open serial port with
0 comments:
Post a Comment