Serial port reading

O

ORC

I have made a serial port class inspired bu the MTTTY example and the serial
port class from OpenNETCF . below is the code from the readfile method in
which there is a problem. The code calls the WaitForSingleObject function
but always locks there until the timeout happens (GetOverlappedResult is
never called). If data is present in the buffer before I call this method
everything works well (readfile return immediately with the data). Can any
body point to the possible problem ?

OVERLAPPED readOverlapped = new OVERLAPPED();
IntPtr readOverlappedPtr = IntPtr.Zero;
ManualResetEvent readEvent = new ManualResetEvent(true);
UInt32 bytesRead = 0;

readOverlappedPtr = LocalAlloc(0x40, Marshal.SizeOf(readOverlapped));
readOverlapped.Offset = 0;
readOverlapped.OffsetHigh = 0;
readOverlapped.hEvent = readEvent.Handle;
Marshal.StructureToPtr(readOverlapped, readOverlappedPtr, true);
if (!api.ReadFile(hCommPort, buffer, (UInt32)count, ref bytesRead,
readOverlappedPtr))
{ // readfile didn't finish immediatly so wait for it to finish
if(Marshal.GetLastWin32Error() == ERROR_IO_PENDING)
{ // no error occurred so wait for readfile to finish
if (api.WaitForSingleObject(hCommPort, readTimeout) ==
WAIT_OBJECT_0)
{ // Check for number of bytes actually received
api.GetOverlappedResult(hCommPort, readOverlappedPtr,
ref bytesRead, false);
}
}
}

Your help is highly appreciated !

Thanks
Ole
 
O

ORC

OK - got it to work - I did set the eventhandle in WaitForSingleObject to
the handle to the serial port (big mistake!)

Thanks anyway!
Ole
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top