Hi all,
I'm having a lot of problems with receiving data from the comport. I'am receiving data serial data from a ekstern device, that from time to times sends garbage data to the PC. By garbage data i mean absolutely random signals, where the signal timing (signalshifts, stopbit and so on) don't live up to the serial standarts.
In my MFC application i have created a thread, that continouesly read data from the com port and places them i a buffer. The problem is that sometimes the data reception hangs, and i cant receive data again before I reinitialize the comport.The hangs only acours when the garbage data is transmittet from the ekstern device. When the device transmits real seriel data the reception works fine. I need to log data over a relative long time, so the data reception must not stop when these garbage signals arrives at the com port.
I have include the source kode for the Receive thread, where it can be seen how the DCB struct are setup.
UINT CServiceinstrumentTerminalDlg::ReceiveThread(LPVOID pParam)
{
bool StopStatus = false;
int indeks = 0;
char ReceiveBuffer[100];
int DataReceived;
HANDLE g_hCom;
COMMTIMEOUTS commtimeouts;
DCB dcb;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
dcb.ByteSize = 8;
dcb.wReserved = 0;
dcb.fAbortOnError = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE;
dcb.fNull = FALSE;
dcb.fErrorChar = FALSE;
dcb.fInX = FALSE;
dcb.fOutX = FALSE;
dcb.fTXContinueOnXoff = TRUE;
dcb.fDsrSensitivity = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fOutxDsrFlow = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fParity = FALSE;
dcb.fBinary= TRUE;
dcb.BaudRate = CBR_9600;
dcb.DCBlength = sizeof(dcb);
commtimeouts.ReadIntervalTimeout=500;
commtimeouts.ReadTotalTimeoutMultiplier=MAXDWORD;
commtimeouts.ReadTotalTimeoutConstant=1000;
commtimeouts.WriteTotalTimeoutMultiplier=0;
commtimeouts.WriteTotalTimeoutConstant=0;
g_hCom = CreateFile((char*)pParam, GENERIC_READ | GENERIC_WRITE, 0,NULL,OPEN_EXISTING,0,NULL);
SetCommState(g_hCom,&dcb);
SetCommTimeouts(g_hCom,&commtimeouts);
do {
ReadFile(g_hCom,ReceiveBuffer,10,(unsigned long*)&DataReceived,0);
EnterCriticalSection(&DataArrayLock);
for(char n=0; n<DataReceived; n++)
DataArray.Add(ReceiveBuffer[n]);
LeaveCriticalSection(&DataArrayLock);
EnterCriticalSection(&StopReceiveLock);
{
StopStatus = StopReceive;
}
LeaveCriticalSection(&StopReceiveLock);
} while(!StopStatus);
CloseHandle(g_hCom);
return 0;
}
If you know why the recection stops, and how to solve the problem please reply.
Thanks
Dahl
