From 468a5415022b3fec6c874f4ff690041a5fe2c195 Mon Sep 17 00:00:00 2001
From: LAPTOP-T815PCOQ\25526 <mr.liuyang@126.com>
Date: 星期四, 28 十一月 2024 13:59:38 +0800
Subject: [PATCH] 1.IO模块读写PLC数据
---
SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp | 160 +++++++++++++++++++++++++++++++++++++++++++++++------
1 files changed, 141 insertions(+), 19 deletions(-)
diff --git a/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp b/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp
index 39e90a4..f707501 100644
--- a/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp
+++ b/SourceCode/Bond/BondEq/View/IOMonitoringDlg.cpp
@@ -5,6 +5,7 @@
#include "BondEq.h"
#include "afxdialogex.h"
#include "IOMonitoringDlg.h"
+#include "ToolUnits.h"
#define TIMER_INIT 1
#define TIMER_READ_PLC_DATA 2
@@ -53,6 +54,12 @@
// 璁$畻椤垫暟
m_nCurrentPage = 1;
m_nTotalPages = (m_displayData.size() + m_nRowsPerPage - 1) / m_nRowsPerPage;
+}
+
+void CIOMonitoringDlg::SetPLC(CPLC* pPLC)
+{
+ ASSERT(pPLC);
+ m_pPLC = pPLC;
}
CFont* CIOMonitoringDlg::GetOrCreateFont(int nFontSize)
@@ -185,13 +192,33 @@
CString currentText;
pControl->GetWindowText(currentText);
+ BOOL bOn = FALSE;
if (currentText == _T("OFF")) {
- pControl->SetBkColor(RGB(0, 255, 0)); // 缁胯壊鑳屾櫙
- pControl->SetText(_T("ON")); // 鏇存柊鏂囨湰涓� ON
+ //pControl->SetBkColor(RGB(0, 255, 0)); // 缁胯壊鑳屾櫙
+ //pControl->SetText(_T("ON")); // 鏇存柊鏂囨湰涓� ON
+ bOn = TRUE;
}
else {
- pControl->SetBkColor(RGB(255, 0, 0)); // 绾㈣壊鑳屾櫙
- pControl->SetText(_T("OFF")); // 鏇存柊鏂囨湰涓� OFF
+ //pControl->SetBkColor(RGB(255, 0, 0)); // 绾㈣壊鑳屾櫙
+ //pControl->SetText(_T("OFF")); // 鏇存柊鏂囨湰涓� OFF
+ bOn = FALSE;
+ }
+
+ pControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 4]);
+ pControl->GetWindowText(currentText);
+
+ int nAddress;
+ MC::SOFT_COMPONENT component;
+ if (ParsePLCAddress(currentText, component, nAddress) && m_pPLC) {
+ TRACE("鍦板潃瑙f瀽鎴愬姛: %s\n", currentText);
+ m_pPLC->writeBit(component, nAddress, bOn, [](IMcChannel* pChannel, int addr, DWORD value, int flag) {
+ if (flag == 0) {
+ TRACE("鍐欏叆鎴愬姛: 鍦板潃: %d, 鍊�: %lu\n", addr, value);
+ }
+ else {
+ TRACE("鍐欏叆澶辫触: 鍦板潃: %d, 閿欒鐮�: %d\n", addr, flag);
+ }
+ });
}
});
x += colWidthSmall;
@@ -291,23 +318,118 @@
m_staticControls.clear();
}
-void CIOMonitoringDlg::UpdatePLCStates()
+bool CIOMonitoringDlg::ParsePLCAddress(const CString& address, MC::SOFT_COMPONENT& component, int& addr)
{
- // 闅忔満鍊兼ā鎷�
- for (size_t i = 0; i < m_inputPLCAddresses.size(); ++i) {
- // 妯℃嫙鑾峰彇杈撳叆鐘舵��
- bool inputState = (rand() % 2 == 0); // 鍋跺皵涓� true/false
- auto* inputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 0]);
- inputControl->SetBkColor(inputState ? RGB(0, 255, 0) : RGB(255, 0, 0));
- inputControl->SetText(inputState ? _T("ON") : _T("OFF"));
+ if (address.GetLength() < 2) {
+ return false;
}
- for (size_t i = 0; i < m_outputPLCAddresses.size(); ++i) {
- // 妯℃嫙鑾峰彇杈撳嚭鐘舵��
- bool outputState = (rand() % 2 == 0); // 鍋跺皵涓� true/false
- auto* outputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 3]);
- outputControl->SetBkColor(outputState ? RGB(0, 255, 0) : RGB(255, 0, 0));
- outputControl->SetText(outputState ? _T("ON") : _T("OFF"));
+ // 鎻愬彇缁勪欢绫诲瀷锛堢涓�涓瓧绗︼級
+ TCHAR componentChar = address[0];
+ switch (componentChar) {
+ case 'D':
+ component = MC::SOFT_COMPONENT::D;
+ break;
+ case 'M':
+ component = MC::SOFT_COMPONENT::M;
+ break;
+ case 'X':
+ component = MC::SOFT_COMPONENT::X;
+ break;
+ case 'Y':
+ component = MC::SOFT_COMPONENT::Y;
+ break;
+ default:
+ return false;
+ }
+
+ CString hexAddress = address.Mid(1);
+ addr = _tcstoul(hexAddress, nullptr, 16);
+
+ return true;
+}
+
+void CIOMonitoringDlg::UpdatePLCStates()
+{
+ // 绀轰緥锛氫粠 PLC 鑾峰彇鍊硷紝杩欓噷鐢ㄩ殢鏈哄�兼ā鎷�
+ //for (size_t i = 0; i < m_inputPLCAddresses.size(); ++i) {
+ // // 妯℃嫙鑾峰彇杈撳叆鐘舵��
+ // bool inputState = (rand() % 2 == 0); // 鍋跺皵涓� true/false
+ // auto* inputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 0]);
+ // inputControl->SetBkColor(inputState ? RGB(0, 255, 0) : RGB(255, 0, 0));
+ // inputControl->SetText(inputState ? _T("ON") : _T("OFF"));
+ //}
+
+ //for (size_t i = 0; i < m_outputPLCAddresses.size(); ++i) {
+ // // 妯℃嫙鑾峰彇杈撳嚭鐘舵��
+ // bool outputState = (rand() % 2 == 0); // 鍋跺皵涓� true/false
+ // auto* outputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 3]);
+ // outputControl->SetBkColor(outputState ? RGB(0, 255, 0) : RGB(255, 0, 0));
+ // outputControl->SetText(outputState ? _T("ON") : _T("OFF"));
+ //}
+
+ // 杈撳叆鍦板潃鐨勮鍙�
+ if (!m_inputPLCAddresses.empty()) {
+ // 鑾峰彇璧峰鍦板潃鍜岄暱搴�
+ CString startAddressStr = m_inputPLCAddresses.front();
+ CString endAddressStr = m_inputPLCAddresses.back();
+ MC::SOFT_COMPONENT component;
+ int startAddress, endAddress;
+
+ // 瑙f瀽璧峰鍜岀粨鏉熷湴鍧�
+ if (ParsePLCAddress(startAddressStr, component, startAddress) &&
+ ParsePLCAddress(endAddressStr, component, endAddress)) {
+ int inputSize = endAddress - startAddress + 1;
+
+ // 鍥炶皟澶勭悊杈撳叆鏁版嵁
+ auto funOnReadInput = [this, startAddress](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) {
+ if (nDataSize == (unsigned int)(m_inputPLCAddresses.size()) && flag == 0) {
+ for (size_t i = 0; i < m_inputPLCAddresses.size(); ++i) {
+ int offset = i;
+ int value = CToolUnits::toInt16(&pData[offset]);
+
+ auto* inputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 0]); // 绗� 0 鍒�
+ inputControl->SetBkColor(value ? RGB(0, 255, 0) : RGB(255, 0, 0)); // 鏇存柊鑳屾櫙棰滆壊
+ inputControl->SetText(value ? _T("ON") : _T("OFF")); // 鏇存柊鏂囨湰
+ }
+ }
+ };
+
+ // 璇诲彇杈撳叆鏁版嵁
+ m_pPLC->readData(component, startAddress, inputSize, funOnReadInput);
+ }
+ }
+
+ // 杈撳嚭鍦板潃鐨勮鍙�
+ if (!m_outputPLCAddresses.empty()) {
+ // 鑾峰彇璧峰鍦板潃鍜岄暱搴�
+ CString startAddressStr = m_outputPLCAddresses.front();
+ CString endAddressStr = m_outputPLCAddresses.back();
+ MC::SOFT_COMPONENT component;
+ int startAddress, endAddress;
+
+ // 瑙f瀽璧峰鍜岀粨鏉熷湴鍧�
+ if (ParsePLCAddress(startAddressStr, component, startAddress) &&
+ ParsePLCAddress(endAddressStr, component, endAddress)) {
+ int outputSize = endAddress - startAddress + 1;
+
+ // 鍥炶皟澶勭悊杈撳嚭鏁版嵁
+ auto funOnReadOutput = [this, startAddress](IMcChannel* pChannel, int addr, char* pData, unsigned int nDataSize, int flag) {
+ if (nDataSize == (unsigned int)(m_outputPLCAddresses.size()) && flag == 0) {
+ for (size_t i = 0; i < m_outputPLCAddresses.size(); ++i) {
+ int offset = i;
+ int value = CToolUnits::toInt16(&pData[offset]);
+
+ auto* outputControl = static_cast<CBLLabel*>(m_staticControls[i * m_nCols + 3]); // 绗� 3 鍒�
+ outputControl->SetBkColor(value ? RGB(0, 255, 0) : RGB(255, 0, 0)); // 鏇存柊鑳屾櫙棰滆壊
+ outputControl->SetText(value ? _T("ON") : _T("OFF")); // 鏇存柊鏂囨湰
+ }
+ }
+ };
+
+ // 璇诲彇杈撳嚭鏁版嵁
+ m_pPLC->readData(component, startAddress, outputSize, funOnReadOutput);
+ }
}
}
@@ -431,7 +553,7 @@
{
// TODO: 鍦ㄦ娣诲姞娑堟伅澶勭悊绋嬪簭浠g爜鍜�/鎴栬皟鐢ㄩ粯璁ゅ��
if (TIMER_READ_PLC_DATA == nIDEvent) {
- //ASSERT(m_pPLC);
+ ASSERT(m_pPLC);
UpdatePLCStates();
Sleep(100);
}
--
Gitblit v1.9.3