📄 com232.cpp
字号:
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
break;
}
}
}
else
{
// some other error occurred
dwLength = 0 ;
ClearCommError( comDev.m_hCom , &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
}
}
}
return ( dwLength ) ;
} // end of ReadCommBlock()
//-------------------------------------------------------
//
//ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength,DWORD dwTimeOut)
//
//---------------------------------------------------------
//---------------------------------------------------------------------------
int ReadCommBlockEx(CComStatus& comDev,LPSTR lpszBlock, int nMaxLength,DWORD dwTimeOut)
{
LPSTR lpOffset=lpszBlock;
int nReadCount = 0;
char chBuf;
//time_t beginTime,endTime;
if(!comDev.m_hCom)
return 0;
if(dwTimeOut <= 0)
return 0;
MSG msg;
//time(&beginTime);
DWORD dwLastTick,dwNowTick,dwGoneTime;
dwGoneTime = 0;
dwLastTick = GetTickCount();
dwNowTick = dwLastTick;
// double diftime;
do
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
if(ReadCommBlock(comDev,&chBuf,1) > 0)
{
//TRACE("----get a char----\n");
*lpOffset = chBuf;
lpOffset ++;
nReadCount ++;
}
/*time(&endTime);
diftime = difftime(endTime,beginTime);*/
dwNowTick = GetTickCount();
if(dwNowTick < dwLastTick)
{
dwLastTick = dwNowTick;
}
dwGoneTime = dwNowTick - dwLastTick;
//TRACE("gon time = %lu\n",dwGoneTime);
}while((nReadCount < nMaxLength) && (dwGoneTime < dwTimeOut));//((diftime * 1000.0) < dwTimeOut));
return (nReadCount);
}//end ReadCommBlockEx
// BOOL NEAR WriteCommBlock( HWND hWnd, BYTE *pByte )
//
// Description:
// Writes a block of data to the COM port specified in the associated
// TTY info structure.
//
// Parameters:
// HWND hWnd
// handle to TTY window
//
// BYTE *pByte
// pointer to data to write to port
//
// Win-32 Porting Issues:
// - WriteComm() has been replaced by WriteFile() in Win-32.
// - Overlapped I/O has been implemented.
//
//---------------------------------------------------------------------------
BOOL WriteCommBlock( CComStatus& comDev, LPSTR lpByte , DWORD dwBytesToWrite)
{
BOOL fWriteStat ;
DWORD dwBytesWritten ;
// NPTTYINFO npTTYInfo ;
DWORD dwErrorFlags;
DWORD dwError;
DWORD dwBytesSent=0;
COMSTAT ComStat;
char szError[ 128 ] ;
/*
if (NULL == (npTTYInfo = GETNPTTYINFO( hWnd )))
return ( FALSE ) ;
*/
fWriteStat = WriteFile( comDev.m_hCom , lpByte, dwBytesToWrite,
&dwBytesWritten, &( comDev.m_wtos) ) ;
// Note that normally the code will not execute the following
// because the driver caches write operations. Small I/O requests
// (up to several thousand bytes) will normally be accepted
// immediately and WriteFile will return true even though an
// overlapped operation was specified
if (!fWriteStat)
{
if(GetLastError() == ERROR_IO_PENDING)
{
// We should wait for the completion of the write operation
// so we know if it worked or not
// This is only one way to do this. It might be beneficial to
// place the write operation in a separate thread
// so that blocking on completion will not negatively
// affect the responsiveness of the UI
// If the write takes too long to complete, this
// function will timeout according to the
// CommTimeOuts.WriteTotalTimeoutMultiplier variable.
// This code logs the timeout but does not retry
// the write.
while(!GetOverlappedResult( comDev.m_hCom,
&(comDev.m_wtos), &dwBytesWritten, TRUE ))
{
dwError = GetLastError();
if(dwError == ERROR_IO_INCOMPLETE)
{
// normal result if not finished
dwBytesSent += dwBytesWritten;
continue;
}
else
{
// an error occurred, try to recover
wsprintf( szError, "<CE-%u>", dwError ) ;
//WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
break;
}
}
dwBytesSent += dwBytesWritten;
if( dwBytesSent != dwBytesToWrite )
wsprintf(szError,"\nProbable Write Timeout: Total of %ld bytes sent", dwBytesSent);
else
wsprintf(szError,"\n%ld bytes written", dwBytesSent);
OutputDebugString(szError);
}
else
{
// some other error occurred
ClearCommError( comDev.m_hCom, &dwErrorFlags, &ComStat ) ;
/*if ((dwErrorFlags > 0) && DISPLAYERRORS( npTTYInfo ))
{
wsprintf( szError, "<CE-%u>", dwErrorFlags ) ;
WriteTTYBlock( hWnd, szError, lstrlen( szError ) ) ;
}*/
return ( FALSE );
}
}
return ( TRUE ) ;
} // end of WriteCommBlock()
/*
extern int fileflash[100];
BOOL FAR InitComDev(HWND hWnd)
{
DCB dcb;
int x;
if(XwCom&0x10)
CommStatus.idComDev=OpenComm("COM1\0",1024,1128);
if(XwCom&0x20)
CommStatus.idComDev=OpenComm("COM2\0",1024,1128);
if(XwCom&0x40)
CommStatus.idComDev=OpenComm("COM3\0",1024,1128);
if(XwCom&0x80)
CommStatus.idComDev=OpenComm("COM4\0",1024,1128);
//if ((CommStatus.idComDev=OpenComm(InitCommParam.bPort,1024,1128))<0)
if (CommStatus.idComDev<0)
{
MessageBox (hWnd,"打开串行口错误!","错误",MB_ICONSTOP);
return (FALSE);
}
GetCommState(CommStatus.idComDev,&dcb);
dcb.BaudRate=InitCommParam.wBaudRate;
dcb.ByteSize=InitCommParam.bByteSize;
dcb.Parity=InitCommParam.bParity;
dcb.StopBits=InitCommParam.bStopBits;
dcb.fChEvt=InitCommParam.fChEvt;
dcb.EvtChar=InitCommParam.bEvtChar;
dcb.fBinary=InitCommParam.fBinary;
x=SetCommState(&dcb);
if (CommStatus.fConnected=!(x<0))
{
SetCommEventMask(CommStatus.idComDev,EV_RXFLAG);
EnableCommNotification(CommStatus.idComDev,hWnd,58,4) ;
//MAXBLOCKLENGTH,5);
ClearCommBreak(CommStatus.idComDev);
}
else
{
MessageBox (hWnd,"串行口设置错误!","错误",MB_ICONSTOP);
CloseComm(CommStatus.idComDev);
}
return(CommStatus.fConnected);
}
BOOL FAR ProcessCOMMNotification(HWND hWnd,LPARAM lParam)
{
int nError=0,x;
unsigned int nLength=0;
BYTE ch0;
COMSTAT ComStat;
x=0;
while (nError=GetCommError(CommStatus.idComDev,&ComStat));
if (ComStat.cbInQue<1)
return TRUE;
if((CN_EVENT&LOWORD(lParam))||(CN_RECEIVE&LOWORD(lParam)));
{
fileflash[11]++;
GetCommEventMask(CommStatus.idComDev,EV_RXFLAG);
if(opation==1)
{ nLength=ReadComm(CommStatus.idComDev,(LPSTR)&sCom2[0],MAXBLOCKLENGTH);
opation++;
}
else if(opation==3)
{ nLength=ReadComm(CommStatus.idComDev,(LPSTR)&sCom3[0],MAXBLOCKLENGTH);
opation=0;
}
else if(opation<0||opation>=4)
opation=0;
return(TRUE);
}
}
void SendData(BYTE option)
{
BYTE sCom1[5];
if(option==0)
strcpy(sCom1,"1100\xd");
else if(option==2)
strcpy(sCom1,"1200\xd");
else
return;
FlushComm(CommStatus.idComDev,0);
FlushComm(CommStatus.idComDev,1);
WriteComm(CommStatus.idComDev,(LPSTR)sCom1,strlen(sCom1));
}
BOOL FAR CloseConnection(HWND hWnd)
{
EnableCommNotification (CommStatus.idComDev,NULL,-1,-1);
CloseComm(CommStatus.idComDev);
CommStatus.fConnected=FALSE;
return (TRUE);
}
void tron(float *buf)
{
char b[5];
int i,j,k;
long ll;
float *netdata;
netdata=(float *)GlobalLock(downstationdata[6][3].rdata);
b[4]=0;
for(k=0;k<100;k++)
if(sCom2[k]=='A')break;
if(k==100)
{
GlobalUnlock(downstationdata[6][3].rdata);
return;
}
for(i=0;i<13;i++)
{
if(i==0||i==1)
continue;
for(j=0;j<4;j++)
b[j]=sCom2[i*4+j+1+k];
ll=0;
if(!strcmp("5555",b))
ll=0;
else
sscanf(b,"%x",&ll);
*(netdata+i-2)=(float)ll;
}
for(k=0;k<100;k++)
if(sCom3[k]=='B')break;
if(k==100)
{
GlobalUnlock(downstationdata[6][3].rdata);
return;
}
for(i=0;i<14;i++)
{
if(i==0||i==1)
continue;
for(j=0;j<4;j++)
b[j]=sCom3[i*4+j+1+k];
sscanf(b,"%f",netdata+i+11-2);
*(netdata+i+11-2)/=10;
}
GlobalUnlock(downstationdata[6][3].rdata);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -