2024-03-19 17:45:12 +08:00
|
|
|
|
#pragma once
|
|
|
|
|
#include "../stdafx.h"
|
|
|
|
|
#include "Command.h"
|
|
|
|
|
#include "../external/SocketHandle/SocketHandle.h"
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include "Snap7/s7_client.h"
|
|
|
|
|
#include "../config/bean/CommunicationCfg.h"
|
|
|
|
|
#include "SerialPort.h"
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <list>
|
|
|
|
|
#include "ConnectInterface.h"
|
2024-04-12 15:51:41 +08:00
|
|
|
|
#include "BaseData.h"
|
|
|
|
|
#include <unordered_map>
|
2024-04-16 13:32:05 +08:00
|
|
|
|
#include "../config/bean/AlarmCfg.h"
|
2024-04-12 15:51:41 +08:00
|
|
|
|
|
2024-03-19 17:45:12 +08:00
|
|
|
|
|
|
|
|
|
class BaseStat {
|
|
|
|
|
public:
|
|
|
|
|
BaseStat()
|
|
|
|
|
: isConnected(false)
|
|
|
|
|
, writeTimeoutFlag(0)
|
|
|
|
|
, readTimeoutFlag(0)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
virtual ~BaseStat() {};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
bool isConnected; //通讯连接
|
|
|
|
|
unsigned int writeTimeoutFlag;
|
|
|
|
|
unsigned int readTimeoutFlag;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class BaseClient:public IConnect
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
BaseClient();
|
|
|
|
|
virtual ~BaseClient();
|
|
|
|
|
|
|
|
|
|
virtual void Init() = 0;
|
|
|
|
|
virtual void Startup();
|
|
|
|
|
virtual void Shutdown();
|
|
|
|
|
virtual void CtrlStart() { Startup(); };
|
|
|
|
|
virtual void CtrlStop() { Shutdown(); };
|
|
|
|
|
|
|
|
|
|
void SetRunFlag(bool runflag);
|
|
|
|
|
virtual bool IsServerConnected() = 0;
|
|
|
|
|
|
|
|
|
|
virtual bool IsComConnected() { return m_BaseStat.isConnected; }
|
|
|
|
|
|
|
|
|
|
void ClearCycleCommand();
|
2024-04-16 13:32:05 +08:00
|
|
|
|
void SendToClients(WRITETYPE type,const string& addKey = "");
|
2024-03-19 17:45:12 +08:00
|
|
|
|
protected:
|
|
|
|
|
virtual void InitCommand() = 0;
|
2024-05-22 15:58:54 +08:00
|
|
|
|
virtual unsigned int GetInterval() { return 500; }
|
2024-03-19 17:45:12 +08:00
|
|
|
|
static DWORD WINAPI ThreadProc(BaseClient* pclient);
|
|
|
|
|
virtual void RunProc()=0;
|
|
|
|
|
Command* GetSendCycleCommand();
|
|
|
|
|
void UpdateCycleCommands();
|
|
|
|
|
|
|
|
|
|
virtual void ServerDisconnectProc() {};
|
|
|
|
|
virtual void ServerConnectedProc() {};
|
|
|
|
|
virtual void WriteSuccessProc(int wlength, unsigned char* buffer, Command* pcommand) {};
|
|
|
|
|
virtual void ReadSuccessProc(int rlength, unsigned char* buffer, Command* pcommand) {};
|
|
|
|
|
virtual void WriteTimeoutProc(Command* pcommand) {};
|
|
|
|
|
virtual void ReadTimeoutProc(Command* pcommand) {};
|
|
|
|
|
virtual void VerifyFaild(Command* pcommand) {};
|
|
|
|
|
|
|
|
|
|
virtual void CycleBegin() {};
|
|
|
|
|
|
2024-04-16 13:32:05 +08:00
|
|
|
|
void InsertMp(void* startPtr, size_t count,const string& suff = "");
|
2024-03-19 17:45:12 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
HANDLE m_Thread;
|
|
|
|
|
queue<Command*> m_RTCommands;
|
|
|
|
|
vector<Command*> m_CycleCommands;
|
|
|
|
|
CRITICAL_SECTION m_ValueCS;
|
|
|
|
|
CRITICAL_SECTION m_RtcCS;
|
|
|
|
|
bool m_RunFlag;
|
|
|
|
|
//unsigned int m_interval;
|
|
|
|
|
//CSocketHandle m_Client;
|
|
|
|
|
uint64_t m_LastCycleTickcount;
|
|
|
|
|
BaseStat m_BaseStat;
|
2024-04-12 15:51:41 +08:00
|
|
|
|
|
2024-04-16 13:32:05 +08:00
|
|
|
|
unordered_map<std::string, BaseData*> m_baseMp; //大部分使用这个
|
2024-04-16 17:36:27 +08:00
|
|
|
|
unordered_map<std::string, AlarmCfg*> m_alarmCfgMp;
|
2024-03-19 17:45:12 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class S7Client :public BaseClient
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
S7Client(CommunicationCfg* pconfig);
|
|
|
|
|
virtual ~S7Client();
|
|
|
|
|
virtual void Init();
|
|
|
|
|
virtual bool IsServerConnected() {
|
|
|
|
|
return m_S7Client->Connected;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
virtual bool IsSignalConnected() {
|
|
|
|
|
bool rel = false;
|
|
|
|
|
EnterCriticalSection(&m_ValueCS);
|
|
|
|
|
rel = m_BaseStat.isConnected;
|
|
|
|
|
LeaveCriticalSection(&m_ValueCS);
|
|
|
|
|
return rel;
|
|
|
|
|
}
|
|
|
|
|
CommunicationCfg* GetConfig() { return m_Config; }
|
|
|
|
|
void Shutdown();
|
|
|
|
|
//virtual void Startup();
|
|
|
|
|
private:
|
|
|
|
|
virtual void InitCommand() {}
|
|
|
|
|
virtual void RunProc();
|
2024-06-04 14:08:12 +08:00
|
|
|
|
unsigned int GetInterval() { return m_Config->m_Interval->GetValue(); }
|
2024-03-19 17:45:12 +08:00
|
|
|
|
protected:
|
|
|
|
|
TSnap7Client *m_S7Client;
|
|
|
|
|
CommunicationCfg* m_Config;
|
|
|
|
|
int m_WriteTimeout;
|
|
|
|
|
int m_ReadTimeout;
|
|
|
|
|
word m_LocalTsap;
|
|
|
|
|
word m_RemoteTsap;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ComClient :public BaseClient
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
ComClient(CommunicationCfg* cfg);
|
|
|
|
|
virtual ~ComClient();
|
|
|
|
|
virtual void Init();
|
|
|
|
|
virtual bool IsServerConnected() { return (m_ComClient.IsOpen() == TRUE ? true : false); }
|
|
|
|
|
CommunicationCfg* GetConfig() { return m_Config; }
|
|
|
|
|
|
|
|
|
|
void Shutdown();
|
|
|
|
|
private:
|
|
|
|
|
virtual void InitCommand() {}
|
|
|
|
|
virtual void InitSerialPort();
|
|
|
|
|
virtual void RunProc();
|
2024-06-04 14:08:12 +08:00
|
|
|
|
unsigned int GetInterval() { return m_Config->m_Interval->GetValue(); }
|
2024-03-19 17:45:12 +08:00
|
|
|
|
virtual void ServerDisconnectProc();
|
|
|
|
|
void WriteSuccessProc(int wlength, unsigned char* buffer, Command* pcommand);
|
|
|
|
|
void WriteTimeoutProc(Command* pcommand);
|
|
|
|
|
void ReadTimeoutProc(Command* pcommand);
|
|
|
|
|
void ReadSuccessProc(int rlength, unsigned char* buffer, Command* pcommand);
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
CSerialPort2 m_ComClient;
|
|
|
|
|
CommunicationCfg* m_Config;
|
|
|
|
|
int m_Freq;
|
|
|
|
|
int m_ReadTimeout;
|
|
|
|
|
int m_WriteTimeout;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class TcpClient :public BaseClient
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
TcpClient(CommunicationCfg* pconfig, int freq = 50);
|
|
|
|
|
virtual ~TcpClient();
|
|
|
|
|
|
|
|
|
|
virtual void Init();
|
|
|
|
|
virtual bool IsServerConnected() { return (m_Client.IsOpen() == TRUE ? true : false); }
|
|
|
|
|
CommunicationCfg* GetConfig() { return m_Config; }
|
|
|
|
|
void Shutdown();
|
|
|
|
|
private:
|
|
|
|
|
virtual void InitCommand() {}
|
|
|
|
|
virtual void RunProc();
|
|
|
|
|
|
|
|
|
|
virtual void ServerDisconnectProc();
|
|
|
|
|
virtual void WriteSuccessProc(int wlength, unsigned char* buffer, Command* pcommand);
|
|
|
|
|
virtual void WriteTimeoutProc(Command* pcommand);
|
|
|
|
|
virtual void ReadTimeoutProc(Command* pcommand);
|
|
|
|
|
virtual void ReadSuccessProc(int rlength, unsigned char* buffer, Command* pcommand);
|
2024-06-04 14:08:12 +08:00
|
|
|
|
unsigned int GetInterval() { return m_Config->m_Interval->GetValue(); }
|
2024-04-12 15:51:41 +08:00
|
|
|
|
|
2024-03-19 17:45:12 +08:00
|
|
|
|
protected:
|
|
|
|
|
CSocketHandle m_Client;
|
|
|
|
|
CommunicationCfg* m_Config;
|
|
|
|
|
int m_Freq;
|
|
|
|
|
int m_ReadTimeout;
|
|
|
|
|
int m_WriteTimeout;
|
2024-04-12 15:51:41 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-19 17:45:12 +08:00
|
|
|
|
};
|