mrDarker
2025-08-04 a3943d169515d841e70c57703cbd9fcbf5409a5a
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
#pragma once
 
#include "CCLinkIEControl.h"
 
#include <thread>
#include <atomic>
#include <functional>
#include <vector>
 
using OutValuesArray = std::array<double, 4>;
using Callback = std::function<void()>;
using AnalyzeCallback = std::function<OutValuesArray()>;
using LogCallback = std::function<void(const CString& strContent, int type)>;
 
class CPLCSignalListener
{
public:
    CPLCSignalListener();
    ~CPLCSignalListener();
 
    bool Initialize(StationIdentifier station, int nIntervalMs = 200);
 
    void SetStartCallback(Callback cb);
    void SetStopCallback(Callback cb);
    void SetAnalyzeCallback(AnalyzeCallback cb);
    void SetLogCallback(LogCallback cb);
 
    bool Start();
    void Stop();
 
    bool WriteOutValues(const OutValuesArray& values);
 
private:
    void PulseBitDevice(DeviceType eDevType, short nBitNo, int nDelayMs = 50);
    void HandleAckLife(int i, bool bCurrTriggerBit);
    void ThreadProc();
 
private:
    std::unique_ptr<CCCLinkIEControl> m_pPlc;
    StationIdentifier m_station;
    int m_nIntervalMs = 200;
 
    std::atomic<bool> m_bConnected{ false };
    std::atomic<bool> m_bRunning{ false };
    std::thread m_thread;
    std::vector<bool> m_vecPrevBits;
 
    std::array<bool, 2> m_vecAckSent = { false, false }; // ÊÇ·ñÒÑ·¢ËÍ M10/M11
    std::array<int, 2> m_vecAckCounter = { 0, 0 };       // ¼ÆÊýÆ÷£¬³¬Ê±×Ô¶¯Çå³ý
 
    Callback m_cbStart;
    Callback m_cbStop;
    AnalyzeCallback m_cbAnalyze;
    LogCallback m_cbLog;
};