#ifndef _DOWN_COMM_H_
#define _DOWN_COMM_H_


/**********************************/
//#define PVHS70_STYLE_NULL   0
//#define PVHS70_STYLE_COM1   1
//#define PVHS70_STYLE_COM2   2
//#define PVHS70_STYLE_COM3   3
//#define PVHS70_STYLE_COM4   4
//#define PVHS70_STYLE_IO     5


//#define PVHS70_BROKE_NULL      0
//#define PVHS70_BROKE_SOFTWARE1 1
//#define PVHS70_BROKE_SOFTWARE2 2
//#define PVHS70_BROKE_HARDWARE  3


//#define BOARD_500V_NULL     0
//#define BOARD_500V_PROGRAM  1
//#define BOARD_500V_SINGLE   2

//#define HIGHVOL_MODE_SINGLE 0
//#define HIGHVOL_MODE_2P     1
//#define HIGHVOL_MODE_3P     2
//#define HIGHVOL_MODE_3P_EX  3



/**********************************/
#define COMM_ERROR_SUCCESS       0
#define COMM_ERROR_TIME         -1
#define COMM_ERROR_USER         -2
#define COMM_ERROR_CHECK        -3
#define COMM_ERROR_BUSY         -4
#define COMM_ERROR_DEFAULT      -5
#define COMM_ERROR_OVER         -6
#define COMM_ERROR_ADDRESS	    -7
#define COMM_ERROR_FORMAT       -8
#define COMM_ERROR_ANSWER       -9
#define COMM_ERROR_FILE         -0xA
#define COMM_ERROR_READY        -0xB
#define COMM_ERROR_CRAFT        -0xC
#define COMM_ERROR_AGAIN        -0xD
#define COMM_ERROR_BUFFER_FULL  -0xE

/*********************************/
#define  COMM_COMMAND_SUCESS    0x0000
#define  COMM_COMMAND_DATA      0x0001
#define  COMM_COMMAND_AGAIN     0x0002
#define  COMM_COMMAND_ASK       0x0003
#define  COMM_COMMAND_ANSWER    0x0004
#define  COMM_COMMAND_FIRST     0x0005
#define  COMM_COMMAND_NEXT      0x0006
#define  COMM_COMMAND_LAST      0x0007
#define  COMM_COMMAND_CHECK     0x0008
#define  COMM_COMMAND_KEY       0x0009
#define  COMM_COMMAND_LIMITS    0x000A
#define  COMM_COMMAND_CODE      0x000B
#define  COMM_COMMAND_READY     0x000C
#define  COMM_COMMAND_FILE      0x000D
#define  COMM_COMMAND_DIR       0x000E
#define  COMM_COMMAND_WORKER    0x000F
#define  COMM_COMMAND_CLOSE     0x0010

#define  COMM_COMMAND_TEST_ASK1 0x0011
#define  COMM_COMMAND_TEST_ASK2 0x0012
#define  COMM_COMMAND_TEST_ASK3 0x0013
#define  COMM_COMMAND_TEST_ASK4 0x0014
#define  COMM_COMMAND_TEST_ASK5 0x0015

#define  COMM_COMMAND_TEST_ANSWER1 0x0016
#define  COMM_COMMAND_TEST_ANSWER2 0x0017
#define  COMM_COMMAND_TEST_ANSWER3 0x0018
#define  COMM_COMMAND_TEST_ANSWER4 0x0019
#define  COMM_COMMAND_TEST_ANSWER5 0x001A

#define  COMM_COMMAND_TEST_ASK6     0x001B
#define  COMM_COMMAND_TEST_ASK7     0x001C
#define  COMM_COMMAND_TEST_ASK8     0x001D
#define  COMM_COMMAND_TEST_ASK9     0x001E
#define  COMM_COMMAND_TEST_ASK10    0x001F

