Click here to Skip to main content
16,007,932 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Template class question... Pin
Matt Gullett14-Dec-02 16:03
Matt Gullett14-Dec-02 16:03 
GeneralRe: Template class question... Pin
Taka Muraoka14-Dec-02 16:17
Taka Muraoka14-Dec-02 16:17 
GeneralRe: Template class question... Pin
Matt Gullett14-Dec-02 16:45
Matt Gullett14-Dec-02 16:45 
GeneralRe: Template class question... Pin
Taka Muraoka14-Dec-02 16:55
Taka Muraoka14-Dec-02 16:55 
GeneralRe: Template class question... Pin
Matt Gullett14-Dec-02 17:04
Matt Gullett14-Dec-02 17:04 
GeneralRe: Template class question... Pin
Todd Smith14-Dec-02 17:11
Todd Smith14-Dec-02 17:11 
GeneralRe: Template class question... Pin
Matt Gullett14-Dec-02 17:23
Matt Gullett14-Dec-02 17:23 
GeneralHelp on Overlapped Serial IO Pin
work_to_live14-Dec-02 13:44
work_to_live14-Dec-02 13:44 
I've been working on serial IO for a few days now. To creep up on it, I started out non overlapped, and it was a piece of cake. Then I decided to add a thread to handle the reads. My first version used a while(true) loop in the thread, and polled for data in the receive buffer. It worked fine, but I didn't like the fact that it was always looping. Here's where the fun begins... I changed to overlapped IO, and was amazed that my first version worked (I thought). Unfortunately, it only worked when I sent packets that were relatively small, less than 6 bytes. If I send more than 6 bytes, WaitForMultipleObjects won't return after the first read. I'll paste the thread's code at the end of this. A lot of it is commented out to make sure something (incorrect memory management or whatever) wasn't causing the problem.

Here's the result of my debug so far...
If I send data of just a few bytes, everything works as expcted. WaitForMultibleObjects returns whenever data is received, and waits again the next time it is called.

If I send the magic number of 9 bytes...
WaitForMultipleObjects returns execution to the thread.
I read data out of the port with ReadFile.
Loop back to WaitForMultipleObjects, and wait for more.
Now if I send another character, or string of chars, WaitForMultipleObjects does not return! I have another entry point to read the serial port from the main thread, and if I use it, I can read the data. In addition, when I read the data with that other entry point, the WaitForMultipleObjects function returns, but finds no data since the main thread read it (that's expected). From this point on, WaitForMultipleObjects won't return when characters are received by the port.

I tried clearing the threads lasterror before the ReadFile, and then checked the error after the ReadFile command, and get "command exited normally" or words to that effect.

The readfile is opened with 1000 byte buffers for both read and write.

Any help would be greatly appreciated! Smile | :)

