C++/VB - ReadFile still not working in thread

Asked By vandenberg_greg
20-Jan-10 09:17 PM
Andreas,

I tried the suggestions of increasing the read thread's priority to highest,
and double checked my ReadModem function that uses an overlapped read and I
still cannot see why it is not getting any data.  The WaitForSingleObject
always times out before it gets WAIT_OBJECT_0. If I set it the timeout to
INFINITE it just sits there. Do you have any ideas, suggestions etc. Please
help.

Greg

int CTapiModem::ReadModem(unsigned char *buf, int *count, int timeout)
{
// read "count" bytes at a time from modem

DWORD dwReadLen =0;
int nRet;
char *szText;

OVERLAPPED ov = {0, 0, 0, 0, NULL}; // Initialization is important!;
//createEvent parameters:
//1 - pointer to security attibutes structure
//2 - manual reset
//3 - initial state
//4 name of event object
ov.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
ov.Offset = 0;
ov.OffsetHigh = 0;

if (m_hSerialHandle != NULL)
{
nRet = ReadFile(m_hSerialHandle,buf,*count,&dwReadLen,&ov);

if (!nRet)
{
nRet = GetLastError();

if(nRet != ERROR_IO_PENDING)
{
GetErrorString(nRet,szText);
if (nRet == ERROR_INVALID_HANDLE)
{
return 1;
}
}
}

if (WaitForSingleObject(ov.hEvent,timeout) == WAIT_OBJECT_0)
{
GetOverlappedResult(m_hSerialHandle, &ov, &dwReadLen,FALSE);
}
else
{
return 2; //timeout elapsed, objects state = non-signalled
// = buffer empty
}
if (dwReadLen == 0)
{
return 3;
}
else
{
*count = dwReadLen;
return 0;
}
}
else
{
*buf = NULL;
return 5; //m_hSerialHandle == NULL
}

}
TAPIaroundTheWorld
(1)
ReadIntervalTimeout
(1)
GetOverlappedResult
(1)
SetCommTimeouts
(1)
GetLastError
(1)
CTapiModem
(1)
ExMVP
(1)
HSerialHandle
(1)
  vandenberg_greg replied to vandenberg_greg
25-Jan-10 11:15 PM
So is my problem with readfile not a tapi issue but communication port issue?
I have 5 threads going on at once. Is this too mamy threads? This is really
frustrating.
  Andreas Marschall [exMVP TAPI] replied to vandenberg_greg
26-Jan-10 08:49 AM
Greg,
I guess so.
TAPI / UniModem TSP is only providing the comm handle.


I do not think so.
What CPU usage does Task-Manager show?
In TaskMgr you can add colums for #handles, #threads, cpu% etc.

--
Best Regards
Andreas Marschall
Microsoft MVP for TAPI / Windows SDK / Visual C++ 2003-2008
TAPI / TSP Developer and Tester
My TAPI and TSPI FAQ:
http://www.I-B-A-M.de/Andreas_Marschall's_TAPI_and_TSPI_FAQ.htm
My Toto® Tools (a collection of free, mostly TAPI related tools):
http://www.i-b-a-m.de/Andreas_Marschall's_Toto_Tools.htm
TAPI development around the world (Frappr! map):
http://www.frappr.com/TAPIaroundTheWorld
* Please post all messages and replies to the newsgroup so all may
* benefit from the discussion.  Private mail is usually not replied to.
* This posting is provided "AS IS" with no warranties, and confers no
rights.
  vandenberg_greg replied to Andreas Marschall [exMVP TAPI]
26-Jan-10 09:27 PM
Andreas,

I looked at the task manager performance window and the CPU usage was around
15~20%. I looked in the Processes window and my applications CPU usage was
around 5~8%, Memory usage 8676 K, Handles around 200, and Threads were 8.
I created 5 threads plus the GUI. I do not know where the other 2 threads
came from.
I use Sleep() in some of the threads to keep the cpu usage down, is this a
problem?

I am thinking of writing a simple program to go through the login sequence
with the calling program, then just have a read thread and a write thread to
see if I can get any data.

Is there a Discussion Group for readfile comm issues?

Thanks,
Greg
  vandenberg_greg replied to vandenberg_greg
06-Apr-10 12:05 AM
After much web study and experimentation I was finally able to get readfile
to read data. I needed to do a ResetEvent to my overlapped structures before
each readfile and writefile, and I had to call SetCommTimeouts to set the
ReadIntervalTimeout to 100 ms.
  vandenberg_greg replied to vandenberg_greg
