From 611471f319d13a13602cae65f2b2bc2afd5b1dbf Mon Sep 17 00:00:00 2001
From: darker <mr.darker@163.com>
Date: 星期五, 14 二月 2025 17:49:19 +0800
Subject: [PATCH] 1. 修复读写字、读写双字和扩展读写数据函数

---
 SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp |   68 +++++++++++++++++++++-------------
 1 files changed, 42 insertions(+), 26 deletions(-)

diff --git a/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp b/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp
index 1f6deab..23c31e8 100644
--- a/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp
+++ b/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp
@@ -219,7 +219,8 @@
     if (nRet == 0) {
         m_bConnected.store(true);
         m_enBoardType = enBoardType;
-    } else {
+    }
+    else {
         UpdateLastError(nRet);
         LOG_ERROR(m_strLastError);
     }
@@ -270,7 +271,7 @@
     }
 
     // 鑾峰彇鐗堟湰淇℃伅
-    short buf[32] = {0};
+    short buf[32] = { 0 };
     const short nRet = mdBdVerRead(m_nPath, buf);
     if (nRet != 0) {
         UpdateLastError(nRet);
@@ -428,7 +429,7 @@
         return ERROR_CODE_NOT_CONNECTED;
     }
 
-    short buf[6] = {0};
+    short buf[6] = { 0 };
     const short nRet = mdBdSwRead(m_nPath, buf);
     if (nRet != 0) {
         UpdateLastError(nRet);
@@ -451,12 +452,13 @@
 
     // 鍒濆鍖栬鍙栫紦鍐插尯
     vecData.clear();
-    vecData.resize(nSize);
+    vecData.resize(nSize, 0);
 
     // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
     {
         std::lock_guard<std::mutex> lock(m_mtx);
         short* pData = vecData.data();
+        nSize *= sizeof(short);
         nRet = mdReceive(m_nPath, CombineStation(station), nDevType, nDevNo, &nSize, pData);
     }
 
@@ -556,6 +558,7 @@
     // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
     {
         std::lock_guard<std::mutex> lock(m_mtx);
+        nSize *= sizeof(short);
         nRet = mdSend(m_nPath, CombineStation(station), nDevType, nDevNo, &nSize, pData);
     }
 
@@ -599,7 +602,7 @@
 
     // 璁$畻闇�瑕佸啓鍏ョ殑瀛楄妭鏁帮紙姣忎釜瀛楀崰 2 瀛楄妭锛�
     const short nDevType = CalculateDeviceType(station, enDevType);
-    const auto nSize = static_cast<short>(vecData.size() * sizeof(uint16_t));
+    const auto nSize = static_cast<short>(vecData.size());
     const auto pData = const_cast<short*>(reinterpret_cast<const short*>(vecData.data()));
 
     return WriteData(station, nDevType, nDevNo, nSize, pData);
@@ -616,8 +619,8 @@
 
     // 璁$畻闇�瑕佸啓鍏ョ殑瀛楄妭鏁帮紙姣忎釜鍙屽瓧鍗� 4 瀛楄妭锛�
     const short nDevType = CalculateDeviceType(station, enDevType);
-    const auto nSize = static_cast<short>(vecData.size() * sizeof(uint32_t));
-    std::vector<short> vecBuffer(vecData.size() * 2, 0);
+    const auto nSize = static_cast<short>(vecData.size() * sizeof(short));
+    std::vector<short> vecBuffer(nSize, 0);
     {
         std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
         ConvertUint32ToShort(vecData, vecBuffer);
@@ -640,20 +643,21 @@
         return ERROR_CODE_INVALID_PARAM;
     }
 
-    vecData.resize(nSize);
-    long nActualSize = (nSize + 1) / 2;
-    std::vector<short> vecBuffer(nActualSize, 0);
+    nSize = nSize % 2 != 0 ? nSize + 1 : nSize;
+    std::vector<short> vecBuffer(nSize / 2, 0);
 
     {
         std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
-        nRet = mdReceiveEx(m_nPath, station.nNetNo, station.nStNo, nDevType, nDevNo, &nActualSize, vecBuffer.data());
+        nRet = mdReceiveEx(m_nPath, station.nNetNo, station.nStNo, nDevType, nDevNo, &nSize, vecBuffer.data());
     }
 
     if (nRet != 0) {
         UpdateLastError(nRet);
         LOG_ERROR(m_strLastError);
-    } else {
+    }
+    else {
         std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
+        vecData.resize(nSize);
         ConvertShortToChar(vecBuffer, vecData);
     }
 
@@ -671,7 +675,8 @@
 
     // 灏� vecData 杞崲涓� short 绫诲瀷鐨勭紦鍐插尯
     long nSize = static_cast<long>(vecData.size());
-    std::vector<short> vecBuffer((nSize + 1) / 2, 0);
+    nSize = nSize % 2 != 0 ? nSize + 1 : nSize;
+    std::vector<short> vecBuffer(nSize / 2, 0);
 
     {
         std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
@@ -797,7 +802,8 @@
     if (nRet != 0) {
         UpdateLastError(nRet); // 鏇存柊閿欒鐮�
         LOG_ERROR(m_strLastError);
-    } else {
+    }
+    else {
         std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
         ConvertShortToChar(vecBuffer, vecData);
     }
@@ -854,7 +860,8 @@
     if (nRet != 0) {
         UpdateLastError(nRet);
         LOG_ERROR(m_strLastError);
-    } else {
+    }
+    else {
         std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
         ConvertShortToChar(vecBuffer, vecData);
     }
@@ -1022,7 +1029,7 @@
     }
 
     // 绗� 0 涓厓绱犲瓨鍌ㄦ暟閲忥紝鏈�澶ф敮鎸� 64 涓簨浠�
-    std::array<short, 65> eventno = {0};
+    std::array<short, 65> eventno = { 0 };
     eventno[0] = static_cast<short>(vecEventNumbers.size());
     std::copy(vecEventNumbers.begin(), vecEventNumbers.end(), eventno.begin() + 1);
 
@@ -1051,7 +1058,8 @@
     if (it != m_mapError.end()) {
         // 濡傛灉鎵惧埌锛岀洿鎺ヨ繑鍥炲搴旇瑷�鐨勯敊璇俊鎭�
         m_strLastError = it->second;
-    } else {
+    }
+    else {
         // 濡傛灉鏈壘鍒帮紝澶勭悊鐗规畩鑼冨洿
         m_strLastError = "Unknown error.";
         if (nCode == -28611 || nCode == -28612) {
@@ -1179,13 +1187,17 @@
 BoardType CPerformanceMelsec::FindBoardTypeByChannel(const int nChannel) {
     if (nChannel >= MELSECNET_CHANNEL(1) && nChannel <= MELSECNET_CHANNEL(4)) {
         return BoardType::MELSECNET_H;
-    } else if (nChannel >= CC_LINK_CHANNEL(1) && nChannel <= CC_LINK_CHANNEL(4)) {
+    }
+    else if (nChannel >= CC_LINK_CHANNEL(1) && nChannel <= CC_LINK_CHANNEL(4)) {
         return BoardType::CC_LINK_VER_2;
-    } else if (nChannel >= CC_LINK_IE_CONTROL_CHANNEL(1) && nChannel <= CC_LINK_IE_CONTROL_CHANNEL(4)) {
+    }
+    else if (nChannel >= CC_LINK_IE_CONTROL_CHANNEL(1) && nChannel <= CC_LINK_IE_CONTROL_CHANNEL(4)) {
         return BoardType::CC_LINK_IE_CONTROL;
-    } else if (nChannel >= CC_LINK_IE_FIELD_CHANNEL(1) && nChannel <= CC_LINK_IE_FIELD_CHANNEL(4)) {
+    }
+    else if (nChannel >= CC_LINK_IE_FIELD_CHANNEL(1) && nChannel <= CC_LINK_IE_FIELD_CHANNEL(4)) {
         return BoardType::CC_LINK_IE_FIELD;
-    } else if (nChannel >= CC_LINK_IE_TSN_CHANNEL(1) && nChannel <= CC_LINK_IE_TSN_CHANNEL(4)) {
+    }
+    else if (nChannel >= CC_LINK_IE_TSN_CHANNEL(1) && nChannel <= CC_LINK_IE_TSN_CHANNEL(4)) {
         return BoardType::CC_LINK_IE_TSN;
     }
     return BoardType::UNKNOWN;
@@ -1206,10 +1218,12 @@
         enDevType == DeviceType::LSB || enDevType == DeviceType::LSW) {
         // 缃戠粶鍙峰姞鍋忕Щ
         nDevType += station.nNetNo;
-    } else if (enDevType == DeviceType::ER) {
+    }
+    else if (enDevType == DeviceType::ER) {
         // 鏂囦欢瀵勫瓨鍣ㄧ殑鍧楀彿鍔犲亸绉�
         nDevType += 0;
-    } else if (enDevType == DeviceType::SPG) {
+    }
+    else if (enDevType == DeviceType::SPG) {
         // 璧峰 I/O No. 梅 16 鐨勫��
         nDevType += 0 / 16;
     }
@@ -1223,7 +1237,8 @@
     for (size_t i = 0; i < vecChar.size(); i++) {
         if (i % 2 == 0) {
             vecShort[i / 2] = static_cast<unsigned char>(vecChar[i]);       // 浣庡瓧鑺�
-        } else {
+        }
+        else {
             vecShort[i / 2] |= static_cast<unsigned char>(vecChar[i]) << 8; // 楂樺瓧鑺�
         }
     }
@@ -1244,7 +1259,8 @@
     for (size_t i = 0; i < vecUint8.size(); i++) {
         if (i % 2 == 0) {
             vecShort[i / 2] = static_cast<short>(vecUint8[i]);          // 浣庡瓧鑺�
-        } else {
+        }
+        else {
             vecShort[i / 2] |= static_cast<short>(vecUint8[i] << 8);    // 楂樺瓧鑺�
         }
     }
@@ -1273,7 +1289,7 @@
     vecUint32.resize((vecShort.size() + 1) / 2, 0); // 姣忎袱涓� short 鍚堝苟涓轰竴涓� uint32_t
     for (size_t i = 0; i < vecUint32.size(); i++) {
         vecUint32[i] = (static_cast<uint32_t>(static_cast<uint16_t>(vecShort[i * 2 + 1])) << 16) | // 楂�16浣�
-                       static_cast<uint32_t>(static_cast<uint16_t>(vecShort[i * 2]));              // 浣�16浣�
+            static_cast<uint32_t>(static_cast<uint16_t>(vecShort[i * 2]));              // 浣�16浣�
     }
 }
 

--
Gitblit v1.9.3