www.pudn.com > ser_port_ver10.zip > SerialPort.h


/* 
**	FILENAME			CSerialPort.h 
** 
**	PURPOSE				This class can read, write and watch one serial port. 
**						It sends messages to its owner when something happends on the port 
**						The class creates a thread for reading and writing so the main 
**						program is not blocked. 
*/ 
 
#ifndef __SERIALPORT_H__ 
#define __SERIALPORT_H__ 
#include "Data.h" 
 
 
class CSerialPort 
{														  
public: 
	// contruction and destruction 
	CSerialPort(); 
	virtual		~CSerialPort(); 
 
	// port initialisation											 
	BOOL		InitPort(CWnd* pPortOwner, UINT portnr = 1, UINT baud = 19200, char parity = 'N', UINT databits = 8, UINT stopsbits = 1, DWORD dwCommEvents = EV_RXCHAR | EV_CTS, UINT nBufferSize = 512); 
 
	// start/stop comm watching 
	BOOL		StartMonitoring(); 
	BOOL		RestartMonitoring(); 
	BOOL		StopMonitoring(); 
 
	DWORD		GetWriteBufferSize(); 
	DWORD		GetCommEvents(); 
	DCB			GetDCB(); 
 
	void		WriteToPort(char string); 
	 
 
//------------------------------------------------------------------------- 
protected: 
	static void DataProcess(char* RXbuff,DWORD ByteRead); 
	// protected memberfunctions 
	void		ProcessErrorMessage(char* ErrorText); 
	static UINT	CommThread(LPVOID pParam); 
//	static void	ReceiveChar(CSerialPort* port, COMSTAT comstat); 
	static void ReceiveChar(CSerialPort* port, COMSTAT comstat,char* RXbuff,DWORD ByteRead); 
	static void	WriteChar(CSerialPort* port); 
	//static void DataProcess(char* RXbuff,DWORD ByteRead); 
 
	// thread 
	CWinThread*			m_Thread; 
 
	// synchronisation objects 
	CRITICAL_SECTION	m_csCommunicationSync; 
	BOOL				m_bThreadAlive; 
 
	// handles 
	HANDLE				m_hShutdownEvent; 
	HANDLE				m_hComm; 
	HANDLE				m_hWriteEvent; 
 
	// Event array.  
	// One element is used for each event. There are two event handles for each port. 
	// A Write event and a receive character event which is located in the overlapped structure (m_ov.hEvent). 
	// There is a general shutdown when the port is closed.  
	HANDLE				m_hEventArray[3]; 
 
	// structures 
	OVERLAPPED			m_ov; 
	COMMTIMEOUTS		m_CommTimeouts; 
	DCB					m_dcb; 
 
	// owner window 
	CWnd*				m_pOwner; 
 
	// misc 
	UINT				m_nPortNr; 
	char*				m_szWriteBuffer; 
	DWORD				m_dwCommEvents; 
	DWORD				m_nWriteBufferSize; 
}; 
 
#endif __SERIALPORT_H__