[Top-posting undone for clarity.]
It would still be useful to know what you are intending
to accomplish on the receive side, and what happens
every 300 mS.
A more detailed set of observations would be useful.
What do you actually see? What did you expect?
That comment still applies. We see how you initialize
the comm port, but nothing reveals how you attempt
to send or receive data.
I am not very good in C++ problem and this is my codes below. I do not know
whether I got handshaking involve in my code. Could you guys let me
know?
The BuildCommDCB() docs should tell you what the
default setting is. You've done nothing to override it.
A few minor code comments are inserted below.
HANDLE OpenComm(char *lpszPort, int nBaud, char *nParity, int nData, int
nStop)
{
HANDLE hCom;
LPDCB lpDcb;
char szCom[10];
memset(szCom, 0, sizeof(szCom));
strcpy(szCom, "\\.\\COM");
strcat(szCom, lpszPort);
strcat(szCom, ":");
lpDcb = new(DCB);
You may as well just define an auto DCB variable as
dynamically allocate such an object. I note that you
leak the one created above.
//create port handle
hCom =
CreateFile(szCom,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_FLAG_O
VERLAPPED,NULL);
Overlapped I/O is a great way to go when using the
comm API, but it is tricky to use. This is why it is
especially important to see what else you do with
the port, other than initialize it.
//failed coz invalid handle provided
if (hCom == INVALID_HANDLE_VALUE)
{
hCom = NULL;
}
If the above fails, the other comm API calls should
not be made.
//fail to get port state
if (!GetCommState(hCom,lpDcb))
{
hCom = NULL;
}
If the above fails, it would be a good idea to
close the HANDLE, not just set it to 0.
//set setting [COM1: baud=9600 parity=N data=8 stop=1]
char strTemp[50];
memset(strTemp, 0, sizeof(strTemp));
sprintf(strTemp, "baud=%d parity=%s data=%d stop=%d", nBaud, nParity, nData, nStop);
if (!BuildCommDCB(strTemp,lpDcb))
{
hCom = NULL;
Ditto.
}
if (!SetCommState(hCom,lpDcb))
{
hCom = NULL;
Ditto.
}
// set communication timeouts
// get default values
BOOL bPort;
COMMTIMEOUTS CommTimeouts;
bPort = GetCommTimeouts(hCom, &CommTimeouts);
I do not see why you want to get the timeouts when
you are setting them all anyway.
// set new values
CommTimeouts.ReadIntervalTimeout = 15;
CommTimeouts.ReadTotalTimeoutConstant = 250;
CommTimeouts.ReadTotalTimeoutMultiplier = 1;
CommTimeouts.WriteTotalTimeoutConstant = 250;
CommTimeouts.WriteTotalTimeoutMultiplier = 1;
bPort = SetCommTimeouts(hCom, &CommTimeouts);
return hCom;
}
So, what does your read code look like?