/*-----------------------------------------------------------------------*/
/*   FS-MTS  1.0  Ver.                                                   */
/*   Copyright (C) 2000 FIRST SAIL                                       */
/*   All Rights Reserved                                                 */
/*   Warning:                                                            */
/*       This program author is GuoHeng,this system is protected         */
/*   by copyright law and international Treaties . Unauthorized          */
/*   reproduction of  distribution of this  system, or any portion       */
/*   of it , may result in severe civil and criminal penalties ,         */
/*   and will be prosecuted to the maximum extern possible under         */
/*   the law.                                                            */
/*                                                                       */
/*   Update Date:2002-06-25                                              */
/*   Update Time:22:35:00                                                */
/*-----------------------------------------------------------------------*/

#ifndef _RELAY_H_
#define _RELAY_H_

//输出/输出口的消息映射定义
#define  DECLARE_PLC_PORT()         extern DWORD FAR dwAfxVirtualPort[][5]
#define  BEGIN_PORT_MAP()           DWORD FAR dwAfxVirtualPort[][5]={
#define  ON_PORT(a, b, c, d, e)     (DWORD)a, (DWORD)b, (DWORD)c, (DWORD)d, (DWORD)e,
#define  END_PORT_MAP()             (DWORD)0, (DWORD)0, (DWORD)0, (DWORD)0, (DWORD)0};

#define  MAX_INPUT_COUNT  40       //最大支持的输入口数量
#define  MAX_RELAY_COUNT  240      //最大支持的输出口数据



#define BYTE_0_ACTIVE     0//字节操作0有效
#define BYTE_1_ACTIVE     1//字节操作1有效
#define WORD_0_ACTIVE     2//短字操作0有效
#define WORD_1_ACTIVE     3//短字操作1有效
#define DWORD_0_ACTIVE    4//双字操作0有效
#define DWORD_1_ACTIVE    5//双字操作1有效


//#define STD_OUTPORT_BYTE_BOARD      0  //8位数据0有效
//#define PC_OUTPORT_BYTE_BOARD       1  //8位数据1有效
//#define STD_OUTPORT_WORD_BOARD      2  //16位数据0有效
//#define PC_OUTPORT_WORD_BOARD       3  //16位数据1有效
//#define STD_OUTPORT_DWORD_BOARD     4  //32位数据0有效
//#define PC_OUTPORT_DWORD_BOARD      5  //32位数据1有效

#define BYTE1_0_ACTIVE_SERIAL   10
#define BYTE2_0_ACTIVE_SERIAL   11
#define BYTE3_0_ACTIVE_SERIAL   12
#define BYTE4_0_ACTIVE_SERIAL   13
#define BYTE5_0_ACTIVE_SERIAL   14
#define BYTE6_0_ACTIVE_SERIAL   15
#define BYTE7_0_ACTIVE_SERIAL   16
#define BYTE8_0_ACTIVE_SERIAL   17

#define BYTE1_1_ACTIVE_SERIAL   18
#define BYTE2_1_ACTIVE_SERIAL   19
#define BYTE3_1_ACTIVE_SERIAL   20
#define BYTE4_1_ACTIVE_SERIAL   21
#define BYTE5_1_ACTIVE_SERIAL   22
#define BYTE6_1_ACTIVE_SERIAL   23
#define BYTE7_1_ACTIVE_SERIAL   24
#define BYTE8_1_ACTIVE_SERIAL   25


#define MAX_RELAY_NAME_LENGTH      16 //输入/输出口的名称最大数符数 - 1

#define  MAX_DRAW_RELAY_COUNT      22


//输入/输出口属性
typedef struct _tagRELAYINFORMATION
{
	char    strRelayName[MAX_RELAY_NAME_LENGTH];
	WORD	wPort;
	WORD	wBit;
	WORD    wBoardIndex;
	WORD    wBoardStyle;
}RELAYINFORMATION;

//获得输出口的类型
int        FAR  GetOutportBoardStyle();
int        FAR  GetOutportBoardStyle2();

//获得输入口的类型
int        FAR  GetInportBoardStyle();
int        FAR  GetInportBoardStyle2();

//建立输入和输出口
BOOL       FAR  BuildRelayInformation(CRegister *pRegister);
//建立输入口(严禁调用)
//BOOL       FAR  BuildInputInformation(CRegister *pRegister);
//获得指定输出口的端口号和字(节)的位序号
BOOL       FAR  GetRelayPortAndBit(char *pStr,int *pnPort,int *pnBit, int* pnBoardStyle = NULL);
//获得指定输入口的端口号和字(节)的位序号
BOOL       FAR  GetInputPortAndBit(char *pStr,int *pnPort,int *pnBit, int* pnBoardStyle = NULL);
//判断指定输入口的逻辑状态

//高级端口8位读
BYTE INPORTB(WORD nPort);
//高级端口16位读
WORD INPORT(WORD nPort);

//高级端口8位写
void FAR  OUTPORTB(WORD nPort, BYTE nValue);
//高级端口16位写
void OUTPORT(WORD nPort, WORD wValue);

//执行输出字符串（老版本）
BOOL       FAR  ExculatateRelay(char *pStr,
	CRegister *pRegister, BOOL bDisplay=FALSE,BOOL bOpen=TRUE);
