Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi all,

If you have seen my previous questions, I am developing this mfc app which will read serial comm messages from a device. The device sends many messages some @ 10hz, some at 1hz.

The design of my app is simple, a thread to read the serial comm, a thread to send serial comm message to the device, as well as the mfc portion.

I am using a serial comm wrapper which can be found here:
CSerialPort v1.03 - Serial Port Wrapper[^]
Which I cannot change the read timeout.

The flow of receiving messages from the device goes like this:
1) message arrives at the serial port
2) port detects the message and fires off an event
3) thread activates and reads from buffer
4) thread processes the serial message and if found several messages will send several messages to mfc via windows messaging
5) mfc receives the message and will display on the list control

The problem I am currently having is that mfc does not "consistently" display on the list control. When I put break points and sort of pause the program, it is able find the message and display it. This prompted me to put Sleep statements inside the app. Which sometimes work well but most times fail.

I believe it is the processing of the information that is failing. But I am at wits end, so any ideas are welcome. Here are some snippets of the code.

heres the receiving thread
C++
while(true)
{
SerialPort.WaitEvent();
// somehow i must put this here to make it work only sometimes
Sleep(50);
CSerial::EEvent eEvent =  SerialPort.GetEventType();
int numberOfBlks = 0;
if(eEvent == CSerial::EEventRecv)
{
    DWORD dwBytesRead = 0;
    byte abBuffer[128];
    bool crcCheck = false;
    bool msgCheck = false;
			
    SerialPort.Read(abBuffer, 128, &dwBytesRead, 0, INFINITE);

    if(dwBytesRead > 0)
    {
	numberOfBlks = pvnsTab->stripMsgInternalCRC(abBuffer, sizeof(abBuffer), msgBlk);
    }
    
    for(int p=0; p<numberofblks ; ++p)    {
	MsgBlk msgBlkTemp = msgBlk[p];
	if(msgBlkTemp.fValid && msgBlkTemp.fValidCRC)
	{
	    char szTimestamp[128];
	    SYSTEMTIME st;
	    GetLocalTime(&st);
	    sprintf_s(szTimestamp, 128, "%02d:%02d:%02d:%03d", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);

	    byte bytetobesent[256];
	    memcpy(bytetobesent, msgBlkTemp.msg, sizeof(bytetobesent));
					
	    SendMessage(pvnsTab->GetSafeHwnd(), WM_MSG_RECV, (WPARAM)bytetobesent, (LPARAM)szTimestamp);
	}
    }
}
}


heres the receiving end
C++
LRESULT CVnTabs::ReadData(WPARAM wParam,LPARAM lParam)
{
    // need to cast wParam to bytesread & lparam to timestamp
    char* timestamp = (char*)lParam;
    byte* bytesRead = (byte*)wParam;

    // need to decode the message here and display
    if(bytesRead[0] == 0x64) 
    // putting break point here allow the bytesRead[0] == 100 to work
    {
	// clear the list ctrl first
	pPosListCtrl->DeleteAllItems();
        // do message processing here, one example :
        
        nRow = pPosListCtrl->InsertItem(0, CString("Alert"));
        unsigned short alert = 0;
	alert = (vnsalert << 8) + bytesRead[8];
	alert = (vnsalert << 8) + bytesRead[9];
        CString szAlert;
        szAlert.Format(_T("%d"), alert);
        pPosListCtrl->SetItemText(nRow, 1, szAlert);
    }
}


Many thanks in advance!
Posted
Updated 21-Nov-11 15:34pm
v2

1 solution

You should put the UI code into a separate thread so the actual updating of the List View is not happening in the same thread as the data receiver.
 
Share this answer
 
Comments
Zsefv2 22-Nov-11 10:50am    
you mean the ReadData is not on the main thread?
Richard MacCutchan 22-Nov-11 12:20pm    
I don't know which thread it is in, but you should keep your windows code in the main thread and your data manipulation in background. That way the window updates are more likely to keep up with the changes.
Zsefv2 22-Nov-11 20:41pm    
ok, i understand, so i should code in such a way that processing of the message is done in the thread while the main thread(mfc) should just receive the message and display only?
Richard MacCutchan 23-Nov-11 3:01am    
Essentially, yes.
Zsefv2 23-Nov-11 3:25am    
Ok I put the message processing in the thread and it works better now. I still have to put that silly sleep statement though... by the way, there are several messages. 1 of them comes in at 1Hz while the other at 4Hz i tried sleep(50) only the one sending at 4Hz shows up, i put sleep(500) it works ok, but am missing some messages for the 4Hz, which is not much of a problem but I am curious is there a optimum sleep time to put in my case?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900