//<Header File>

#ifndef __SERIALPORTTOOL_H__
#define __SERIALPORTTOOL_H__

#define WM_COMM_BREAK_DETECTED WM_USER+1
#define WM_COMM_CTS_DETECTED WM_USER+2
#define WM_COMM_DSR_DETECTED WM_USER+3
#define WM_COMM_ERR_DETECTED WM_USER+4
#define WM_COMM_RING_DETECTED WM_USER+5
#define WM_COMM_RLSD_DETECTED WM_USER+6
#define WM_COMM_RXCHAR WM_USER+7
#define WM_COMM_RXFLAG_DETECTED WM_USER+8
#define WM_COMM_TXEMPTY_DETECTED WM_USER+9

class CSerialPort
{
public:
	CSerialPort();
	virtual ~CSerialPort();
	HANDLE OpenUartPort(short PortNum,LPCTSTR Settings);
	BOOL StartMonitoring();
	BOOL RestartMonitoring();
	BOOL StopMonitoring();
	DWORD GetWriteBufferSize();
	DWORD GetCommEvents();
	DCB   GetDCB();
	DWORD WriteChars(unsigned char *Data,unsigned char length);
	DWORD ReceiveChars(unsigned char *RxBuff,DWORD length);
	BOOL ClosePort(HANDLE hCtrl); UINT m_nWriteSize;
 
protected:
 void ProcessErrorMessage(char* ErrorText);
 static UINT CommThread(LPVOID pParam);
 static void ReceiveChar(CSerialPort* port,COMSTAT comstat);
 static void WriteChar(CSerialPort* port);
 //thread
 CWinThread* m_Thread;
 //synchronisation objects
 CRITICAL_SECTION m_csCommunicationSync;
 BOOL m_bThreadAlive;
 //handles
 HANDLE m_hShutdownEvent;
 HANDLE m_hComm;
 HANDLE m_hWriteEvent;
 HANDLE m_hEventArray[3];
 //structures
 OVERLAPPED m_ov;
 COMMTIMEOUTS m_CommTimeOuts;
 COMSTAT m_state;
 DCB m_dcb;
 //owner window
 CWnd* m_pOwner;
 //misc
 UINT m_nPortNr;
 unsigned char* m_szWriteBuffer;
 DWORD m_dwCommEvents;
 DWORD m_nWriteBufferSize;
};
#endif __SERIALPORTTOOL_H__

//</Header File>