//执行输出字符串（新版本）
BOOL       FAR  ExecuteRelay(char *pStr,
	CRegister *pRegister, BOOL bDisplay=FALSE,BOOL bOpen=TRUE);

void ClearRelay(int nTime);
//void FlushRelay(int nTime);
void RefrushRelay(int nTime);


//打开I/O映射文件
CRegister* FAR  OpenRelayRegister(char *pStr);
//关闭I/O映射文件
BOOL       FAR  CloseRelayRegister(CRegister **pRegister);

//设置调试状态输出串执行属性
void       FAR  SetRelayAttrib(BOOL bDebug=FALSE,int nTime=2000);

//初始化输出板
BOOL       FAR  InitOutportBoard();

//将输出端口的指定位清“0”
int       FAR  ResetOutportBit(int nPort,int nBit);
//将输出端口的指定位置“1”
int       FAR  SetOutportBit(int nPort,int nBit);
//获得初始化时的输出口值
DWORD       FAR  GetInitOutportBoardCommand();
DWORD       FAR  GetInitOutportBoardCommand2();

DWORD       FAR  GetInitInportBoardCommand();
DWORD       FAR  GetInitInportBoardCommand2();

//获得输入/输出板名称
char      *GetBoardName_Out(int nIndex);
char      *GetBoardName_In(int nIndex);

//获得输出板的数据
int   FAR GetOutportBoardCount();
int   FAR GetOutportBoardCount1();
int   FAR GetOutportBoardCount2();

//获得输入板的数据
int   FAR GetInportBoardCount();
int   FAR GetInportBoardCount1();
int   FAR GetInportBoardCount2();

//获得指定输出端口的索引板号
int   FAR GetRelayPortIndex(int nPort);
//获得指定索引的输入板端口号
int   FAR GetInputPort(int nIndex);
int   FAR GetRelayPort(int nIndex);
BOOL FAR IsExistRelayPort(int nPort);

DWORD FAR GetPrivOutportValue(int nPort);
DWORD FAR GetPrivOutportValue2(int nPort);

void Set586CPUAttrib (BOOL bCPU586, WORD wLoop);

BOOL FAR LockActiveInput(const char *pStr,CRegister *pRegister,int* pnPort,int* pnBit, int* pnStyle);
BOOL FAR LockActiveRelay(const char *pStr,CRegister *pRegister,int* pnPort,int* pnBit, int* pnStyle);
BOOL FAR ActiveInput(const char *pStr, CRegister *pRegister);
BOOL FAR ActiveRelay(const char *pStr, CRegister *pRegister);
BOOL FAR InputWaitting(const char* pStr, CRegister* pRegister, BOOL bActive, DWORD dwWait = -1L);

void AfxRelayAgain(WORD wDelayTime_Software);
BOOL AfxRelayAgainEx(WORD wDelayTime_Software, int nAgains = 1, WORD wDelayTime_Hardtware = 0);

void  FAR Afx_IO_Delay();
void  FAR Afx_IO_Delay2(WORD wTimes);
BYTE  FAR AfxSetIRQMask(BYTE byMask);
BYTE  FAR AfxAddIRQMask(BYTE byMask);

BOOL FAR ActiveInputEx(WORD wPort, int nBit, int nStyle);


void GPIO_ExecuteOut_Enable(BOOL bReset, BOOL bEnable, BOOL bExec);
void GPIO_InitOut_Serial_HC595();

BOOL IO_ExecuteOut_Index_Flush(int nIndex, BOOL bExec, void (*pFc)(DWORD dwPara), DWORD dwPara);
BOOL IO_ExecuteOut_Port_Flush(int nPort, BOOL bExec, void (*pFc)(DWORD dwPara), DWORD dwPara);
BOOL IO_ExecuteOut_Relay(int nPort, int nBit, BOOL bOpen, BOOL bExec);

BOOL Serial_ExecuteOut_Index_Flush(int nIndex, BOOL bExec, void (*pFc)(DWORD dwPara), DWORD dwPara);
BOOL Serial_ExecuteOut_Port_Flush(int nPort, BOOL bExec, void (*pFc)(DWORD dwPara), DWORD dwPara);
BOOL Serial_ExecuteOut_Relay(int nPort, int nBit, BOOL bOpen, BOOL bExec);

BOOL PLC_ExecuteOut_Index_Flush(int nIndex, BOOL bExec, void (*pFc)(DWORD dwPara), DWORD dwPara);
BOOL PLC_ExecuteOut_Port_Flush(int nPort, BOOL bExec, void (*pFc)(DWORD dwPara), DWORD dwPara);
BOOL PLC_ExecuteOut_Relay(int nPort, int nBit, BOOL bOpen, BOOL bExec);

BOOL PLC_ExecuteOut_Port(int nPort, DWORD dwValue, BOOL bExec, void (*pFc)(DWORD dwPara), DWORD dwPara);

void  AfxSetIOPerTimeUS(float fTimeUS);
float AfxGetIOPerTimeUS();
int AfxGetRealVirtualCount();
WORD AfxGetGPIOSerialFormat();

void PLC_SetRelaySynch_Before(BOOL bBefore);
int  PLC_GetRelaySynch_Before();

#endif