template<class T=char, int nSize=100> class CSerialComms
{
public:
T& operator[]( int nIndex ) { return m_buf[nIndex]; }
CSerialComms& operator=( const CSerialComms& r )
{
memcpy( m_buf, r.m_buf, nSize );
m_cto = r.m_cto;
m_dcb = r.m_dcb;
m_hFile = r.m_hFile;
return *this;
}
inline int SetTimeouts( DWORD dwReadInterval = 1, DWORD dwReadTotalTimeout = 100, DWORD dwReadTotalTimeoutConstant = 1000, DWORD dwWriteTotalTimeout = 0, DWORD dwWriteTotalTimeoutConstant = 0 )
{
int nError = SUCCESS;
m_cto.ReadIntervalTimeout = dwReadInterval;
m_cto.ReadTotalTimeoutMultiplier = dwReadTotalTimeout;
m_cto.ReadTotalTimeoutConstant = dwReadTotalTimeoutConstant;
m_cto.WriteTotalTimeoutMultiplier = dwWriteTotalTimeout;
m_cto.WriteTotalTimeoutConstant = dwWriteTotalTimeoutConstant;
if( !::SetCommTimeouts( m_hFile, &m_cto) )
{
nError = GetLastError();
}
return nError;
}
inline int OpenPort( LPCTSTR szPort = "COM1" )
{
int nError = SUCCESS;
m_hFile = ::CreateFile( szPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if( m_hFile == INVALID_HANDLE_VALUE )
{
nError = GetLastError();
}
return nError;
}
inline int SetCommsDevice( DWORD dwBaud = 57600, BYTE iParity = NOPARITY, BYTE iStopBits = ONESTOPBIT, BYTE iByteSize = 8 )
{
int nError = SUCCESS;
memset( &m_dcb, 0, sizeof( m_dcb ) );
m_dcb.DCBlength = sizeof( m_dcb );
m_dcb.BaudRate = dwBaud;
m_dcb.Parity = iParity;
m_dcb.StopBits = iStopBits;
m_dcb.ByteSize = iByteSize;
if( !::SetCommState( m_hFile, &m_dcb ) )
{
nError = GetLastError();
}
return nError;
}
DWORD TxData( T* szBuf )
{
DWORD nowrote = 0;
memset( m_buf, 0, sizeof( m_buf ) );
if( sizeof( m_buf ) >= strlen( szBuf ) ) // check to see if buffer allocated is big enough
{
memcpy( &m_buf[1], szBuf, strlen( szBuf ) );
m_buf[0] = 0x2;
m_buf[ strlen(szBuf) + 1 ] = 0x3;
return TxData();
}
return BUFFEROVERFLOW;
}
DWORD TxData()
{
DWORD nowrote = 0;
::WriteFile( m_hFile, m_buf, strlen(m_buf), &nowrote, NULL );
return nowrote;
}
CSerialComms( void ){ memset( m_buf, 0, nSize ); };
~CSerialComms(){};
private:
T m_buf[nSize];
COMMTIMEOUTS m_cto;
DCB m_dcb;
HANDLE m_hFile;
enum { FAILED = 0, SUCCESS = 1, BUFFEROVERFLOW = 2 };
};
#endif // !defined(AFX_SERIALCOMMS_H__BA76EBFF_6F02_4E79_BC65_DD4C2655354B__INCLUDED_)
template<class T=char, int nSize=100> class CSerialComms
{
public:
T& operator[]( int nIndex ) { return m_buf[nIndex]; }
CSerialComms& operator=( const CSerialComms& r )
{
memcpy( m_buf, r.m_buf, nSize );
m_cto = r.m_cto;
m_dcb = r.m_dcb;
m_hFile = r.m_hFile;
return *this;
}
inline int SetTimeouts( DWORD dwReadInterval = 1, DWORD dwReadTotalTimeout = 100, DWORD dwReadTotalTimeoutConstant = 1000, DWORD dwWriteTotalTimeout = 0, DWORD dwWriteTotalTimeoutConstant = 0 )
{
int nError = SUCCESS;
m_cto.ReadIntervalTimeout = dwReadInterval;
m_cto.ReadTotalTimeoutMultiplier = dwReadTotalTimeout;
m_cto.ReadTotalTimeoutConstant = dwReadTotalTimeoutConstant;
m_cto.WriteTotalTimeoutMultiplier = dwWriteTotalTimeout;
m_cto.WriteTotalTimeoutConstant = dwWriteTotalTimeoutConstant;
if( !::SetCommTimeouts( m_hFile, &m_cto) )
{
nError = GetLastError();
}
return nError;
}
inline int OpenPort( LPCTSTR szPort = "COM1" )
{
int nError = SUCCESS;
m_hFile = ::CreateFile( szPort, GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if( m_hFile == INVALID_HANDLE_VALUE )
{
nError = GetLastError();
}
return nError;
}
inline int SetCommsDevice( DWORD dwBaud = 57600, BYTE iParity = NOPARITY, BYTE iStopBits = ONESTOPBIT, BYTE iByteSize = 8 )
{
int nError = SUCCESS;
memset( &m_dcb, 0, sizeof( m_dcb ) );
m_dcb.DCBlength = sizeof( m_dcb );
m_dcb.BaudRate = dwBaud;
m_dcb.Parity = iParity;
m_dcb.StopBits = iStopBits;
m_dcb.ByteSize = iByteSize;
if( !::SetCommState( m_hFile, &m_dcb ) )
{
nError = GetLastError();
}
return nError;
}
DWORD TxData( T* szBuf )
{
DWORD nowrote = 0;
memset( m_buf, 0, sizeof( m_buf ) );
if( sizeof( m_buf ) >= strlen( szBuf ) ) // check to see if buffer allocated is big enough
{
memcpy( &m_buf[1], szBuf, strlen( szBuf ) );
m_buf[0] = 0x2;
m_buf[ strlen(szBuf) + 1 ] = 0x3;
return TxData();
}
return BUFFEROVERFLOW;
}
DWORD TxData()
{
DWORD nowrote = 0;
::WriteFile( m_hFile, m_buf, strlen(m_buf), &nowrote, NULL );
return nowrote;
}
CSerialComms( void ){ memset( m_buf, 0, nSize ); };
~CSerialComms(){};
private:
T m_buf[nSize];
COMMTIMEOUTS m_cto;
DCB m_dcb;
HANDLE m_hFile;
enum { FAILED = 0, SUCCESS = 1, BUFFEROVERFLOW = 2 };
};
#endif // !defined(AFX_SERIALCOMMS_H__BA76EBFF_6F02_4E79_BC65_DD4C2655354B__INCLUDED_)
To copy to clipboard, switch view to plain text mode
Bookmarks