123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- // CommRS232.cpp: implementation of the CCommRS232 class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "CommRS232.h"
- #include "icpdas.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CCommRS232::CCommRS232() : CCommAsyn()
- {
- m_hCom = INVALID_HANDLE_VALUE;
- m_bOpen=FALSE;
- }
- CCommRS232::~CCommRS232()
- {
- if( m_hCom != INVALID_HANDLE_VALUE )
- {
- CloseHandle( m_hCom );
- m_hCom = INVALID_HANDLE_VALUE;
- }
- // sio_close(m_nPort);
- }
- BOOL CCommRS232::InitParam(PPORTPARAM pPortParam)
- {
- ASSERT(pPortParam!=NULL);
- if(pPortParam==NULL)
- {
- return FALSE;
- }
- if( !CCommAsyn::InitParam(pPortParam) )
- {
- return FALSE;
- }
- if( !InitComm(pPortParam) )
- {
- return FALSE;
- }
- return TRUE;
- }
- BOOL CCommRS232::InitComm(PPORTPARAM pPortParam)
- {
- m_bOpen=FALSE;
- ASSERT(pPortParam!=NULL);
- if(pPortParam==NULL)
- return FALSE;
- COMMTIMEOUTS timeout;
- DCB dcb;
- BOOL result;
- int temp=0;
- char tmp[20]="\\\\.\\com";
- char buffer[20];
- m_nPort=pPortParam->PortNo;
- _itoa(m_nPort,buffer,10);
- strcat(tmp,buffer);
- m_hCom = CreateFile(tmp, GENERIC_READ | GENERIC_WRITE,
- 0, NULL, OPEN_EXISTING, NULL , NULL);
- if(m_hCom==INVALID_HANDLE_VALUE)
- {
- //AfxMessageBox( "¶Ë¿Ú²»´æÔÚ»òÒÑ»µ£¡" , MB_OK , 0 );
- // ErrorProcess();
- return FALSE;
- }
- result=SetupComm(m_hCom,1024,1024);
- ASSERT(result);
- if(!result)
- return FALSE;
- result=PurgeComm(m_hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
- ASSERT(result);
- if(!result)
- return FALSE;
- result=GetCommState(m_hCom, &dcb);
- ASSERT(result);
- if(!result)
- return FALSE;
- dcb.Parity=pPortParam->Parity;
- if(dcb.Parity==NOPARITY)
- dcb.fParity=FALSE;
- else
- dcb.fParity=TRUE;
- dcb.BaudRate=pPortParam->BaudRate;
- dcb.ByteSize=pPortParam->ByteSize;
- dcb.StopBits=pPortParam->StopBits;
- if(dcb.ByteSize==8)
- dcb.StopBits=ONESTOPBIT;
- result=SetCommState(m_hCom, &dcb);
- ASSERT(result);
- if(!result)
- return FALSE;
- result=GetCommTimeouts(m_hCom, &timeout);
- ASSERT(result);
- if(!result)
- return FALSE;
- timeout.ReadIntervalTimeout=MAXDWORD;
- timeout.ReadTotalTimeoutMultiplier=0;
- timeout.ReadTotalTimeoutConstant=0;
- timeout.WriteTotalTimeoutMultiplier=0;
- timeout.WriteTotalTimeoutConstant=0;
- result=SetCommTimeouts(m_hCom,&timeout);
- ASSERT(result);
- if(!result)
- return FALSE;
- m_bOpen=TRUE;
- return TRUE;
- }
- BOOL CCommRS232::CloseComm()
- {
- m_bOpen=FALSE;
- if(m_hCom!=INVALID_HANDLE_VALUE)
- {
- BOOL result=CloseHandle(m_hCom);
- if(!result)
- return FALSE;
- }
- return TRUE;
- }
- int CCommRS232::Read(BYTE *pBuf, int len)
- {
- DWORD nTick = GetTickCount();
- if(m_hCom == INVALID_HANDLE_VALUE)
- {
- return 0;
- }
- if( pBuf == NULL || !::AfxIsValidAddress(pBuf, len, FALSE ) )
- {
- return 0;
- }
- if( !m_bOpen )
- {
- return 0;
- }
- BOOL bReadStatus;
- DWORD dwBytesRead, dwErrorFlags;
- COMSTAT ComStat;
- ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
- while( ComStat.cbInQue != len )
- {
- if( GetTickCount() - nTick > 3000 ) break;
- ClearCommError( m_hCom, &dwErrorFlags, &ComStat );
- Sleep(100);
- }
- //if( !ComStat.cbInQue ) return( 0 );
- dwBytesRead = (DWORD) ComStat.cbInQue;
- if( len < (int) dwBytesRead ) dwBytesRead = (DWORD) len;
- bReadStatus = ReadFile( m_hCom, pBuf, dwBytesRead, &dwBytesRead , NULL);
- if( !bReadStatus )
- {
- DWORD dwError;
- dwError = GetLastError();
- CString strMsg;
- strMsg.Format("ReadFile Error! ErrorCode = %d", dwError);
- //AfxMessageBox(strMsg);
- PurgeComm(m_hCom, PURGE_TXABORT|
- PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
- return 0;
- }
- PurgeComm(m_hCom, PURGE_TXABORT|
- PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
- return( (int) dwBytesRead );
- }
- int CCommRS232::Write(BYTE *pBuf, int len)
- {
- if( !m_bOpen )
- {
- return 0;
- }
- if(m_hCom == INVALID_HANDLE_VALUE || len<=0)
- {
- return 0;
- }
- DWORD dwResult = 0;
- BOOL bResult=WriteFile( m_hCom, pBuf, (DWORD)len, &dwResult, NULL );
- if(bResult)
- {
- return dwResult;
- }
- else
- {
- return 0;
- }
- }
|