Dear fellow coders,
I am facing this problem with my program. The program is interfacing with another machine which is outputting serial data at 1Hz. My program is MFC based (a cpropertypage) and to handle the data, i have defined a thread (pRecvDataThread) to handle the reading of data from the serial port's buffer, and display it on my list control in the main thread.
I have separated GUI stuff like getting list control handles etc out of the thread as suggested in some tutorial. However my GUI still is hardly responsive. Heres the section of the code, does anyone know whats wrong?
LRESULT MyApp::OnDataArrived(WPARAM wParam,LPARAM lParam)
{
Sleep(40);
const CSerialMFC::EEvent eEvent = CSerialMFC::EEvent(LOWORD(wParam));
const CSerialMFC::EEvent eError = CSerialMFC::EEvent(HIWORD(wParam));
switch(eEvent)
{
case CSerialMFC::EEventRecv:
pRecvDataThread = AfxBeginThread(MyRecvThread, LPVOID(this), THREAD_PRIORITY_NORMAL);
break;
}
return 0;
}
UINT MyRecvThread(LPVOID pParam)
{
MyApp *pTab = (MyApp*)pParam;
WaitForSingleObject(pTab ->MutexRecv, INFINITE);
DWORD dwBytesRead = 0;
byte abBuffer[512];
int errorInt = pTab->serialMFC.Read(abBuffer, sizeof(abBuffer), &dwBytesRead, 0, INFINITE);
if(errorInt != ERROR_SUCCESS)
{
AfxMessageBox(_T("ERROR in reading serial port"));
return 0;
}
char szTimestamp[128];
SYSTEMTIME st;
GetLocalTime(&st);
sprintf_s(szTimestamp, 128, "%02d:%02d:%02d:%03d", st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
pTab->ReadData(abBuffer, szTimestamp, numBytesRead);
ReleaseMutex(MyApp->MutexRecv);
return 0;
}
void MyApp::ReadData(byte bytesRead [], char* timestamp, int bytesread)
{
}