Re: Locking a COM port
nobody_at_noplace.not
Date: 07/07/04
- Next message: David Reeve: "Re: Splash Screen Problem"
- Previous message: Glenn Someone: "Re: Locking a COM port"
- In reply to: AlanGLLoyd: "Re: Locking a COM port"
- Next in thread: AlanGLLoyd: "Re: Locking a COM port"
- Reply: AlanGLLoyd: "Re: Locking a COM port"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 07 Jul 2004 10:30:43 -0400
On 07 Jul 2004 07:36:53 GMT, alanglloyd@aol.com (AlanGLLoyd) wrote:
>I appreciate the asynchronous natiure of overlapped files, but at which level
>and where does any activity surrounding an overlapped file become synchronous.
>At any CPU atomic operation, or at some higher point (which I don't know),
>possibly above the OS level and in the application. How can I (if I can)
>control the asynchronicity of any operation. That's where the fuzziness of my
>understanding lies.
Ok... it becomes synchronous at the driver level where the driver must
manage it's own data stream as a single entity to be sent to a device.
The most common example of this is the print queue. Programs add documents
to the queue, and then go on about their business... the printer driver is
responsible for getting the queued data out to the printer without screwing
it up. (OK that's oversimplification, but it demonstrates the concept)
For you as a programmer the difference is quite simple... when you do not
use overlapped i/o the procedure is going to wait for the operation to
complete before it returns to your program. If you are using overlapped,
the operation will go off, stuff the data in a buffer someplace, tell the
device driver about it and immediately return control to your program
*without waiting for the I/O operation to complete*.
Lets say you are sending lines of text to a printer on a serial port...
Back in the good ole DOS days, you would write a subroutine that would
monitor CTS and send bytes until the printer toggled CTS to signal buffer
full. You would then wait until CTS returned high again and begin sending
more data... wait, send, wait, send... and all the while your program is
stalled waiting for this to finish.
In Windows machines this has been moved to the device driver which sends the
text from a reserved memory buffer. All you as a programmer have to do is
put the text in the buffer and inform the driver it's there... which is done
by WriteFile. As far as we are concerned the operation is complete when the
data is placed in the memory buffer, the driver takes over from there and
finishes the job for us. This is a necessity in multitasking environments
if we don't want entire networks brought to their knees by an empty paper
tray.
Without Overlapped... you use write file to put data into the buffer, and it
will wait for the driver to dispose of the data before returning to your
program.
With overlapped... you use writefile to put data into the buffer and it
returns immediately to your program trusting the driver will adequately
dispose of the data. It queues the data.
As you can imagine using overlapped and trusting device drivers can
significantly increase the performance of your programs... but it also comes
with something of a price...
You have to be very careful about doing some things... For example:
Without overlapped...
Writefile, prepare data, writefile, prepare data, writefile... close port.
This is safe to do because writefile doesn't return until the data is
handled.
With overlapped...
Writefile, prepare data, writefile, prepare data, writefile... wait for data
to be handled... close port.
The extra step is required because writefile didn't wait around for the IO
operation to complete itself... now you have data in the buffers that is not
handled, closing the port immediately after the last writefile would
truncate the data stream before everything is sent.
This wait-for-completion process is acommplished by Signals. Signals let you
synchronize operations to events. Rather than checking CTS on the port, like
we did on DOS, now we set up a signal that goes on when the data buffer is
empty. Check in the Windows SDK for a more or less detailed explanation.
-----
Laura
- Next message: David Reeve: "Re: Splash Screen Problem"
- Previous message: Glenn Someone: "Re: Locking a COM port"
- In reply to: AlanGLLoyd: "Re: Locking a COM port"
- Next in thread: AlanGLLoyd: "Re: Locking a COM port"
- Reply: AlanGLLoyd: "Re: Locking a COM port"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|