LAPTOP-T815PCOQ\25526
2024-12-26 32f4e5154a4bcc3228b9e422e96b7009d55f50ad
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// CProjectPageMain.cpp: 实现文件
//
 
#include "stdafx.h"
#include "BondEq.h"
#include "afxdialogex.h"
#include "CProjectPageMain.h"
#include "ToolUnits.h"
 
#define TIMER_INIT                    1
#define TIMER_READ_PLC_DATA            2
 
#define ID_MSG_UPDATA_DATA_TO_UI    WM_USER + 101
 
// CProjectPageMain 对话框
 
IMPLEMENT_DYNAMIC(CProjectPageMain, CDialogEx)
 
CProjectPageMain::CProjectPageMain(CWnd* pParent /*=nullptr*/)
    : CDialogEx(IDD_PROJECT_PAGE_MAIN, pParent)
{
    m_crBkgnd = PROPAGE_BACKGROUND_COLOR;
    m_hbrBkgnd = nullptr;
    m_pObserver = nullptr;
}
 
CProjectPageMain::~CProjectPageMain()
{
}
 
void CProjectPageMain::SetPLC(CPLC* pPLC)
{
    ASSERT(pPLC);
    m_pPLC = pPLC;
}
 
void CProjectPageMain::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
}
 
void CProjectPageMain::HandleOperation(OperationType eOpType)
{
    WriteOperationDataToPLC(eOpType, true);
    Sleep(200);
    WriteOperationDataToPLC(eOpType, false);
}
 
void CProjectPageMain::WriteOperationDataToPLC(OperationType eOpType, bool bPressed)
{
    MC::SOFT_COMPONENT enComponent = MC::ZR;
    int nStartAddress = 10000;
    int nEndAddress = nStartAddress + 300;
    int nSize = (nEndAddress - nStartAddress + 1) * 2;
 
    // 根据操作类型计算目标地址
    int bitIndex = 0;
    char szWrite[4] = { 0x0, 0x0, 0x0, 0x0 };
    switch (eOpType) {
    case OperationType::RUN:
        nStartAddress += 0;  // RUN 信号地址 0,闪动信息06.0
        szWrite[0] = bPressed ? 0x1 : 0;
        break;
    case OperationType::AUTO:
        nStartAddress += 01; // AUTO 信号地址 c
        szWrite[1] = bPressed ? 0x10 : 0;
        break;
    case OperationType::PUASE:
        nStartAddress += 01; // PUASE 信号地址 d
        szWrite[1] = bPressed ? 0x20 : 0;
        break;
    case OperationType::MANUAL:
        nStartAddress += 05; // MANUAL 信号地址 5.e
        szWrite[1] = bPressed ? 0x40 : 0;
        break;
    case OperationType::MUTE:
        nStartAddress += 01; // MUTE 信号地址 1.a
        szWrite[1] = bPressed ? 0x04 : 0;
        break;
    case OperationType::OPR:
        nStartAddress += 01; // OPR 信号地址 1.a
        szWrite[1] = bPressed ? 0x04 : 0;
        break;
    case OperationType::STOP:
        nStartAddress += 01; // STOP 信号地址 1.a
        szWrite[1] = bPressed ? 0x04 : 0;
        break;
    default:
        AfxMessageBox(_T("未知操作类型!"));
        return;
    }
 
    // 向 PLC 写入信号
    m_pPLC->writeData(enComponent, nStartAddress, szWrite, 2, [eOpType, nStartAddress, bPressed](IMcChannel* pChannel, int nAddr, DWORD nValue, int nFlag) {
        if (nFlag == 0) {
            TRACE("操作成功:类型=%d,地址=%d,值=%d\n", static_cast<int>(eOpType), nAddr, bPressed);
        }
        else {
            TRACE("操作失败:类型=%d,地址=%d,错误码=%d\n", static_cast<int>(eOpType), nAddr, nFlag);
        }
    });
}
 
void CProjectPageMain::ReadPLCDataToUI()
{
    MC::SOFT_COMPONENT enComponent = MC::ZR;
    int nStartAddress = 10000;
    int nEndAddress = nStartAddress + 300;
    int nSize = (nEndAddress - nStartAddress + 1) * 2;
 
    auto funOnReadData = [this, nStartAddress, nSize](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) -> void {
        if (nDataSize == nSize && flag == 0 && ::IsWindow(m_hWnd)) {
            dVelocityRatio = CToolUnits::toInt32(&pData[20 * 2]) * 0.001;
            dTactTime = CToolUnits::toInt32(&pData[82 * 2]) * 0.001;
            nDayShiftCapacity = CToolUnits::toInt16(&pData[84 * 2]) * 0.001;
            nNightShiftCapacity = CToolUnits::toInt16(&pData[50 * 2]) * 0.1;
 
            PostMessage(ID_MSG_UPDATA_DATA_TO_UI);
        }
    };
 
    // m_pPLC->readData(enComponent, nStartAddress, nSize, funOnReadData);
}
 
