extern "C" short APIENTRY
DowntimeT(BYTE nPort, BYTE macno)
{
unsigned long nBYTEs;
BYTE Output[128];
BYTE Input[128];
DWORD dwErrors = 0;
DWORD dwBYTEsWritten;
DWORD dwBYTEsRead;
BOOL bReturn;
DWORD dwErrorFlags;
COMSTAT ComStat;
int i;
if(HandShake(nPort,macno,1)!=0) return -1;
CloseCommDev(nPort);
InitCommDev(nPort,'n');
Output[0] = 0x2A;
Output[1] = 0x01;
Output[2] = 0x08;//时间段数量为8;
Output[3] = (Output[0]+Output[1]+Output[2])/256;//校验码
Output[4] = (Output[0]+Output[1]+Output[2])%256;//校验码
nBYTEs=5;
m_OverlappedWrite.Offset=0;
m_OverlappedWrite.OffsetHigh=0;
ClearCommError(hCommDev,&dwErrors,NULL);
PurgeComm(hCommDev, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
bReturn = WriteFile(hCommDev,Output,nBYTEs,&dwBYTEsWritten,&m_OverlappedWrite);
if (!bReturn) {
ClearCommError(hCommDev,&dwErrors,NULL);
Sleep(200);
CloseCommDev(nPort);
return -1;
}
if (nBYTEs!=dwBYTEsWritten) {CloseCommDev(nPort);
return -1;}
Sleep(200);
for (i=0;i<999999;i++)
{
ClearCommError(hCommDev,&dwErrorFlags,&ComStat);
if(ComStat.cbInQue>0)
{
dwBYTEsRead=min(2,ComStat.cbInQue);
if(dwBYTEsRead>0)
bReturn = ReadFile(hCommDev,Input,1,&dwBYTEsRead,NULL);
if (!bReturn) {
ClearCommError(hCommDev,&dwErrors,NULL);
Sleep(200);
CloseCommDev(nPort);
return -1;
}
if( Input[0] != macno){
Sleep(200);
CloseCommDev(nPort);
return -1;
}
else
{
EndComm(0);
return 0;
}
}
}
return -1;
}