Read Thread.................................................................
UINT CSerial::ReadPortThread(LPVOID lpvParam)
{
	CSerial *pSerial = (CSerial*)lpvParam;
        int bytesin;
	// Since we'll be waiting on multiple events, we'll use a handle
	// array, and create events for each element in the array. For now,
	// I'm only using "quit" and "rxchar" events.

	// Set up the threads loop events. i.e. events that will return
	// execution to the thread. Note that the CSerial Object houses the
	// event handles since it must assert the "quit thread" event (hReadEvents[0]
	pSerial->hReadEvents[0] = CreateEvent(NULL, true, false, NULL);
	
	// Create the rxchar event, and assign it to hReadEvents[1]
	pSerial->hReadEvents[1] = CreateEvent(NULL, true, true, NULL);
	
	// Set the hEvent element of the CSerial objects overlapped
	// structure equal to hReadEvents[1].
	pSerial->OverlappedRead.hEvent=pSerial->hReadEvents[1];

	// Set up the comm event mask to respond to received chars.
	// When we issue the WaitCommEvent, the receive comm port will assert the
	// OverlappedRead.hEvent event when it receives a char.
	SetCommMask(pSerial->m_hIDComDev,EV_RXCHAR);

	DWORD evt;

    DWORD dwHandleSignaled;
	SetLastError(0);  // debug
	while (true)
	{
		// Since this is an overlapped file, this function will return
		// immediately. Later, when a character is received, the comm
		// port will assert the OverlappedRead.hEvent event.
	    if (!WaitCommEvent(pSerial->m_hIDComDev,&evt, &pSerial->OverlappedRead))
		{
			if (ERROR_IO_PENDING!=GetLastError())
			{
			    TRACE("WaitCommEvent failed, exiting Read Thread\n");
			    return 0;
			}
			// We're waiting. Use WaitFor... and wait for the event
		}
		if (!ResetEvent(pSerial->OverlappedRead.hEvent))
		{
			TRACE("ResetEvent failed, exiting Read Thread\n");
			return 0;
		}
		dwHandleSignaled=WaitForMultipleObjects(2,
			   pSerial->hReadEvents,
			   FALSE,
			   INFINITE);
        // Which event occured?
        switch(dwHandleSignaled)
        {
            case WAIT_OBJECT_0 + 1:
				{// Chars are in the rcv port
                    if ( (bytesin=pSerial->ReadDataWaiting()) > 0)
					{
			            char buff[500];
						DWORD BytesRead;
			            // Get the data from the serial port
						TRACE("Before ReadFile ");
						pSerial->TraceLastError(GetLastError());
						SetLastError(0);
						ReadFile(pSerial->m_hIDComDev,
							     buff,bytesin,&BytesRead,
								 &pSerial->OverlappedRead);
						TRACE("After ReadFile ");
						pSerial->TraceLastError(GetLastError());
/* debug
			            //pSerial->ReadData(buff,bytesin);

			            // copy the data to the msg buffer
						CSerPortMsg *msg = new CSerPortMsg;
			            msg->SetBuffer(buff,bytesin);

			            // Call the virtual function the user will over-ride
			            // to handle this message.
			            pSerial->ReadThreadDataIn(msg);
*/
					}
					break;
				}
			case WAIT_OBJECT_0:
				{// Time to quit
					PurgeComm(pSerial->m_hIDComDev,PURGE_RXABORT | PURGE_RXCLEAR);
					return 0;
				}
		}
	}
} 

GeneralRe: Help on Overlapped Serial IO Pin
work_to_live14-Dec-02 14:21
work_to_live14-Dec-02 14:21 
GeneralRe: Help on Overlapped Serial IO Pin
Michael Dunn14-Dec-02 15:55
sitebuilderMichael Dunn14-Dec-02 15:55 
GeneralRe: Help on Overlapped Serial IO Pin
patk14-Dec-02 17:07
patk14-Dec-02 17:07 
GeneralRe: Help on Overlapped Serial IO Pin
work_to_live14-Dec-02 17:57
work_to_live14-Dec-02 17:57 
GeneralRe: Help on Overlapped Serial IO Pin
patk14-Dec-02 18:49
patk14-Dec-02 18:49 
GeneralRe: Help on Overlapped Serial IO Pin
work_to_live14-Dec-02 19:23
work_to_live14-Dec-02 19:23 
GeneralRe: Help on Overlapped Serial IO Pin
work_to_live14-Dec-02 20:01
work_to_live14-Dec-02 20:01 
GeneralRe: Help on Overlapped Serial IO Pin
patk15-Dec-02 2:35
patk15-Dec-02 2:35 
GeneralDeskband remove check Pin
14-Dec-02 13:33
suss14-Dec-02 13:33 
Generalprinting a linked list Pin
Anonymous14-Dec-02 12:06
Anonymous14-Dec-02 12:06 
GeneralRe: printing a linked list Pin
Christian Graus14-Dec-02 12:33
protectorChristian Graus14-Dec-02 12:33 
GeneralRe: printing a linked list Pin
nde_plume14-Dec-02 13:06
nde_plume14-Dec-02 13:06 
GeneralPutting a control bar inside a splitter window Pin
trimtrom14-Dec-02 11:19
trimtrom14-Dec-02 11:19 
Questionhow can i check it? Pin
imran_rafique14-Dec-02 10:22
imran_rafique14-Dec-02 10:22 
AnswerRe: how can i check it? Pin
l a u r e n14-Dec-02 11:28
l a u r e n14-Dec-02 11:28 
GeneralRe: how can i check it? Pin
Christian Graus14-Dec-02 11:48
protectorChristian Graus14-Dec-02 11:48 
GeneralRe: how can i check it? Pin
imran_rafique14-Dec-02 11:52
imran_rafique14-Dec-02 11:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.