my

// common.h: interface for the Ccommon class.
//
//////////////////////////////////////////////////////////////////////#if !defined(AFX_COMMON_H__F4B52E19_22ED_4F41_B2A5_038B4B3BFE47__INCLUDED_)
#define AFX_COMMON_H__F4B52E19_22ED_4F41_B2A5_038B4B3BFE47__INCLUDED_#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000#include "afxmt.h"
//#include "include\UpLink232Def.h"
class Ccommon  
{
public:
Ccommon();
virtual ~Ccommon();private:
BOOL isOpened;
int iInBufferSize;
int iOutBufferSize;
COMMCONFIG ComCfg;
    COMMTIMEOUTS TimeOuts;   
DWORD TimeOut;public:
HANDLE hComDevice;public:
BOOL rs232_open(int iComPort,int iBaudRate=CBR_115200); 
BOOL rs232_close();
    BOOL rs232_write(BYTE* pbWrDataBuf,DWORD WrLen,DWORD* pActWrBytes);
BOOL rs232_read(BYTE* pbRdDataBuf,DWORD RdLen,DWORD* pActRdBytes);
// BOOL rs232_read_header(BYTE* pbRdDataBuf,DWORD RdLen,DWORD* pActRdBytes);////added 720
};
  #endif // !defined(AFX_COMMON_H__F4B52E19_22ED_4F41_B2A5_038B4B3BFE47__INCLUDED_)
// common.cpp: implementation of the Ccommon class.
//
//////////////////////////////////////////////////////////////////////#include "stdafx.h"
#include "common.h"#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
     