#define  COMM_COMMAND_TEST_ANSWER6  0x0020
#define  COMM_COMMAND_TEST_ANSWER7  0x0021
#define  COMM_COMMAND_TEST_ANSWER8  0x0022
#define  COMM_COMMAND_TEST_ANSWER9  0x0023
#define  COMM_COMMAND_TEST_ANSWER10 0x0024

#define COMM_COMMAND_TEST_SAVE         0x0025

#define COMM_COMMAND_ADJUST_ASK         0x30
#define COMM_COMMAND_ADJUST_ANSWER      0x31

#define COMM_COMMAND_RST_ASK1           0x0080
#define COMM_COMMAND_RST_ASK2           0x0081
#define COMM_COMMAND_RST_ASK3           0x0082
#define COMM_COMMAND_RST_ASK4           0x0083
#define COMM_COMMAND_RST_ASK5           0x0084
#define COMM_COMMAND_RST_ASK6           0x0085
#define COMM_COMMAND_RST_ASK7           0x0086
#define COMM_COMMAND_RST_ASK8           0x0087

#define COMM_COMMAND_RST_ANSWER1        0x0088
#define COMM_COMMAND_RST_ANSWER2        0x0089
#define COMM_COMMAND_RST_ANSWER3        0x008A
#define COMM_COMMAND_RST_ANSWER4        0x008B
#define COMM_COMMAND_RST_ANSWER5        0x008C
#define COMM_COMMAND_RST_ANSWER6        0x008D
#define COMM_COMMAND_RST_ANSWER7        0x008E
#define COMM_COMMAND_RST_ANSWER8        0x008F

#define  COMM_COMMAND_END      0x00FF
/*********************************/
#define CBR_50                 50lu
#define CBR_110               110lu
#define CBR_300               300lu
#define CBR_600               600lu
#define CBR_1200             1200lu
#define CBR_2400             2400lu
#define CBR_4800             4800lu
#define CBR_9600             9600lu
#define CBR_14400           14400lu
#define CBR_19200           19200lu
#define CBR_38400           38400lu
#define CBR_56000           56000lu
#define CBR_57600           57600lu
#define CBR_115200         115200lu
#define CBR_128000         128000lu
#define CBR_256000         256000lu

#define NOPARITY            0
#define ODDPARITY           1
#define EVENPARITY          2
#define MARKPARITY          3
#define SPACEPARITY         4

#define ONESTOPBIT          0
#define ONE5STOPBITS        1
#define TWOSTOPBITS         2

#define DTR_CONTROL_DISABLE    0x00
#define DTR_CONTROL_ENABLE     0x01
#define DTR_CONTROL_HANDSHAKE  0x02

#define RTS_CONTROL_DISABLE    0x00
#define RTS_CONTROL_ENABLE     0x01
#define RTS_CONTROL_HANDSHAKE  0x02
#define RTS_CONTROL_TOGGLE     0x03

#define COMM_DB25_TXD  2  //数据发送端,输出
#define COMM_DB25_RXD  3  //数据接收端,输入
#define COMM_DB25_RTS  4  //请求发送(计算机要求发送数据),输出
#define COMM_DB25_CTS  5  //清除发送(MODEM准备接收数据),输入
#define COMM_DB25_DSR  6  //数据设备准备就绪,输入
#define COMM_DB25_SG   7  //信号地
#define COMM_DB25_DCD  8  //数据载波检测,输入
#define COMM_DB25_DTR 20  //数据终端准备就绪(计算机),输出 
#define COMM_DB25_RI  22  //响铃指示,输入


#define COMM_DB9_TXD   3  //数据发送端,输出
#define COMM_DB9_RXD   2  //数据接收端,输入
#define COMM_DB9_RTS   7  //请求发送(计算机要求发送数据),输出
#define COMM_DB9_CTS   8  //清除发送(MODEM准备接收数据),输入
#define COMM_DB9_DSR   6  //数据设备准备就绪,输入
#define COMM_DB9_SG    5  //信号地
#define COMM_DB9_DCD   1  //数据载波检测,输入
#define COMM_DB9_DTR   4  //数据终端准备就绪(计算机),输出 
#define COMM_DB9_RI    9  //响铃指示,输入
               