06-Apr-10 01:24 PM
After much web study and experimentation I was finally able to get readfile
to read data. I needed to do a ResetEvent to my overlapped structures before
each readfile and writefile, and I had to call SetCommTimeouts to set the
ReadIntervalTimeout to 100 ms.
help
When to call GetOverlappedResult()? C++ / VB In looking over the docs and sample applications on the MSDN site it's not obvious to me under what circumstances I should be calling GetOverlappedResult(). After some debugging I think I have the logic correct, but I want to confirm I'm not missing any other cases. The docs and samples seem to indicate that GetOverlappedResult() only needs to be called when one of the API's which take an OVERLAPPED FALSE and GetLastError() returns ERROR_MORE_DATA appears to be another case where you need to call GetOverlappedResult() because the ReadFile() call did not return the number of bytes read. Am I correct about this? Are there any other cases in which I should be calling GetOverlappedResult()? - - Thanks, Nick nicknospamdu@community.nospam remove "nospam" change community. to msn.com Win32 Kernel Discussions OVERLAPPED.Internal (1) FileStream.BeginRead (1) FileStream.EndRead (1) GetQueuedCompletionStatus (1) CreateIoCompletionPort (1) LpNumberOfBytesRead (1) GetOverlappedResult (1) Hi Nick, Are you using I / O completion port in your application? There are several ways to use I / O completion port in application. Using GetOverlappedResult() is an old fashion way and is not recommended because it can not achieve high multithreading performance such as scalability etc. . . Below is the pseudocode of GetOverlappedResult(), as you can see it internally uses WaitForSingleObject() API which will cause the calling thread
GetOverlappedResult C++ / VB Hi everybody, I've got a question. I have to read and send using overlapped structure. When I read from the device everything works correctly except sometimes that GetOverlappedResult returns without errors, but the array of byte resulting from the reading operations has no the problem? Thanks to everybody and sorry for my bad english L Win32 Kernel Discussions GetOverlappedResult (1) ReadBuffer (1) ApiStructures (1) ApiFunctions (1) SECURITY_ATTRIBUTES (1) GetLastError (1) ReadedByteNumber (1) LpSecurityDescriptor (1 ref overlapped); if (result = = 0) / / error in reading { if (ApiFunctions.GetLastError() = = ApiConstants.ERROR_IO_PENDING) { result = ApiFunctions.GetOverlappedResult(readHandle, ref overlapped, ref readedByteNumber, true); if (result ! = 0) / / ok { return readbuffer; / / works correctly, but and in reading an empty message (array filled with 0). Best regards Luca Scabau keywords: GetOverlappedResult description: Hi everybody, I've got a question. I have to read and send messages
write it to my device. The entire sequence (wait for buffer full, then DeviceIoControl, then GetOverlappedResult) should take no more than 10 milliseconds. / / / / / / / / / / / / / / / / / / / / BEGIN SNIPPET / / / / / / / / / / / / / / / / / / / / / / / / / ResetEvent(context-> ahEvents[OVERLAPPEDWRITE]); int na WaitForMultipleObjects(2, context-> ahEvents, FALSE, INFINITE); { if (dw = = WAIT_OBJECT_0+1) / / overlapped i / o event { rc = GetOverlappedResult(hDevice, &(context-> overlapped), νmBytesRead, FALSE); if (!rc) HandleLE(GetLastError(), "GetOverlappedResult()"); } else / / thread stop event or error { HandleLE(GetLastError(), "WaitForMultipleObjects()"); if (CancelIo(hDevice) = = 0) / / cancel pending diagnostics and measurements revealed that this time is not spent in DeviceIoControl() and not in GetOverlappedResult() but rather in WaitForMultipleObjects(). My (current) conclusion is that this is due to preemption. Intuitively in order for "my socket handling sequence" to take less than 10ms, WaitForMultipleObjects (and thus GetOverlappedResult) should move elsewhere (possibly to another thread?) If so, how do I do that? It move on to other things? Create (yet another) thread, dedicated only for this WaitForMultipleObjects and GetOverlappedResult? If you could provide a link to somewhere it is being done properly (or a use DeviceIoControl in a "fire and forget" manner (in overlapped mode!), without checking for what GetOverlappedResult() returns? Thanks, Chris I think you don't understand why WaitForMultipleObjects returns late. Most likely use DeviceIoControl in a "fire and forget" manner (in overlapped mode!), without checking for what GetOverlappedResult() returns? Thanks, Chris What would you like to move on to? This will determine how
test C++ / VB test Win32 TAPI Discussions TAPI (1) Andreas_Marschall (1) S_TAPI_and_TSPI_FAQ (1) TAPIaroundTheWorld (1) S_Toto_Tools (1) MSFTNGP (1) TLqFJHA (1) TestRe (1) Daniel, for testing please don't de / Andreas_Marschall's_Toto_Tools.htm TAPI development around the world (Frappr! map): http: / / www.frappr.com / TAPIaroundTheWorld * Please post all messages and replies to the newsgroup so all may * benefit from the