1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#pragma once
#include <string>
#include "AlarmMonitor.h"
#include "MMSystem.h"
#pragma comment(lib,"winmm")
 
 
#define ALARM_MONITOR        _T("¾¯¸æÐÅÏ¢")
 
typedef std::function<void(void* pFrom, int)> ONPLCSTATECHANGED;
typedef std::function<void(void* pFrom, int)> ONPLCMONITORDATA;
typedef std::function<void(void* pFrom, CAlarm*, int)> ONALARM;
typedef struct _PLCListener
{
    ONPLCSTATECHANGED        onStateChanged;
    ONPLCMONITORDATA        onMonitorData;
    ONALARM                    onAlarm;
} PLCListener;
 
typedef struct _MONITOR
{
    int id;
    int beginAddr;
    int readLen;
    MC::SOFT_COMPONENT softComponent;
    char* szRecvBuffer;
    HANDLE hEvent;
    ULONGLONG readCount;
} MONITOR;
 
enum class PLCSTATE
{
    READY = 0,
    CONNECTING,
    CONNECTED,
    DISCONNECTED
};
 
class CPLC
{
public:
    CPLC();
    CPLC(const char* pszName, const char* pszIp, const unsigned int port);
    ~CPLC();
 
public:
    void setListener(PLCListener& listener);
    void setWorkDir(const char* pszDir);
    void init();
    void term();
    void sendBroadcast(CComponent* pSender, CIntent* pIntent);
    void onRecvBroadcast(CComponent* pSender, CIntent* pIntent);
    bool isConnected();
    void connect();
    void setActionInterval(unsigned int nInterval);
    std::string& getName();
    std::string& getIp();
    unsigned int getPort();
    bool isMute();
    void addComponent(CComponent* pComponent);
    CComponent* getComponent(const char* pszName);
    CAlarmMonitor* getAlarmMonitor();
    unsigned onMonitor();
    void OnTimer(UINT nTimerid);
    int addMonitor(int id, int beginAddr, int endAddr, MC::SOFT_COMPONENT softComponent, char* pszRecvBuffer);
    int readWord(MC::SOFT_COMPONENT softComponent, unsigned int addr, ONREAD funOnRead);
    int readData(MC::SOFT_COMPONENT softComponent, unsigned int addr, unsigned int nReadLen, ONREADDATA funOnReadData);
    int writeWord(MC::SOFT_COMPONENT softComponent, unsigned int addr, int value, ONWRITE funOnWrite);
    int writeDWord(MC::SOFT_COMPONENT softComponent, unsigned int addr, int value, ONWRITE funOnWrite);
    int writeBit(MC::SOFT_COMPONENT softComponent, unsigned int addr, BOOL bValue, ONWRITE funOnWrite);
    int writeData(MC::SOFT_COMPONENT softComponent, unsigned int addr, const char* pszData, unsigned int length, ONWRITE funOnWrite);
    void readPLCDataRegularly();
 
public:
    int getVelocityRatio();
    double getTackTime();
    int getDayShiftCapacity();
    int getNightShiftCapacity();
 
public:
    HANDLE m_hTimeEvent;
 
private:
    inline void Lock() { EnterCriticalSection(&m_criticalSection); }
    inline void Unlock() { LeaveCriticalSection(&m_criticalSection); }
    void setState(PLCSTATE state);
    void monitorReadData(MONITOR& monitor);
    CString& dataToHexString(const char* pData, const int size, CString& strOut);
    int onMonitorData(MONITOR& monitor);
 
private:
    std::string m_strWorkDir;
    std::string m_strName;
    std::string m_strIp;
    unsigned int m_nPort;
    IMcChannel* m_pChannel;
    PLCSTATE m_state;
    PLCListener m_listener;
    unsigned int m_nActionInterval;
    unsigned int m_nUnHeartBeat;
    std::vector<CComponent*> m_components;
    char* m_pPlcData;
 
private:
    CRITICAL_SECTION m_criticalSection;
    std::vector<MONITOR> m_monitors;
    HANDLE m_hMcMonitorStop;
    HANDLE m_hMcMonitorThreadHandle;
    unsigned m_mcMonitorThrdaddr;
    MMRESULT m_nTimerId;
    bool m_bMute;
 
private:
    int m_nVelocityRatio;        // ËٶȱÈ
    double m_dTactTime;            // ÖÜÆÚʱ¼ä
    int m_nDayShiftCapacity;    // °×°à²úÄÜ
    int m_nNightShiftCapacity;    // Ò¹°à²úÄÜ
    bool m_bBlBtnsStates[7];    // PLC View°´Å¥×´Ì¬
};