#define     COMM_REGISTER_THR     0  //Offset to Xmit hld reg (write)
#define     COMM_REGISTER_RBR     0  //Receiver holding buffer (read)
#define     COMM_REGISTER_BAUDL   0  //Baud Low  8  bit register
#define     COMM_REGISTER_BAUDH   1  //Baud High 8  bit register
#define     COMM_REGISTER_IER     1  //interrupt enable register
#define     COMM_REGISTER_IIR     2  //Interrupt identification register
#define     COMM_REGISTER_FCR     2  //FIFO control
#define     COMM_REGISTER_LCR     3  //Line control register
#define     COMM_REGISTER_MCR     4  //MODEM control register
#define     COMM_REGISTER_LSR     5  //Line status register
#define     COMM_REGISTER_MSR     6  //MODEM status register
#define     COMM_REGISTER_SR      7  //scratch register

typedef struct tagCOMMCONTROL
{
	int  nIRQ;
	int  nBase;
	int  nErrorCode;
	void interrupt (*pOldFc) (...);

	DWORD   dwBaud;
	WORD    wDataBit;
	WORD    wStartBit;
	WORD    wStopBit;
	WORD    wParity;

	BYTE* pBufferR;      //输入缓冲区
	int   nSizeR;        //输入缓冲区的尺寸
	int   nTailR;        //输入缓冲区的尾指针
	int   nHeadR;        //输入缓冲区的头指针
	BOOL  bFullR;        //输入缓冲区的标志

	BYTE* pBufferS;      //输出缓冲区
	int   nSizeS;        //输出缓冲区的尺寸
	int   nTailS;        //输出缓冲区的尾指针
	int   nHeadS;        //输出缓冲区的头指针
	BOOL  bFullS;        //输出缓冲区的标志

}COMMCONTROL, *LPCOMMCONTROL;


class CMotorNet
{
private:
	//BOOL   m_bReceive;
	//BOOL   m_bSend;

	//void  FAR (*m_pReceivePageFc)(int nPage);
	//void  FAR (*m_pSendPageFc)(int nPage);
	//int     m_nCommOverTime;
	//DWORD   m_dwCommWaittingTime;

	//WORD    m_wIPAddress;
	//WORD    m_wOtherIPAddress;
	//IPHEAD  m_sIPHead;
	//WORD    m_wCommand;

	//BOOL	m_b386;
	//BOOL    m_bRelay;
	//BOOL	m_bSingle;

	//BYTE    m_byInputBuffer[COMM_INPUT_BUFFER_SIZE];

	//DWORD   m_dwReceiveLength;

private:
	BOOL    m_bInterrupt;
	int     m_nIRQ;


	int		m_nErrorCode;
public:
	COMMCONTROL* GetCommControl();
private:
	void    CloseInterrupt();
public:

	//void    SetBaud(DWORD dwValue){m_dwBaud=dwValue;}
	//DWORD   GetBaud(){return(m_dwBaud);}
	//void    SetDataBit(WORD wDataBit){m_wDataBit=wDataBit;}
	//WORD    GetDataBit(){return(m_wDataBit);}
	//void    SetStopBit(WORD wStopBit){m_wStopBit = wStopBit;}
	//WORD    GetStopBit(){return(m_wStopBit);}
	//WORD    GetParityStyle(){return(m_wParityStyle);}
	//void    SetCommBase(int nBase=0x2f8){m_nCommBase=nBase;}
	//void	  SetCommBase(char *PComm);
	//int     GetCommBase(){return(m_nCommBase);}
	//void    ModifyCommAttrib();
	//void    SetParityStyle(WORD wParityStyle){m_wParityStyle=wParityStyle;}


	void    ChangeParity(WORD wParity);

