/******************************************************************************
Copyright (C) 2005 Matteo Lucarelli

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
******************************************************************************/

// wSerial interface for windows COM ports
// buffer are concateneted with previous read, 
// then to perform a simple read you have to clear before

#if !defined(_WSERIAL_ML_INCLUDED_)
#define _WSERIAL_ML_INCLUDED_

// error codes
#define WSERIALERR_NONE			0
#define WSERIALERR_ACCESS		1
#define WSERIALERR_UNCOMPLETE	2
#define WSERIALERR_NOHANDLE		3
#define WSERIALERR_NODATA		4

class wSerial  
{
public:

	wSerial(	DWORD buffersdim,	// buffer dimension in bytes
				LPCTSTR port,		// port to open as string ("COM1",COM2",ecc)
				DWORD baudrate,		// speed setting (bps) can be CBR_NUM (windows define) or num
				BYTE parity,		// EVENPARITY:2,MARKPARITY:3NOPARITY:0,:ODDPARITY:1SPACEPARITY:4
				BYTE bytesize,		// byte size in bits
				BYTE stopbits);		// ONESTOPBIT:0,ONE5STOPBITS:1,TWOSTOPBITS:2 	2 stop bits.
	virtual ~wSerial();

	// change port settings after instance
	bool reset(DWORD baudrate,BYTE parity,BYTE bytesize,BYTE stopbits);

	// write len bytes of buff
	bool write(char* buff,DWORD len);
	bool read();
	void clear();	// restart buffers

	unsigned long getbuffdim(){return m_BufferDim;} // buffer size in byte
	int getlasterror(){return m_Error;}	// last error code

	char* m_Buffer;			// binary
	char* m_BufferString;   // string (null-terminated, can't contains zeros)

protected:

	HANDLE mh_Port;

	int m_Error;
	DWORD m_BufferMaxDim;
	DWORD m_BufferDim;
	char* m_tmpBuffer;
};

#endif // _WSERIAL_ML_INCLUDED_