www.pudn.com > IEC104MasterSlave.rar > IEC104Master.h, change:2013-04-16,size:4400b


// IEC104Master.h: interface for the CIEC104Master class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#if !defined(AFX_IEC104MASTER_H__2758F4F2_1D00_4035_AF0A_378DE67D3DC3__INCLUDED_) 
#define AFX_IEC104MASTER_H__2758F4F2_1D00_4035_AF0A_378DE67D3DC3__INCLUDED_ 
#include <winsock2.h> 
 
#if _MSC_VER > 1000 
#pragma once 
#endif // _MSC_VER > 1000 
 
#define MAX_ASDU_LENGTH		249 
 
// 激活命令 
#define CMD_STARTDT			0x04 
#define CMD_STOPDT			0x10 
#define CMD_TESTFR			0x40 
 
// 确认命令 
#define CMD_STARTC			0x08 
#define CMD_STOPC			0x20 
#define CMD_TESTC			0x80 
 
 
//define error mode 
#define ERROR_BY_CALLBACK	0 
#define ERROR_BY_EXCEPTION	1 
 
struct APCI{    // U-Format 
	unsigned char start;    // 起始字节 
	unsigned char len;      // 帧长度 
	unsigned char field1;   // 控制域1-4 
	unsigned char field2; 
	unsigned char field3; 
	unsigned char field4; 
}; 
 
struct ASDU_header{                 // 数据单元标识 
	unsigned char type;				// 类型标识 
	unsigned char qual;				// 可变结构限定词 
	unsigned char tx_cause_1;		// 传送原因 
	unsigned char tx_cause_2; 
	unsigned char commom_asdu_1;	// 公共地址 
	unsigned char commom_asdu_2; 
}; 
 
struct ASDU{ 
	struct ASDU_header	header;      // 数据单元标识 
	unsigned char		data[243];   // 信息体 
}; 
 
struct APDU{ 
	struct APCI	apci; 
	struct ASDU	asdu; 
}; 
 
class CIEC104Master   
{ 
private: 
	SOCKET      Socket; 
	WSADATA     wsaData; 
	SOCKADDR_IN	RemoteInfo; 
	char        RemoteHost[32]; 
	int         RemotePort; 
	bool		Connected; 
	 
	char		ErrorMode; 
	char		LastError[100]; 
	 
	HANDLE		RxThread; 
	DWORD		RxId; 
	HANDLE		TrThread; 
	DWORD		TrId; 
	 
	int			RxCounter; 
	int			LastAckTx; 
	int			TxCounter; 
	int			LastAckRx; 
	 
	int			T0;					// time-out of connection establishment (reconnection) TCP连接建立的超时时间 
	int			T1;					// time-out of send or test APDUs (wait ack).   RTU(服务器)端启动U格式测试过程后等待U格式测试应答的超时时间 
	int			T2;					// time-out for ack in case of no data messages (T2 < T1). S格式的超时时间 
	int			T3;					// time-out for sending TESFR in case of long idle state.  没有实际的数据交换时,任何一端启动U格式测试过程的最大间隔时间 
	int			K;					// maximum difference N(R) and N(S) 
	int			W;					// lastest ack after receiving W I-Format message. 
	 
	int			timer_Connect;		// associated of T0 param 
	//bool		timer_Connectflag; 
	 
	int			timer_Confirm;		// associated of T1 param 
	//bool		timer_Confirmflag; 
	 
	int			timer_S_Ack;		// associated of T2 param 
	bool		timer_S_Ackflag; 
	 
	int			timer_U_Test;		// associated of T3 param 
	bool		timer_U_Testflag; 
	 
	 
	static DWORD WINAPI TimerHandler(LPVOID lpParam); 
	static DWORD WINAPI RxHandler(LPVOID lpParam); 
	void Init(bool free); 
	 
public: 
	void ProcessFormatU(APCI *p); 
	void OnYKDeact(BYTE *msgbuf, unsigned int len); 
	void OnYKEXE(BYTE *msgbuf, unsigned int len); 
	CIEC104Master(void); 
	~CIEC104Master(void); 
	 
	int OpenLink(char *serverIP,int port=2404); 
	int CloseLink(bool free=true); 
	int SetParams(int t0=30, int t1=15, int t2=10, int t3=20, int k=12, int w=8); 
	 
	int Send_U_Msg(int cmd); 
	int Send_S_Msg(void); 
	int Send_I_Msg(BYTE *msgbuf, unsigned int len); 
	void Send_ASDU_Msg(BYTE *msgbuf, int len); 
 
	//	int Mirror_Msg(BYTE *msgbuf, unsigned int len); 
	int Send_StartDT(void); 
	int Send_StopDT(void); 
	int Send_TestFR(void); 
	int Interrogate(int asdu, int group); 
	int Decode_I_Msg(BYTE *msgbuf, unsigned int len); 
	bool GetSockConnectStatus(); 
	void SetErrorMode(int mode); 
	void GetLastErrorMsg(char *msgbuf, size_t len); 
	 
	virtual void OnRxAsduMsg(BYTE *msgbuf, unsigned int len){}; 
	virtual void OnTxMsg(BYTE *msgbuf, unsigned int len){}; 
	virtual void OnRxMsg(BYTE *msgbuf, unsigned int len){}; 
	virtual void OnErrorMsg(int code, char *msgbuf){}; 
	virtual BOOL GetIsResponseUTest(){return 0;}; 
	virtual BOOL GetIsResponseIFrame(){return 0;}; 
	virtual BOOL GetIsResponseS(){return 0;}; 
	virtual void onUpdateWnd(){}; 
	virtual void onCloseLink(){}; 
	 
	virtual void OnRxSinglePoint(unsigned int asdu, unsigned long address, 
		bool value, bool valid, bool blocked, FILETIME time){}; 
	virtual void OnRxMeasuredValue(unsigned int asdu, unsigned long address, 
		float value, bool valid, bool blocked, FILETIME time){}; 
}; 
 
#endif // !defined(AFX_IEC104MASTER_H__2758F4F2_1D00_4035_AF0A_378DE67D3DC3__INCLUDED_)