	void    InitComm(int nBase, DWORD dwBaud, WORD wDataBit = 8, WORD wStartBit = 1,
				WORD wStopBit = 1, WORD wParity = NOPARITY, BOOL bInterrupt = FALSE);
	void    CloseComm();
	void    OpenIRQ();
	void    CloseIRQ();
public:
	BOOL    IsCommData();
	BOOL	IsBusy();
	BOOL    WaitCommData(DWORD dwWait);

	BYTE	ReceiveData();
	BOOL	SendData(BYTE nCh);

	int 	ReceiveBuffer(BYTE* pBuffer, int nCount, DWORD dwTimeOut = 0lu);
	BOOL	SendBuffer(BYTE* pBuffer, int nCount);

	void    ClearBufferR();
	void    ClearBufferS();
    BOOL    GetInterruptFlags(){return (m_bInterrupt);}
public:
	int	    GetLastCommError(){return(m_nErrorCode);};
	void	SetLastCommError(int nErrorCode){m_nErrorCode=nErrorCode;}
	void    ClearLastCommError(){m_nErrorCode=COMM_ERROR_SUCCESS;}

public:
	//void    SetCommWaittingTime(DWORD dwValue){m_dwCommWaittingTime=dwValue;};
	//DWORD   GetCommWaittingTImer(){return(m_dwCommWaittingTime);}	
	//void    SetReceivePageFc(void  FAR (*pFc)(int nPage)){m_pReceivePageFc=pFc;}
	//void    SetSendPageFc(void  FAR (*pFc)(int nPage)){m_pSendPageFc=pFc;}
	//void    SetSingle(BOOL bSingle){m_bSingle=bSingle;}
	//BOOL	  GetSingle(){return(m_bSingle);}
	//void	  Set386(BOOL b386){m_b386=b386;}
	//BOOL	  Get386(){return(m_b386);}	
	//void    SetDelayEnable(BOOL bValue){m_bRelay=bValue;}
	//BOOL    GetDelayEnabel(){return(m_bRelay);}
	//void	  SetIPAddress(WORD wAddress){m_wIPAddress=wAddress;}
	//WORD	  GetIPAddress(){return(m_wIPAddress);}

	//WORD  GetReceiveLength(){return(m_dwReceiveLength);}
	//WORD  GetCommand(){return(m_wCommand);}
	//WORD	GetOtherIPAddress(){return(m_wOtherIPAddress);}
	//void  SetCommOverTime(int nTime=5){m_nCommOverTime=nTime;}

	//BOOL	  ReceiveSendAnswer(WORD wCommand);
	//BOOL    SendReceiveAnswer(WORD wCommand);	
	//BOOL	ReceivePage(BOOL bTest=FALSE);
	//BOOL	SendPage(BYTE *pbyData,WORD wCount,WORD wReceiveIPAddress,WORD wCommand,BOOL bTest=TRUE);
	//BOOL  ReceiveFile(char *pStr,DWORD dwFileLength);
	//BOOL  SendFile(char *pStr,char *pStrTargetPath,WORD wAddress);//,WORD wSendAfterDelay=150,WORD wSendPageDelay=50);
	//BOOL	SetChange(char *pStr,BOOL bChange);

	//BYTE	*GetInputBuffer(){return(m_byInputBuffer);}
public:
	CMotorNet();
	~CMotorNet();
};
/*
#define COMM_INPUT_BUFFER_SIZE (1*1024)
#define FILE_PAGE_LENGTH 1024

struct IPHEAD
{
	BYTE   chFormat[12];    //12
	WORD   wReceiveIPAddress; //14
	WORD   wSendIPAddress;	//16
	WORD   wCommand;        //18
	WORD   wLength;         //20
	IPHEAD();
};

struct FIRST_FILE_PAGE
{
	DWORD  dwFileLength;
	char   chFileName[16];
	char   chTargetPath[256];
};

struct NEXT_FILE_PAGE
{
	WORD   wPage;
	char   chBuffer[FILE_PAGE_LENGTH];
};

struct RECEIVE_FILE_PAGE
{
	WORD  wAddress;
	char  chFileName[256];
};
*/


#endif
