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
while(true)
{
SerialPort.WaitEvent();
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
LRESULT CVnTabs::ReadData(WPARAM wParam,LPARAM lParam)
{
char* timestamp = (char*)lParam;
byte* bytesRead = (byte*)wParam;
if(bytesRead[0] == 0x64)
{
pPosListCtrl->DeleteAllItems();
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!