#pragma once


#include "robbase.h"

#include <rvb\rdg.h>
#include <rvb\rvtypes.h> 

//#define __USE_YANYU_GPIO_   1



enum PLC_DATA_TYPE
{
    PDT_NONE	= -1,	//未定义
	PDT_BIT	 	= 0,	//位类型(包括位块: 按位存储,往字节低位对齐), TRUE/FALSE
	PDT_BYTE 	= 1,	//字节(1字节: char存储格式)
	PDT_WORD 	= 2,	//字(2字节: short存储格式)
	PDT_DWORD	= 3,	//双字(4字节: int存储格式)
	PDT_FLOAT	= 4,	//浮点数(4字节: float存储格式)
 	PDT_TEXT    = 5,    //字符串 
	PDT_POINT_3D ,      //3D点  
};


class ROBBASE_API CDevice
{
public:
	CDevice(void);
	virtual ~CDevice(void);
	 
	enum COMM_TYPE{
		CT_UNKNOWN=-1,
	    CT_SCOMM=0, //串口
		CT_DIO=1,   //本地IO
		CT_INET=2,  //因特网络
	};

	virtual BOOL Create(int module, int model );
	BOOL         Create();

	virtual void Destroy();

	virtual BOOL Open(int portIndex=-1);
	virtual void Close();

	int GetCommType();

	BOOL IsOpened();
		
	 virtual  BOOL Read( int type, int address , char* pValue, int count);
	 virtual  BOOL Read( int type, int address , unsigned char* pValue );
	 virtual  BOOL Read( int type, int address , short int* pValue );
	 virtual  BOOL Read( int type, int address ,   int* pValue );
	 virtual  BOOL Read( int type, int address ,   float* pValue );
	  
	 virtual   BOOL Write( int addrType, int address , char value, int count);
	 virtual   BOOL Write( int addrType, int address , unsigned char value);
	 virtual   BOOL Write( int addrType, int address , short int value);
	 virtual   BOOL Write( int addrType, int address , int value);
	 virtual   BOOL Write( int addrType, int address , float value);

	 virtual BOOL ReadEx(int dataType, int addrType, int address, double* pValue);
	virtual   BOOL ReadEx(int dataType, int addrType, int address, float* pValue);
	virtual   BOOL ReadEx(int dataType, int addrType, int address, int* pValue);

	 virtual  BOOL WriteEx(RvDouble data, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(int data, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(__int64 data, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(const char* strText, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(RvRgb data, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(RvSize data, int dataType, int addrType, int address);

	virtual   BOOL WriteEx(RvPoint_f data, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(RvLine_f data, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(RvRect_f data, int dataType, int addrType, int address);
	virtual   BOOL WriteEx(RvEllipse_f data, int dataType, int addrType, int address);


	virtual   BOOL WriteEx(int dataType, int addrType, int address, void* pData, int size);
	   
	BOOL WriteBuffer(const char* pDataIn, int size);
	BOOL ReadBuffer(char* pDataOut, int& size);
	BOOL ReadBuffer(int seperator, char* pDataOut, int& size);

	int  GetTimeout();
	void SetTimeout(int timeout);

	void SetVendorInfo(const char* strInfo);
	const char* GetVendorInfo();

 	BOOL IsVirtual();
	void SetVirtual(BOOL flag);
	void ClearVirtual();
	BOOL WriteVirtual(const char* pDataIn, int size);
	BOOL ReadVirtual(char* pDataOut, int& size);

	int  m_moduleId; //通信模块类型标识
	int  m_ModelId;  //设备硬件型号标识

	int  m_nLocalAddr; //PLC设备地址
	int  m_nRomoteAddr; //PLC设备地址
	
	
	

	//用于串口设备
	int m_nBaudRate;  //波特率
	int m_nDataBits;  //数据位
    int m_nStopBits;  //停止位
	int m_nParityBits;  
	int m_nPortIndex;

	//用于TCP/IP设备
	int  m_nNetAddress;  //ip 地址
	int  m_nNetPort;     //端口
	BOOL m_bRetryTcp;    //重联TCP

	BOOL m_bEnabled;
protected:
	HANDLE m_hDevice;

	CRITICAL_SECTION m_cs;

	COMM_TYPE  m_nCommType;

	int   m_nTimeout;
private:
	

	//数组相关
	//BOOL  m_bWrite2Array; //是否读写到数组
	//int   m_nDataSize;  //in bytes
	//void* m_pArrayData;
	//int   m_nMaxElemCount;
	//int   m_nCurElemCount;
	//int   m_nCurDataType;
	//int   m_nCurAddrType;

	
	
};


ROBBASE_API BOOL WriteReadingToDevice0(RvReadingBank bank, int index, CDevice* pDevice);
ROBBASE_API BOOL WriteReadingToDevice1(RvReadingBank bank, const char* strName, CDevice* pDevice);
ROBBASE_API BOOL WriteReadingToDevice2(RvReadingBank bank, CDevice* pDevice);