BEGIN_MESSAGE_MAP(CProjectPageMain, CDialogEx)
    ON_WM_CTLCOLOR()
    ON_WM_DESTROY()
    ON_WM_SIZE()
    ON_WM_TIMER()
    ON_WM_CLOSE()
    ON_BN_CLICKED(IDC_BUTTON_ACTIVATE, &CProjectPageMain::OnBnClickedButtonActivate)
    ON_BN_CLICKED(IDC_BUTTON_AUTO, &CProjectPageMain::OnBnClickedButtonAuto)
    ON_BN_CLICKED(IDC_BUTTON_PUASE, &CProjectPageMain::OnBnClickedButtonPuase)
    ON_BN_CLICKED(IDC_BUTTON_MANUAL_OPERATION, &CProjectPageMain::OnBnClickedButtonManualOperation)
    ON_BN_CLICKED(IDC_BUTTON_SOUND_OFF, &CProjectPageMain::OnBnClickedButtonSoundOff)
    ON_BN_CLICKED(IDC_BUTTON_RESETTING, &CProjectPageMain::OnBnClickedButtonResetting)
    ON_BN_CLICKED(IDC_BUTTON_STOP, &CProjectPageMain::OnBnClickedButtonStop)
    ON_MESSAGE(ID_MSG_UPDATA_DATA_TO_UI, &CProjectPageMain::OnUpdateDataToUI)
END_MESSAGE_MAP()
 
 
// CProjectPageMain 消息处理程序
 
void CProjectPageMain::InitRxWindows()
{
    /* code */
    // 订阅数据
    IRxWindows* pRxWindows = RX_GetRxWindows();
    pRxWindows->enableLog(5);
    if (m_pObserver == NULL) {
        m_pObserver = pRxWindows->allocObserver([&](IAny* pAny) -> void {
            // onNext
            pAny->addRef();
            int code = pAny->getCode();
            //if (true) {
 
            //}
            pAny->release();
        }, [&]() -> void {
            // onComplete
        }, [&](IThrowable* pThrowable) -> void {
            // onErrorm
            pThrowable->printf();
        });
 
        theApp.m_model.getObservable()->observeOn(pRxWindows->mainThread())
            ->subscribe(m_pObserver);
    }
}
 
BOOL CProjectPageMain::OnInitDialog()
{
    CDialogEx::OnInitDialog();
 
    InitRxWindows();
    SetTimer(TIMER_READ_PLC_DATA, 500, nullptr);
    return TRUE;  // return TRUE unless you set the focus to a control
    // 异常: OCX 属性页应返回 FALSE
}
 
HBRUSH CProjectPageMain::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor);
 
    if (nCtlColor == CTLCOLOR_STATIC) {
        pDC->SetBkColor(m_crBkgnd);
    }
 
    if (m_hbrBkgnd == nullptr) {
        m_hbrBkgnd = CreateSolidBrush(m_crBkgnd);
    }
 
    return m_hbrBkgnd;
}
 
void CProjectPageMain::OnDestroy()
{
    CDialogEx::OnDestroy();
 
    if (m_hbrBkgnd != nullptr) {
        ::DeleteObject(m_hbrBkgnd);
    }
 
    ASSERT(m_pObserver != NULL);
    m_pObserver->unsubscribe();
    m_pObserver = NULL;
}
 
void CProjectPageMain::OnSize(UINT nType, int cx, int cy)
{
    CDialogEx::OnSize(nType, cx, cy);
 
    CRect rcClient;
    GetClientRect(&rcClient);
}
 
void CProjectPageMain::OnTimer(UINT_PTR nIDEvent)
{
    if (TIMER_READ_PLC_DATA == nIDEvent) {
        ASSERT(m_pPLC);
 
        if (m_pPLC != nullptr) {
            ReadPLCDataToUI();
        }
    }
 
    CDialogEx::OnTimer(nIDEvent);
}
 
void CProjectPageMain::OnClose()
{
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    KillTimer(TIMER_READ_PLC_DATA);
 
    CDialogEx::OnClose();
}
 
void CProjectPageMain::OnBnClickedButtonActivate()
{
    // TODO: 在此添加控件通知处理程序代码
    HandleOperation(OperationType::RUN);
}
 
void CProjectPageMain::OnBnClickedButtonAuto()
{
    // TODO: 在此添加控件通知处理程序代码
    HandleOperation(OperationType::AUTO);
}
 
void CProjectPageMain::OnBnClickedButtonPuase()
{
    // TODO: 在此添加控件通知处理程序代码
    HandleOperation(OperationType::PUASE);
}
 
void CProjectPageMain::OnBnClickedButtonManualOperation()
{
    // TODO: 在此添加控件通知处理程序代码
    HandleOperation(OperationType::MANUAL);
}
 
void CProjectPageMain::OnBnClickedButtonSoundOff()
{
    // TODO: 在此添加控件通知处理程序代码
    HandleOperation(OperationType::MUTE);
}
 
void CProjectPageMain::OnBnClickedButtonResetting()
{
    // TODO: 在此添加控件通知处理程序代码
    HandleOperation(OperationType::OPR);
}
 
void CProjectPageMain::OnBnClickedButtonStop()
{
    // TODO: 在此添加控件通知处理程序代码
    HandleOperation(OperationType::STOP);
}
 
LRESULT CProjectPageMain::OnUpdateDataToUI(WPARAM wParam, LPARAM lParam)
{
    CString strText;
    strText.Format(_T("%.3f %"), dVelocityRatio);
    GetDlgItem(IDC_EDIT_VELOCITY_RATIO)->SetWindowText(strText);
 
    strText.Format(_T("%.3f"), dTactTime);
    GetDlgItem(IDC_EDIT_TACT_TIME)->SetWindowText(strText);
 
    SetDlgItemInt(IDC_EDIT_DAY_SHIFT_CAPACITY, nDayShiftCapacity);
    SetDlgItemInt(IDC_EDIT_NIGHT_SHIFT_CAPACITY, nNightShiftCapacity);
 
    return 0;
}