Ronco
2010-05-05 15:08:01 UTC
In our applcation, runiing on WIN CE 5.0 version, we have the following
problem:
Sometime after a call at ReadFile() function, it not return from it's wait
status for
new received data.
Pratically our system stop to receive data from connected host.
To out from this condition it is necessary to restart operating system.
It is not possible only restart our application program.
Our application is based on PC104 SECO board (model M543R-CBBBL)
Follow our relating code.
void StartHostCommLoopAv()
{
hHostAv = CreateFile(sHostPortAv, GENERIC_READ + GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (hHostAv == INVALID_HANDLE_VALUE)
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("StartHostCommLoopAv Error %d\n\r",GetLastError());
#endif
}
else
{
DCB dcb;
if (GetCommState(hHostAv, &dcb))
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("GetCommState +\n\r");
#endif
dcb.BaudRate = Pulsar.ComQuattro.BaudRate;
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE; // era DTR_CONTROL_ENABLE mod. il
19/04/2010
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE; // era RTS_CONTROL_ENABLE mod. il
19/04/2010
dcb.fAbortOnError = FALSE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hHostAv, &dcb))
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("SetCommState Error %d\n\r",GetLastError());
#endif
}
}
else
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("GetCommState Error %d\n\r",GetLastError());
#endif
}
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(hHostAv, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = 1000; //MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 1000;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(hHostAv, &CommTimeouts);
DWORD dwHostThreadID;
hHostThrdAv = CreateThread(NULL, 0, HostCommAvThreadProc, NULL, 0,
&dwHostThreadID);
}
}
DWORD HostCommAvThreadProc(LPVOID lpParameter)
{
BYTE rx_status = 0;
BYTE ch_buff[COLUMN_FROM_HOST_BUFFER];
bool bLoop = true;
bool bDle = false;
DWORD nr = 0;
int giaLetti = 0;
int giaScritti = 0;
UINT toWrite;
char ch;
WORD per_me;
UINT i, j;
CaricaProgrammazioneTessereAv();
pConnDedicataAv->CleanBuffers();
ZeroMemory(ch_buff, sizeof(ch_buff));
pConnDedicataAv->vars_init(TRUE, &rx_status);
rx_status = RX_ATTESA;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("HostCommAvThreadProc +\n\r");
#endif
do
{
ReadFile(hHostAv, &ch, 1, &nr, NULL);
if(nr == 1)
{
switch(ch)
{
case SOH_AV:
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("SOH_AV INIZIO RX\n\r",per_me);
#endif
rx_status = 0;
ch_buff[0] = ch;
giaLetti = 1;
continue;
case ETX_AV:
ch_buff[giaLetti++] = ch;
per_me = ch_buff[1];
if(Pulsar.ComQuattro.IndirizzoAv == per_me)
rx_status = RX_OK;
else
rx_status = INDIRIZZO_ERRATO;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("ETX_AV FINE RX\n\r",per_me);
#endif
pConnDedicataAv->GESTIONE_COLLOQUIO_CON_HOST_AV(ch_buff,
&rx_status,giaLetti);
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("ETX_AV FINE RX per_me = %d\n\r",per_me);
#endif
break;
case DLE_AV:
bDle = true;
continue;
default:
if (bDle)
{
bDle = false;
ch &= ~DLE_MK_AV;
}
ch_buff[giaLetti++] = ch;
continue;
}
}
if (pConnDedicataAv->TX_SEM && giaLetti)
{
giaScritti = 0;
toWrite = pConnDedicataAv->LenghAv+1;
ch_buff[0] = pConnDedicataAv->tx_buff[0];
for (i = 1, j = 1; i < toWrite - 1; i++)
{
switch (pConnDedicataAv->tx_buff[i])
{
case SOH_AV:
case ETX_AV:
case DLE_AV:
ch_buff[j++] = DLE_AV;
ch_buff[j++] = pConnDedicataAv->tx_buff[i] | DLE_MK_AV;
break;
default:
ch_buff[j++] = pConnDedicataAv->tx_buff[i];
break;
}
}
ch_buff[j++] = pConnDedicataAv->tx_buff[i];
toWrite = j;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("INIZIO TX\n\r");
#endif
do
{
WriteFile(hHostAv, (char *)&ch_buff[giaScritti], 1,&nr, NULL);
giaScritti += nr;
toWrite -= nr;
} while (toWrite);
pConnDedicataAv->ContRestart = Pulsar.ComQuattro.TimerAvAttiva;
bDle = false;
nr = 0;
giaLetti = 0;
rx_status = RX_ATTESA;
pConnDedicataAv->TX_SEM = FALSE;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("FINE TX\n\r");
#endif
}
ChiudiRiapriSeriale();
Sleep(10);
} while (bLoop);
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("HostCommAvThreadProc -\n\r");
#endif
return 0;
}
problem:
Sometime after a call at ReadFile() function, it not return from it's wait
status for
new received data.
Pratically our system stop to receive data from connected host.
To out from this condition it is necessary to restart operating system.
It is not possible only restart our application program.
Our application is based on PC104 SECO board (model M543R-CBBBL)
Follow our relating code.
void StartHostCommLoopAv()
{
hHostAv = CreateFile(sHostPortAv, GENERIC_READ + GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, 0, NULL);
if (hHostAv == INVALID_HANDLE_VALUE)
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("StartHostCommLoopAv Error %d\n\r",GetLastError());
#endif
}
else
{
DCB dcb;
if (GetCommState(hHostAv, &dcb))
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("GetCommState +\n\r");
#endif
dcb.BaudRate = Pulsar.ComQuattro.BaudRate;
dcb.fBinary = TRUE;
dcb.fParity = FALSE;
dcb.fOutxCtsFlow = FALSE;
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE; // era DTR_CONTROL_ENABLE mod. il
19/04/2010
dcb.fDsrSensitivity = FALSE;
dcb.fTXContinueOnXoff = TRUE;
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
dcb.fErrorChar = FALSE;
dcb.fNull = FALSE;
dcb.fRtsControl = RTS_CONTROL_DISABLE; // era RTS_CONTROL_ENABLE mod. il
19/04/2010
dcb.fAbortOnError = FALSE;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SetCommState(hHostAv, &dcb))
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("SetCommState Error %d\n\r",GetLastError());
#endif
}
}
else
{
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("GetCommState Error %d\n\r",GetLastError());
#endif
}
COMMTIMEOUTS CommTimeouts;
GetCommTimeouts(hHostAv, &CommTimeouts);
CommTimeouts.ReadIntervalTimeout = 1000; //MAXDWORD;
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 1000;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
SetCommTimeouts(hHostAv, &CommTimeouts);
DWORD dwHostThreadID;
hHostThrdAv = CreateThread(NULL, 0, HostCommAvThreadProc, NULL, 0,
&dwHostThreadID);
}
}
DWORD HostCommAvThreadProc(LPVOID lpParameter)
{
BYTE rx_status = 0;
BYTE ch_buff[COLUMN_FROM_HOST_BUFFER];
bool bLoop = true;
bool bDle = false;
DWORD nr = 0;
int giaLetti = 0;
int giaScritti = 0;
UINT toWrite;
char ch;
WORD per_me;
UINT i, j;
CaricaProgrammazioneTessereAv();
pConnDedicataAv->CleanBuffers();
ZeroMemory(ch_buff, sizeof(ch_buff));
pConnDedicataAv->vars_init(TRUE, &rx_status);
rx_status = RX_ATTESA;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("HostCommAvThreadProc +\n\r");
#endif
do
{
ReadFile(hHostAv, &ch, 1, &nr, NULL);
if(nr == 1)
{
switch(ch)
{
case SOH_AV:
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("SOH_AV INIZIO RX\n\r",per_me);
#endif
rx_status = 0;
ch_buff[0] = ch;
giaLetti = 1;
continue;
case ETX_AV:
ch_buff[giaLetti++] = ch;
per_me = ch_buff[1];
if(Pulsar.ComQuattro.IndirizzoAv == per_me)
rx_status = RX_OK;
else
rx_status = INDIRIZZO_ERRATO;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("ETX_AV FINE RX\n\r",per_me);
#endif
pConnDedicataAv->GESTIONE_COLLOQUIO_CON_HOST_AV(ch_buff,
&rx_status,giaLetti);
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("ETX_AV FINE RX per_me = %d\n\r",per_me);
#endif
break;
case DLE_AV:
bDle = true;
continue;
default:
if (bDle)
{
bDle = false;
ch &= ~DLE_MK_AV;
}
ch_buff[giaLetti++] = ch;
continue;
}
}
if (pConnDedicataAv->TX_SEM && giaLetti)
{
giaScritti = 0;
toWrite = pConnDedicataAv->LenghAv+1;
ch_buff[0] = pConnDedicataAv->tx_buff[0];
for (i = 1, j = 1; i < toWrite - 1; i++)
{
switch (pConnDedicataAv->tx_buff[i])
{
case SOH_AV:
case ETX_AV:
case DLE_AV:
ch_buff[j++] = DLE_AV;
ch_buff[j++] = pConnDedicataAv->tx_buff[i] | DLE_MK_AV;
break;
default:
ch_buff[j++] = pConnDedicataAv->tx_buff[i];
break;
}
}
ch_buff[j++] = pConnDedicataAv->tx_buff[i];
toWrite = j;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("INIZIO TX\n\r");
#endif
do
{
WriteFile(hHostAv, (char *)&ch_buff[giaScritti], 1,&nr, NULL);
giaScritti += nr;
toWrite -= nr;
} while (toWrite);
pConnDedicataAv->ContRestart = Pulsar.ComQuattro.TimerAvAttiva;
bDle = false;
nr = 0;
giaLetti = 0;
rx_status = RX_ATTESA;
pConnDedicataAv->TX_SEM = FALSE;
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("FINE TX\n\r");
#endif
}
ChiudiRiapriSeriale();
Sleep(10);
} while (bLoop);
#ifdef DEBUG_PORT_COM4
pDbg->DebugPrint("HostCommAvThreadProc -\n\r");
#endif
return 0;
}