Click to See Complete Forum and Search --> : Comport reading problems


May 23rd, 1999, 04:21 AM
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

June 10th, 1999, 06:49 AM
Hi,

I would suggest you use the debugger to find out exactly where the problem starts.

However I had similar problems also in a recent project of mine where I was communicating with an instrument through the serial port.
When somebody was switching on and/or off the device while my application was running the communication stopped, obviously due to garbage
coming in. Using the debugger I found that this can happen when there is a serial port related error e.g. error 4 -> Parity error. Obvously the garbage can
cause such errors. It seems that even if the error is cleared, the com port can not fully recover.

I solved the problem by completelly reseting the port when I detected an error. Perhaps you should do the same
(aborting your thread and stating again?).

I hope this helps. Best of luck.

KGr

anant anavkar
June 20th, 1999, 01:23 PM
Hi,
You can use ClearCommError() to chk the exact error.

You an set the DCB.fAbortOnError member. Then, a comm error will terminate any pending reads/writes and no new reads/writes are permitted until ClearCommError() is called.

Hope this helps.
regards,
anant