Ccommon::Ccommon()
{
    isOpened=FALSE;
iInBufferSize=8*1024; //8k
iOutBufferSize=8*1024; //8k
//TimeOut=1000;// 7 20 delete
    TimeOut=500;//7 20 added
TimeOuts.ReadIntervalTimeout =0x20000l; 
    TimeOuts.ReadTotalTimeoutMultiplier =20; 
    TimeOuts.ReadTotalTimeoutConstant =20; 
    TimeOuts.WriteTotalTimeoutMultiplier =20; 
    TimeOuts.WriteTotalTimeoutConstant =20;
}Ccommon::~Ccommon()
{
}
BOOL Ccommon::rs232_open(int iComPort,int iBaudRate)
{
char strCOM[MAX_PATH]; if(isOpened)
{
   TRACE("has isOpened.\n");
   return FALSE;
}
sprintf(strCOM,"COM%d",iComPort);
hComDevice=::CreateFile(strCOM,(GENERIC_READ|GENERIC_WRITE),
0,NULL,OPEN_EXISTING,
 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN ,NULL); if(hComDevice == INVALID_HANDLE_VALUE){
if(::GetLastError() != ERROR_SUCCESS){
::SetLastError(0l);
TRACE("COM%d CreateFile ERROR.\n",iComPort);
return FALSE;
}
}
if(!::SetupComm(hComDevice,iInBufferSize,iOutBufferSize)){
::SetLastError(0l);
::CloseHandle(hComDevice);
TRACE("COM%d  SetupComm ERROR.\n",iComPort);
return FALSE;
} ::GetCommState(hComDevice, &ComCfg.dcb); ComCfg.dcb.BaudRate = iBaudRate;
ComCfg.dcb.ByteSize = 8;
ComCfg.dcb.Parity = NOPARITY;
ComCfg.dcb.StopBits = ONESTOPBIT;
ComCfg.dcb.fDtrControl = DTR_CONTROL_DISABLE ;
ComCfg.dcb.fRtsControl = RTS_CONTROL_DISABLE ;
ComCfg.dcb.fInX = ComCfg.dcb.fOutX = 0 ;
ComCfg.dcb.XonChar = 0x11 ;
ComCfg.dcb.XoffChar = 0x13 ;
ComCfg.dcb.fBinary = 1;
// ComCfg.dcb.fAbortOnError = 0; if(!::SetCommState(hComDevice,&ComCfg.dcb)){
::SetLastError(0l);
::CloseHandle(hComDevice);
TRACE("COM%d  SetCommState ERROR.\n",iComPort);
return FALSE;
}
if(!::SetCommMask(hComDevice,(/*EV_BREAK|EV_CTS|EV_DSR|EV_ERR|EV_RING|EV_RLSD|*/EV_RXCHAR/*|EV_RXFLAG|EV_TXEMPTY*/))){
::SetLastError(0l);
::CloseHandle(hComDevice);
TRACE("COM%d SetCommMask ERROR.\n",iComPort);
return FALSE;
} if(!::SetCommTimeouts(hComDevice,&TimeOuts)){
::SetLastError(0l);
::CloseHandle(hComDevice);
TRACE("COM%d SetCommTimeouts ERROR.\n",iComPort);
return FALSE;
} if(!::PurgeComm(hComDevice,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR)){
::SetLastError(0l);
::CloseHandle(hComDevice);
TRACE("COM%d  PurgeComm ERROR.\n",iComPort);
return FALSE;
}  isOpened=TRUE;
return TRUE;
}
BOOL Ccommon::rs232_close()
{
BOOL bRet; if(FALSE==isOpened) return FALSE;  
bRet=::CloseHandle(hComDevice);
if(::GetLastError() != ERROR_SUCCESS){
::SetLastError(0l);
TRACE("CloseHandle ERROR.\n");
}
isOpened=FALSE; 
return bRet;
}
BOOL Ccommon::rs232_write(BYTE* pbWrDataBuf,DWORD WrLen,DWORD* pActWrBytes)
{
BOOL bRet;
// DWORD drslt;
*pActWrBytes = 0l; if(!isOpened)
return FALSE;
bRet=::WriteFile(hComDevice,pbWrDataBuf,WrLen,pActWrBytes,NULL);
FlushFileBuffers(hComDevice);
return bRet;}
BOOL Ccommon::rs232_read(BYTE* pbRdDataBuf,DWORD RdLen,DWORD* pActRdBytes)
{
int ii,jj;
BOOL bRet;
DWORD   readed; *pActRdBytes =0l; if(!isOpened) return FALSE;
for(ii=0; ii < (( int ) TimeOut / 100); ii++)
{
bRet=::ReadFile(hComDevice, ((char*)pbRdDataBuf) + *pActRdBytes,
RdLen - *pActRdBytes, &readed,NULL);
(*pActRdBytes) += readed; if(!bRet)
{
if(::GetLastError() != ERROR_IO_PENDING && ::GetLastError() != ERROR_IO_INCOMPLETE)
{
SetLastError(0l);
return FALSE;
} }
else if(*pActRdBytes == RdLen) 
{
bRet =TRUE;
break;
}
else
bRet =FALSE; }
return bRet;
}
/*  add 7 20
BOOL Ccommon::rs232_read_header(BYTE* pbRdDataBuf,DWORD RdLen,DWORD* pActRdBytes)
{
int ii;
BOOL bRet;
DWORD   readed;
    char  ReadChar;
*pActRdBytes =0l;
    int  ValidNum=0; if(!isOpened) return FALSE;
for(ii=0; ii <(500<RdLen?RdLen:500);ii++)
{
bRet=::ReadFile(hComDevice,&ReadChar,
1, &readed,NULL);
pbRdDataBuf[ValidNum]=ReadChar;
if(!bRet)
{return FALSE;
if(::GetLastError() != ERROR_IO_PENDING && ::GetLastError() != ERROR_IO_INCOMPLETE)
{
SetLastError(0l);
return FALSE;
} }
if(ValidNum<strlen(LINK_SYNC)){
if(ReadChar!=LINK_SYNC[ValidNum]){
ValidNum=0;
continue;
}
}
ValidNum++;
*pActRdBytes=ValidNum;
if(ValidNum==RdLen){
return TRUE;
}
}
return bRet;
}*/add 7 20