From 3ba7f1dbf602fb376c4cd158a8a416a4910902fe Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期六, 15 二月 2025 16:58:40 +0800
Subject: [PATCH] Merge branch 'liuyang'
---
SourceCode/Bond/Servo/ServoGraph.cpp | 19 +
SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp | 503 +++++++++++++++++++++++++------------------------
SourceCode/Bond/Servo/ServoGraph.h | 2
SourceCode/Bond/Servo/ServoDlg.cpp | 32 +++
SourceCode/Bond/Servo/ServoDlg.h | 5
5 files changed, 317 insertions(+), 244 deletions(-)
diff --git a/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp b/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp
index 8807138..23c31e8 100644
--- a/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp
+++ b/SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp
@@ -23,9 +23,9 @@
#define LOG_DEBUG(msg)
#endif
-// 初始化静态成员变量
+// 鍒濆鍖栭潤鎬佹垚鍛樺彉閲�
std::unordered_map<int, std::string> CPerformanceMelsec::m_mapError = {
- // 板块SDK错误码
+ // 鏉垮潡SDK閿欒鐮�
{0, "No error, communication successful."},
{1, "Driver not started. The driver is not running."},
{2, "Timeout error (board response error). Request not completed within timeout."},
@@ -118,7 +118,7 @@
{-28634, "Hardware self-diagnosis error."},
{-28636, "Hardware self-diagnosis error."},
- // 自定义错误码
+ // 鑷畾涔夐敊璇爜
{ERROR_CODE_UNKNOWN, "Error: Unknown error code."},
{ERROR_CODE_NOT_CONNECTED, "Error: Not connected to the device."},
{ERROR_CODE_INVALID_PARAM, "Error: Invalid parameter."},
@@ -137,26 +137,26 @@
m_bConnected.store(false);
}
-// 析构函数
+// 鏋愭瀯鍑芥暟
CPerformanceMelsec::~CPerformanceMelsec() {
Disconnect();
}
-// 获取最近的错误信息
+// 鑾峰彇鏈�杩戠殑閿欒淇℃伅
std::string CPerformanceMelsec::GetLastError() const {
return m_strLastError;
}
-// 保存错误信息
+// 淇濆瓨閿欒淇℃伅
bool CPerformanceMelsec::SaveErrorInfoToFile(const std::string& filename) {
- // 打开文件
+ // 鎵撳紑鏂囦欢
std::ofstream file(filename);
if (!file.is_open()) {
std::cerr << "Failed to open file for saving: " << filename << std::endl;
return false;
}
- // 遍历静态成员变量 m_mapError 并将每个错误信息写入文件
+ // 閬嶅巻闈欐�佹垚鍛樺彉閲� m_mapError 骞跺皢姣忎釜閿欒淇℃伅鍐欏叆鏂囦欢
for (const auto& entry : m_mapError) {
const int nCode = entry.first;
const std::string& strMessage = entry.second;
@@ -167,7 +167,7 @@
return true;
}
-// 加载错误信息
+// 鍔犺浇閿欒淇℃伅
bool CPerformanceMelsec::LoadErrorInfoFromFile(const std::string& filename) {
std::ifstream inFile(filename);
if (!inFile.is_open()) {
@@ -183,7 +183,7 @@
std::string strToken;
std::string strMessage;
- // 使用分隔符 "|" 解析每一行
+ // 浣跨敤鍒嗛殧绗� "|" 瑙f瀽姣忎竴琛�
if (std::getline(iss, strToken, '|')) {
nCode = std::stoi(strToken);
}
@@ -200,7 +200,7 @@
return true;
}
-// 连接到PLC
+// 杩炴帴鍒癙LC
int CPerformanceMelsec::Connect(const short nChannel, const short nMode) {
std::lock_guard<std::mutex> lock(m_mtx);
@@ -214,12 +214,13 @@
return ERROR_CODE_INVALID_PARAM;
}
- // 连接PLC,显式类型转换以匹配 mdOpen 的签名
+ // 杩炴帴PLC锛屾樉寮忕被鍨嬭浆鎹互鍖归厤 mdOpen 鐨勭鍚�
const short nRet = mdOpen(nChannel, nMode, &m_nPath);
if (nRet == 0) {
m_bConnected.store(true);
m_enBoardType = enBoardType;
- } else {
+ }
+ else {
UpdateLastError(nRet);
LOG_ERROR(m_strLastError);
}
@@ -227,7 +228,7 @@
return nRet;
}
-// 断开连接
+// 鏂紑杩炴帴
int CPerformanceMelsec::Disconnect() {
std::lock_guard<std::mutex> lock(m_mtx);
@@ -244,7 +245,7 @@
return nRet;
}
-// 可编程控制器软元件信息表的初始化
+// 鍙紪绋嬫帶鍒跺櫒杞厓浠朵俊鎭〃鐨勫垵濮嬪寲
int CPerformanceMelsec::InitializeController() {
std::lock_guard<std::mutex> lock(m_mtx);
@@ -262,15 +263,15 @@
return nRet;
}
-// 获取版本信息
+// 鑾峰彇鐗堟湰淇℃伅
int CPerformanceMelsec::GetBoardVersion(BoardVersion& version) {
if (!m_bConnected.load()) {
UpdateLastError(ERROR_CODE_NOT_CONNECTED);
return ERROR_CODE_NOT_CONNECTED;
}
- // 获取版本信息
- short buf[32] = {0};
+ // 鑾峰彇鐗堟湰淇℃伅
+ short buf[32] = { 0 };
const short nRet = mdBdVerRead(m_nPath, buf);
if (nRet != 0) {
UpdateLastError(nRet);
@@ -278,7 +279,7 @@
return nRet;
}
- // 填充版本信息到结构体
+ // 濉厖鐗堟湰淇℃伅鍒扮粨鏋勪綋
version.fixedValue[0] = static_cast<char>(buf[0] & 0xFF);
version.fixedValue[1] = static_cast<char>((buf[0] >> 8) & 0xFF);
@@ -309,16 +310,16 @@
return nRet;
}
-// 读取目标站点CPU类型
+// 璇诲彇鐩爣绔欑偣CPU绫诲瀷
int CPerformanceMelsec::ReadCPUCode(const StationIdentifier& station, short& nCPUCode) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 确保线程安全的最小锁定范围
+ // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
{
nCPUCode = 0;
std::lock_guard<std::mutex> lock(m_mtx);
@@ -333,15 +334,15 @@
return nRet;
}
-// 板模式设置
+// 鏉挎ā寮忚缃�
int CPerformanceMelsec::SetBoardMode(const short nMode) {
- // 检查是否已经连接
+ // 妫�鏌ユ槸鍚﹀凡缁忚繛鎺�
if (!m_bConnected.load()) {
UpdateLastError(ERROR_CODE_NOT_CONNECTED);
return ERROR_CODE_NOT_CONNECTED;
}
- // 确保线程安全的最小锁定范围
+ // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
short nRet = 0;
{
std::lock_guard<std::mutex> lock(m_mtx);
@@ -356,9 +357,9 @@
return nRet;
}
-// 获取板模式
+// 鑾峰彇鏉挎ā寮�
int CPerformanceMelsec::GetBoardMode(short& nMode) {
- // 检查是否已经连接
+ // 妫�鏌ユ槸鍚﹀凡缁忚繛鎺�
if (!m_bConnected.load()) {
UpdateLastError(ERROR_CODE_NOT_CONNECTED);
return ERROR_CODE_NOT_CONNECTED;
@@ -380,7 +381,7 @@
return 0;
}
-// 板复位
+// 鏉垮浣�
int CPerformanceMelsec::BoardReset() {
std::lock_guard<std::mutex> lock(m_mtx);
if (!m_bConnected.load()) {
@@ -397,7 +398,7 @@
return nRet;
}
-// 板LED读取
+// 鏉縇ED璇诲彇
int CPerformanceMelsec::ReadBoardLed(std::vector<short>& vecLedBuffer) {
std::lock_guard<std::mutex> lock(m_mtx);
if (!m_bConnected.load()) {
@@ -405,11 +406,11 @@
return ERROR_CODE_NOT_CONNECTED;
}
- // 清空 LED 缓冲区
+ // 娓呯┖ LED 缂撳啿鍖�
vecLedBuffer.clear();
vecLedBuffer.resize(16, 0);
- // 调用 SDK 函数读取 LED 数据
+ // 璋冪敤 SDK 鍑芥暟璇诲彇 LED 鏁版嵁
const short nRet = mdBdLedRead(m_nPath, vecLedBuffer.data());
if (nRet != 0) {
UpdateLastError(ERROR_CODE_NOT_CONNECTED);
@@ -420,7 +421,7 @@
return nRet;
}
-// 获取板状态
+// 鑾峰彇鏉跨姸鎬�
int CPerformanceMelsec::GetBoardStatus(BoardStatus& status) {
std::lock_guard<std::mutex> lock(m_mtx);
if (!m_bConnected) {
@@ -428,35 +429,36 @@
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);
LOG_ERROR(m_strLastError);
}
- // 将 buf 映射到结构体
+ // 灏� buf 鏄犲皠鍒扮粨鏋勪綋
status = BoardStatus::fromBuffer(buf);
return 0;
}
-// 通用读数据
+// 閫氱敤璇绘暟鎹�
int CPerformanceMelsec::ReadData(const StationIdentifier& station, const short nDevType, const short nDevNo, short nSize, std::vector<short>& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int nRet = ValidateStationAndSize(station, nSize);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 初始化读取缓冲区
+ // 鍒濆鍖栬鍙栫紦鍐插尯
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);
}
@@ -466,38 +468,38 @@
}
if (nRet != 0) {
- vecData.clear(); // 如果读取失败,清空缓冲区
+ vecData.clear(); // 濡傛灉璇诲彇澶辫触锛屾竻绌虹紦鍐插尯
}
return nRet;
}
-// 读取位数据
+// 璇诲彇浣嶆暟鎹�
int CPerformanceMelsec::ReadBitData(const StationIdentifier& station, const DeviceType enDevType, const short nDevNo, const short nBitCount, BitContainer& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int nRet = ValidateStationAndSize(station, nBitCount);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 计算需要读取的字节大小(按位对齐为字节数)
+ // 璁$畻闇�瑕佽鍙栫殑瀛楄妭澶у皬锛堟寜浣嶅榻愪负瀛楄妭鏁帮級
const short nDevType = CalculateDeviceType(station, enDevType);
- const auto nSize = static_cast<short>((nBitCount + 7) / 8); // 向上取整
- std::vector<short> vecTempBuffer((nSize + 1) / 2, 0); // 临时缓冲区,字节对齐
+ const auto nSize = static_cast<short>((nBitCount + 7) / 8); // 鍚戜笂鍙栨暣
+ std::vector<short> vecTempBuffer((nSize + 1) / 2, 0); // 涓存椂缂撳啿鍖猴紝瀛楄妭瀵归綈
nRet = ReadData(station, nDevType, nDevNo, nSize, vecTempBuffer);
if (nRet == 0) {
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertShortToUint8(vecTempBuffer, vecData);
}
return nRet;
}
-// 读取字数据
+// 璇诲彇瀛楁暟鎹�
int CPerformanceMelsec::ReadWordData(const StationIdentifier& station, const DeviceType enDevType, const short nDevNo, const short nWordCount, WordContainer& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int nRet = ValidateStationAndSize(station, nWordCount);
if (nRet != 0) {
UpdateLastError(nRet);
@@ -516,46 +518,47 @@
return nRet;
}
-// 读取双字数据
+// 璇诲彇鍙屽瓧鏁版嵁
int CPerformanceMelsec::ReadDWordData(const StationIdentifier& station, const DeviceType enDevType, const short nDevNo, const short nDWordCount, DWordContainer& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int nRet = ValidateStationAndSize(station, nDWordCount);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- const auto nSize = static_cast<short>(nDWordCount * 2); // 每个双字占两个字(每个双字占 4 字节)
+ const auto nSize = static_cast<short>(nDWordCount * 2); // 姣忎釜鍙屽瓧鍗犱袱涓瓧锛堟瘡涓弻瀛楀崰 4 瀛楄妭锛�
const short nDevType = CalculateDeviceType(station, enDevType);
std::vector<short> vecTempBuffer(nSize, 0);
nRet = ReadData(station, nDevType, nDevNo, nSize, vecTempBuffer);
if (nRet == 0) {
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertShortToUint32(vecTempBuffer, vecData);
}
return nRet;
}
-// 通用写数据
+// 閫氱敤鍐欐暟鎹�
int CPerformanceMelsec::WriteData(const StationIdentifier& station, const short nDevType, const short nDevNo, short nSize, short* pData) {
- // 验证站点参数
+ // 楠岃瘉绔欑偣鍙傛暟
int nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 数据有效性
+ // 鏁版嵁鏈夋晥鎬�
if (nSize < 0 || pData == nullptr) {
UpdateLastError(ERROR_CODE_INVALID_PARAM);
return ERROR_CODE_INVALID_PARAM;
}
- // 确保线程安全的最小锁定范围
+ // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
{
std::lock_guard<std::mutex> lock(m_mtx);
+ nSize *= sizeof(short);
nRet = mdSend(m_nPath, CombineStation(station), nDevType, nDevNo, &nSize, pData);
}
@@ -567,68 +570,68 @@
return nRet;
}
-// 写位数据
+// 鍐欎綅鏁版嵁
int CPerformanceMelsec::WriteBitData(const StationIdentifier& station, const DeviceType enDevType, const short nDevNo, const BitContainer& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
const int nRet = ValidateStationAndData(station, vecData);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 计算需要写入的字节数(位数据需要按 8 位对齐为字节数)
+ // 璁$畻闇�瑕佸啓鍏ョ殑瀛楄妭鏁帮紙浣嶆暟鎹渶瑕佹寜 8 浣嶅榻愪负瀛楄妭鏁帮級
const short nDevType = CalculateDeviceType(station, enDevType);
const auto nSize = static_cast<short>((vecData.size() + 7) / 8);
std::vector<short> vecBuffer(vecData.size() / 2 + vecData.size() % 2, 0);
{
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertUint8ToShort(vecData, vecBuffer);
}
return WriteData(station, nDevType, nDevNo, nSize, vecBuffer.data());
}
-// 写字数据
+// 鍐欏瓧鏁版嵁
int CPerformanceMelsec::WriteWordData(const StationIdentifier& station, const DeviceType enDevType, const short nDevNo, const WordContainer& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
const int nRet = ValidateStationAndData(station, vecData);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 计算需要写入的字节数(每个字占 2 字节)
+ // 璁$畻闇�瑕佸啓鍏ョ殑瀛楄妭鏁帮紙姣忎釜瀛楀崰 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);
}
-// 写双字数据
+// 鍐欏弻瀛楁暟鎹�
int CPerformanceMelsec::WriteDWordData(const StationIdentifier& station, const DeviceType enDevType, const short nDevNo, const DWordContainer& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
const int nRet = ValidateStationAndData(station, vecData);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 计算需要写入的字节数(每个双字占 4 字节)
+ // 璁$畻闇�瑕佸啓鍏ョ殑瀛楄妭鏁帮紙姣忎釜鍙屽瓧鍗� 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); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertUint32ToShort(vecData, vecBuffer);
}
return WriteData(station, nDevType, nDevNo, nSize, vecBuffer.data());
}
-// 扩展读数据
+// 鎵╁睍璇绘暟鎹�
long CPerformanceMelsec::ReadDataEx(const StationIdentifier& station, long nDevType, long nDevNo, long nSize, std::vector<char>& vecData) {
- // 验证站点参数和读取大小是否有效
+ // 楠岃瘉绔欑偣鍙傛暟鍜岃鍙栧ぇ灏忔槸鍚︽湁鏁�
long nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
@@ -640,46 +643,48 @@
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());
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
+ nRet = mdReceiveEx(m_nPath, station.nNetNo, station.nStNo, nDevType, nDevNo, &nSize, vecBuffer.data());
}
if (nRet != 0) {
UpdateLastError(nRet);
LOG_ERROR(m_strLastError);
- } else {
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ }
+ else {
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
+ vecData.resize(nSize);
ConvertShortToChar(vecBuffer, vecData);
}
return 0;
}
-// 扩展写数据
+// 鎵╁睍鍐欐暟鎹�
long CPerformanceMelsec::WriteDataEx(const StationIdentifier& station, long nDevType, long nDevNo, const std::vector<char>& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
long nRet = ValidateStationAndData(station, vecData);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 将 vecData 转换为 short 类型的缓冲区
+ // 灏� 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); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertCharToShort(vecData, vecBuffer);
nRet = mdSendEx(m_nPath, station.nNetNo, station.nStNo, nDevType, nDevNo, &nSize, vecBuffer.data());
}
- // 错误处理和日志记录
+ // 閿欒澶勭悊鍜屾棩蹇楄褰�
if (nRet != 0) {
UpdateLastError(nRet);
LOG_ERROR(m_strLastError);
@@ -688,7 +693,7 @@
return nRet;
}
-// 扩展软元件随机读取
+// 鎵╁睍杞厓浠堕殢鏈鸿鍙�
long CPerformanceMelsec::ReadRandomDataEx(const StationIdentifier& station, const std::vector<SoftElement>& vecSoftElements, std::vector<char>& vecData) {
if (vecSoftElements.empty()) {
UpdateLastError(ERROR_INVALID_PARAMETER);
@@ -696,48 +701,48 @@
return ERROR_INVALID_PARAMETER;
}
- // 准备 dev 数据
- std::vector<short> devBuffer(vecSoftElements.size() * 3 + 1, 0); // 每个软元件需要 3 个 short,外加一个计数器
- devBuffer[0] = static_cast<short>(vecSoftElements.size()); // 第一个元素是软元件数量
+ // 鍑嗗 dev 鏁版嵁
+ std::vector<short> devBuffer(vecSoftElements.size() * 3 + 1, 0); // 姣忎釜杞厓浠堕渶瑕� 3 涓� short锛屽鍔犱竴涓鏁板櫒
+ devBuffer[0] = static_cast<short>(vecSoftElements.size()); // 绗竴涓厓绱犳槸杞厓浠舵暟閲�
for (size_t i = 0; i < vecSoftElements.size(); ++i) {
const SoftElement& element = vecSoftElements[i];
- devBuffer[i * 3 + 1] = element.nType; // 软元件类型
- devBuffer[i * 3 + 2] = static_cast<short>(element.nStartNo); // 起始软元件编号
- devBuffer[i * 3 + 3] = element.nElementCount; // 点数
+ devBuffer[i * 3 + 1] = element.nType; // 杞厓浠剁被鍨�
+ devBuffer[i * 3 + 2] = static_cast<short>(element.nStartNo); // 璧峰杞厓浠剁紪鍙�
+ devBuffer[i * 3 + 3] = element.nElementCount; // 鐐规暟
}
- // 计算读取数据所需缓冲区大小
+ // 璁$畻璇诲彇鏁版嵁鎵�闇�缂撳啿鍖哄ぇ灏�
long nBufferSize = 0;
for (const auto& element : vecSoftElements) {
- nBufferSize += element.nElementCount * 2; // 每个点占用 2 个字节
+ nBufferSize += element.nElementCount * 2; // 姣忎釜鐐瑰崰鐢� 2 涓瓧鑺�
}
- // 锁保护及调用 mdRandREx
+ // 閿佷繚鎶ゅ強璋冪敤 mdRandREx
long nRet = 0;
std::vector<short> vecBuffer(nBufferSize / 2, 0);
{
- std::lock_guard<std::mutex> lock(m_mtx); // 确保线程安全
+ std::lock_guard<std::mutex> lock(m_mtx); // 纭繚绾跨▼瀹夊叏
nRet = mdRandREx(m_nPath, station.nNetNo, station.nStNo, devBuffer.data(), vecBuffer.data(), nBufferSize);
}
- // 错误处理和日志记录
+ // 閿欒澶勭悊鍜屾棩蹇楄褰�
if (nRet != 0) {
UpdateLastError(nRet);
LOG_ERROR(m_strLastError);
return nRet;
}
- // 将读取到的 short 数据转换为 char 数据
+ // 灏嗚鍙栧埌鐨� short 鏁版嵁杞崲涓� char 鏁版嵁
vecData.resize(nBufferSize);
for (size_t i = 0; i < vecBuffer.size(); ++i) {
- vecData[i * 2] = static_cast<char>(vecBuffer[i] & 0xFF); // 低字节
- vecData[i * 2 + 1] = static_cast<char>((vecBuffer[i] >> 8) & 0xFF); // 高字节
+ vecData[i * 2] = static_cast<char>(vecBuffer[i] & 0xFF); // 浣庡瓧鑺�
+ vecData[i * 2 + 1] = static_cast<char>((vecBuffer[i] >> 8) & 0xFF); // 楂樺瓧鑺�
}
return nRet;
}
-// 扩展软元件随机写入(支持多个软元件)
+// 鎵╁睍杞厓浠堕殢鏈哄啓鍏ワ紙鏀寔澶氫釜杞厓浠讹級
long CPerformanceMelsec::WriteRandomDataEx(const StationIdentifier& station, const std::vector<SoftElement>& vecSoftElements, const std::vector<char>& vecData) {
if (vecSoftElements.empty() || vecData.empty()) {
UpdateLastError(ERROR_INVALID_PARAMETER);
@@ -745,26 +750,26 @@
return ERROR_INVALID_PARAMETER;
}
- // 准备 dev 数据
- std::vector<long> devBuffer(vecSoftElements.size() * 3 + 1, 0); // 每个软元件需要 3 个 long,外加一个计数器
- devBuffer[0] = static_cast<long>(vecSoftElements.size()); // 第一个元素是软元件数量
+ // 鍑嗗 dev 鏁版嵁
+ std::vector<long> devBuffer(vecSoftElements.size() * 3 + 1, 0); // 姣忎釜杞厓浠堕渶瑕� 3 涓� long锛屽鍔犱竴涓鏁板櫒
+ devBuffer[0] = static_cast<long>(vecSoftElements.size()); // 绗竴涓厓绱犳槸杞厓浠舵暟閲�
for (size_t i = 0; i < vecSoftElements.size(); ++i) {
const SoftElement& element = vecSoftElements[i];
- devBuffer[i * 3 + 1] = static_cast<long>(element.nType); // 软元件类型
- devBuffer[i * 3 + 2] = element.nStartNo; // 起始软元件编号(已经是 long 类型,无需转换)
- devBuffer[i * 3 + 3] = static_cast<long>(element.nElementCount); // 点数
+ devBuffer[i * 3 + 1] = static_cast<long>(element.nType); // 杞厓浠剁被鍨�
+ devBuffer[i * 3 + 2] = element.nStartNo; // 璧峰杞厓浠剁紪鍙凤紙宸茬粡鏄� long 绫诲瀷锛屾棤闇�杞崲锛�
+ devBuffer[i * 3 + 3] = static_cast<long>(element.nElementCount); // 鐐规暟
}
- // 锁保护及调用 mdRandWEx
+ // 閿佷繚鎶ゅ強璋冪敤 mdRandWEx
long nRet = 0;
std::vector<short> vecBuffer(vecData.size() / 2, 0);
{
- std::lock_guard<std::mutex> lock(m_mtx); // 确保线程安全
+ std::lock_guard<std::mutex> lock(m_mtx); // 纭繚绾跨▼瀹夊叏
ConvertCharToShort(vecData, vecBuffer);
nRet = mdRandWEx(m_nPath, station.nNetNo, station.nStNo, devBuffer.data(), vecBuffer.data(), static_cast<long>(vecBuffer.size()));
}
- // 错误处理和日志记录
+ // 閿欒澶勭悊鍜屾棩蹇楄褰�
if (nRet != 0) {
UpdateLastError(nRet);
LOG_ERROR(m_strLastError);
@@ -773,9 +778,9 @@
return nRet;
}
-// 远程设备站/远程站的缓冲存储器读取
+// 杩滅▼璁惧绔�/杩滅▼绔欑殑缂撳啿瀛樺偍鍣ㄨ鍙�
long CPerformanceMelsec::ReadRemoteBuffer(const StationIdentifier& station, long nOffset, long nSize, std::vector<char>& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
@@ -790,36 +795,37 @@
long nActualSize = (nSize + 1) / 2;
std::vector<short> vecBuffer(nActualSize, 0);
{
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
nRet = mdRemBufReadEx(m_nPath, station.nNetNo, station.nStNo, nOffset, &nActualSize, vecBuffer.data());
}
if (nRet != 0) {
- UpdateLastError(nRet); // 更新错误码
+ UpdateLastError(nRet); // 鏇存柊閿欒鐮�
LOG_ERROR(m_strLastError);
- } else {
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ }
+ else {
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertShortToChar(vecBuffer, vecData);
}
return nRet;
}
-// 远程设备站/远程站的缓冲存储器写入
+// 杩滅▼璁惧绔�/杩滅▼绔欑殑缂撳啿瀛樺偍鍣ㄥ啓鍏�
long CPerformanceMelsec::WriteRemoteBuffer(const StationIdentifier& station, long nOffset, const std::vector<char>& vecData) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
long nRet = ValidateStationAndData(station, vecData);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 将 vecData 转换为 short 类型的缓冲区
+ // 灏� vecData 杞崲涓� short 绫诲瀷鐨勭紦鍐插尯
long nSize = static_cast<long>(vecData.size());
std::vector<short> vecBuffer((nSize + 1) / 2, 0);
{
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertCharToShort(vecData, vecBuffer);
nRet = mdRemBufWriteEx(m_nPath, station.nNetNo, station.nStNo, nOffset, &nSize, vecBuffer.data());
}
@@ -832,7 +838,7 @@
return nRet;
}
-// 远程站的缓冲存储器读取 对象站IP地址指定
+// 杩滅▼绔欑殑缂撳啿瀛樺偍鍣ㄨ鍙� 瀵硅薄绔橧P鍦板潃鎸囧畾
long CPerformanceMelsec::ReadRemoteBufferByIp(const std::string& strIP, long nOffset, long nSize, std::vector<char>& vecData) {
uint32_t nAddress = 0;
if (nSize < 0 || !ConvertIpStringToUint32(strIP, nAddress)) {
@@ -840,29 +846,30 @@
return ERROR_CODE_INVALID_PARAM;
}
- // 将缓冲区大小调整为 nSize
+ // 灏嗙紦鍐插尯澶у皬璋冩暣涓� nSize
vecData.resize(nSize, 0);
- std::vector<short> vecBuffer((nSize + 1) / 2, 0); // 转换为 short 类型
+ std::vector<short> vecBuffer((nSize + 1) / 2, 0); // 杞崲涓� short 绫诲瀷
- // 调用底层 SDK
+ // 璋冪敤搴曞眰 SDK
long nRet = 0;
{
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
nRet = mdRemBufReadIPEx(m_nPath, static_cast<long>(nAddress), nOffset, &nSize, vecBuffer.data());
}
if (nRet != 0) {
UpdateLastError(nRet);
LOG_ERROR(m_strLastError);
- } else {
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全保护
+ }
+ else {
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏淇濇姢
ConvertShortToChar(vecBuffer, vecData);
}
return nRet;
}
-// 远程站的缓冲存储器写入 对象站IP地址指定
+// 杩滅▼绔欑殑缂撳啿瀛樺偍鍣ㄥ啓鍏� 瀵硅薄绔橧P鍦板潃鎸囧畾
long CPerformanceMelsec::WriteRemoteBufferByIp(const std::string& strIP, long nOffset, const std::vector<char>& vecData) {
uint32_t nAddress = 0;
if (vecData.empty() || !ConvertIpStringToUint32(strIP, nAddress)) {
@@ -870,13 +877,13 @@
return ERROR_CODE_INVALID_PARAM;
}
- // 转换 vecData 为 short 类型的缓冲区
+ // 杞崲 vecData 涓� short 绫诲瀷鐨勭紦鍐插尯
long nSize = static_cast<long>(vecData.size());
std::vector<short> vecBuffer((nSize + 1) / 2, 0);
long nRet = 0;
{
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏
ConvertCharToShort(vecData, vecBuffer);
nRet = mdRemBufWriteIPEx(m_nPath, static_cast<long>(nAddress), nOffset, &nSize, vecBuffer.data());
}
@@ -889,18 +896,18 @@
return nRet;
}
-// 设置(ON)对象站的指定位软元件
+// 璁剧疆(ON)瀵硅薄绔欑殑鎸囧畾浣嶈蒋鍏冧欢
int CPerformanceMelsec::SetBitDevice(const StationIdentifier& station, const DeviceType enDevType, const short nDevNo) {
- // 验证站点参数
+ // 楠岃瘉绔欑偣鍙傛暟
int nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 确保线程安全的最小锁定范围
+ // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
{
- std::lock_guard<std::mutex> lock(m_mtx); // 线程安全
+ std::lock_guard<std::mutex> lock(m_mtx); // 绾跨▼瀹夊叏
const short nDevType = CalculateDeviceType(station, enDevType);
nRet = mdDevSet(m_nPath, CombineStation(station), nDevType, nDevNo);
}
@@ -913,16 +920,16 @@
return nRet;
}
-// 复位(OFF)对象站的指定位软元件
+// 澶嶄綅(OFF)瀵硅薄绔欑殑鎸囧畾浣嶈蒋鍏冧欢
int CPerformanceMelsec::ResetBitDevice(const StationIdentifier& station, const DeviceType enDevType, const short enDevNo) {
- // 验证站点参数
+ // 楠岃瘉绔欑偣鍙傛暟
int nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 确保线程安全的最小锁定范围
+ // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
{
std::lock_guard<std::mutex> lock(m_mtx);
const short nDevType = CalculateDeviceType(station, enDevType);
@@ -937,11 +944,11 @@
return nRet;
}
-// 扩展位软元件设置
+// 鎵╁睍浣嶈蒋鍏冧欢璁剧疆
long CPerformanceMelsec::SetBitDeviceEx(const StationIdentifier& station, long nDevType, long nDevNo) {
std::lock_guard<std::mutex> lock(m_mtx);
- // 检查参数有效性
+ // 妫�鏌ュ弬鏁版湁鏁堟��
long nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
@@ -957,11 +964,11 @@
return nRet;
}
-// 扩展位软元件复位
+// 鎵╁睍浣嶈蒋鍏冧欢澶嶄綅
long CPerformanceMelsec::ResetBitDeviceEx(const StationIdentifier& station, long nDevType, long nDevNo) {
std::lock_guard<std::mutex> lock(m_mtx);
- // 检查参数有效性
+ // 妫�鏌ュ弬鏁版湁鏁堟��
long nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
@@ -977,23 +984,23 @@
return nRet;
}
-// 执行对象站的CPU
+// 鎵ц瀵硅薄绔欑殑CPU
int CPerformanceMelsec::ControlCPU(const StationIdentifier& station, ControlCode enControlCode) {
- // 验证站点参数和数据有效性
+ // 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int nRet = ValidateStation(station);
if (nRet != 0) {
UpdateLastError(nRet);
return nRet;
}
- // 验证控制码是否合法
+ // 楠岃瘉鎺у埗鐮佹槸鍚﹀悎娉�
const auto nControlCode = static_cast<short>(enControlCode);
if (nControlCode < 0 || nControlCode > 2) {
- UpdateLastError(ERROR_CODE_INVALID_PARAM); // 参数错误
+ UpdateLastError(ERROR_CODE_INVALID_PARAM); // 鍙傛暟閿欒
return ERROR_CODE_INVALID_PARAM;
}
- // 确保线程安全的最小锁定范围
+ // 纭繚绾跨▼瀹夊叏鐨勬渶灏忛攣瀹氳寖鍥�
{
std::lock_guard<std::mutex> lock(m_mtx);
nRet = mdControl(m_nPath, CombineStation(station), nControlCode);
@@ -1007,7 +1014,7 @@
return nRet;
}
-// 事件等待
+// 浜嬩欢绛夊緟
int CPerformanceMelsec::WaitForBoardEvent(std::vector<short> vecEventNumbers, const int nTimeoutMs, EventDetails& details) {
std::lock_guard<std::mutex> lock(m_mtx);
@@ -1021,12 +1028,12 @@
return ERROR_CODE_INVALID_PARAM;
}
- // 第 0 个元素存储数量,最大支持 64 个事件
- std::array<short, 65> eventno = {0};
+ // 绗� 0 涓厓绱犲瓨鍌ㄦ暟閲忥紝鏈�澶ф敮鎸� 64 涓簨浠�
+ std::array<short, 65> eventno = { 0 };
eventno[0] = static_cast<short>(vecEventNumbers.size());
std::copy(vecEventNumbers.begin(), vecEventNumbers.end(), eventno.begin() + 1);
- // 初始化输出参数
+ // 鍒濆鍖栬緭鍑哄弬鏁�
details.nEventNo = 0;
details.details.fill(0);
@@ -1039,100 +1046,100 @@
return nRet;
}
-//============================================辅助函数=======================================================
-// 更新最近的错误信息
+//============================================杈呭姪鍑芥暟=======================================================
+// 鏇存柊鏈�杩戠殑閿欒淇℃伅
void CPerformanceMelsec::UpdateLastError(const int nCode) {
if (nCode == 0) {
return;
}
- // 检查错误码是否存在于映射表中
+ // 妫�鏌ラ敊璇爜鏄惁瀛樺湪浜庢槧灏勮〃涓�
const auto it = m_mapError.find(nCode);
if (it != m_mapError.end()) {
- // 如果找到,直接返回对应语言的错误信息
+ // 濡傛灉鎵惧埌锛岀洿鎺ヨ繑鍥炲搴旇瑷�鐨勯敊璇俊鎭�
m_strLastError = it->second;
- } else {
- // 如果未找到,处理特殊范围
+ }
+ else {
+ // 濡傛灉鏈壘鍒帮紝澶勭悊鐗规畩鑼冨洿
m_strLastError = "Unknown error.";
if (nCode == -28611 || nCode == -28612) {
- // 系统出错
+ // 绯荤粺鍑洪敊
m_strLastError = "System error.";
}
if (nCode >= -20480 && nCode <= -16384) {
- // CC-Link 系统检测出的错误
+ // CC-Link 绯荤粺妫�娴嬪嚭鐨勯敊璇�
m_strLastError = "Error detected in the CC-Link system.";
}
if (nCode >= -12288 && nCode <= -8193) {
- // CC-Link IE TSN 系统检测出的错误
+ // CC-Link IE TSN 绯荤粺妫�娴嬪嚭鐨勯敊璇�
m_strLastError = "Error detected in the CC-Link IE TSN system.";
}
if (nCode >= -8192 && nCode <= -4097) {
- // CC-Link IE 控制网络系统检测出的错误
+ // CC-Link IE 鎺у埗缃戠粶绯荤粺妫�娴嬪嚭鐨勯敊璇�
m_strLastError = "Error detected in the CC-Link IE control network system.";
}
if (nCode >= -4096 && nCode <= -257) {
- // MELSECNET/10 或 MELSECNET/网络系统错误范围
+ // MELSECNET/10 鎴� MELSECNET/缃戠粶绯荤粺閿欒鑼冨洿
m_strLastError = "Errors detected in MELSECNET/10 or MELSECNET/network system.";
}
if (nCode >= 4096 && nCode <= 16383) {
- // MELSEC 数据链接库范围
+ // MELSEC 鏁版嵁閾炬帴搴撹寖鍥�
m_strLastError = "Internal error detected by MELSEC Data Link Library.";
}
if (nCode == 18944 || nCode == 18945) {
- // 链接关联出错
+ // 閾炬帴鍏宠仈鍑洪敊
m_strLastError = "Link association error: Network does not exist, unsupported CPU, or incorrect network No./station number.";
}
if (nCode >= 16384 && nCode <= 20479) {
- // PLC CPU 检测范围
+ // PLC CPU 妫�娴嬭寖鍥�
m_strLastError = "Errors detected by the programmable controller CPU in the target station.";
}
if (nCode >= 28416 && nCode <= 28671) {
- // 冗余功能模块范围
+ // 鍐椾綑鍔熻兘妯″潡鑼冨洿
m_strLastError = "Error detected in the redundancy module of the target station.";
}
}
}
-// 检查连接状态和站点参数有效性
+// 妫�鏌ヨ繛鎺ョ姸鎬佸拰绔欑偣鍙傛暟鏈夋晥鎬�
int CPerformanceMelsec::ValidateStation(const StationIdentifier& station) const {
- // 检查是否已连接
+ // 妫�鏌ユ槸鍚﹀凡杩炴帴
if (!m_bConnected.load()) {
return ERROR_CODE_NOT_CONNECTED;
}
- // 检查网络号和站点号范围
+ // 妫�鏌ョ綉缁滃彿鍜岀珯鐐瑰彿鑼冨洿
if (station.nNetNo < 0 || station.nNetNo > 239 || station.nStNo < 0 || station.nStNo > 255) {
return ERROR_CODE_INVALID_PARAM;
}
- return 0; // 参数有效
+ return 0; // 鍙傛暟鏈夋晥
}
-// 验证站点参数和数据有效性
+// 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
int CPerformanceMelsec::ValidateStationAndSize(const StationIdentifier& station, const short nCount) const {
- // 验证站点参数
+ // 楠岃瘉绔欑偣鍙傛暟
const int nRet = ValidateStation(station);
if (nRet != 0) {
- return nRet; // 如果站点验证失败,返回对应错误码
+ return nRet; // 濡傛灉绔欑偣楠岃瘉澶辫触锛岃繑鍥炲搴旈敊璇爜
}
if (nCount <= 0) {
return ERROR_CODE_INVALID_PARAM;
}
- return 0; // 验证通过
+ return 0; // 楠岃瘉閫氳繃
}
-
-// IP字符串转uint32_t
+// IP瀛楃涓茶浆uint32_t
bool CPerformanceMelsec::ConvertIpStringToUint32(const std::string& strIP, uint32_t& nIP) {
nIP = 0;
std::stringstream ss(strIP);
@@ -1151,173 +1158,181 @@
return true;
}
-//============================================静态辅助函数====================================================
-// 延时,并且转发窗口消息
+//============================================闈欐�佽緟鍔╁嚱鏁�====================================================
+// 寤舵椂锛屽苟涓旇浆鍙戠獥鍙f秷鎭�
void CPerformanceMelsec::Delay(const unsigned int nDelayMs) {
MSG message;
- // 如果延迟时间为 0,仅处理一次消息队列
+ // 濡傛灉寤惰繜鏃堕棿涓� 0锛屼粎澶勭悊涓�娆℃秷鎭槦鍒�
if (nDelayMs == 0) {
- // 非阻塞的检查消息队列
+ // 闈為樆濉炵殑妫�鏌ユ秷鎭槦鍒�
if (PeekMessage(&message, nullptr, 0, 0, PM_REMOVE)) {
- TranslateMessage(&message); // 将消息转化为有效的窗口消息
- DispatchMessage(&message); // 派发消息给相应的窗口过程
+ TranslateMessage(&message); // 灏嗘秷鎭浆鍖栦负鏈夋晥鐨勭獥鍙f秷鎭�
+ DispatchMessage(&message); // 娲惧彂娑堟伅缁欑浉搴旂殑绐楀彛杩囩▼
}
return;
}
DWORD finish;
- const DWORD start = GetTickCount(); // 获取当前的时间戳(从系统启动以来的毫秒数)
+ const DWORD start = GetTickCount(); // 鑾峰彇褰撳墠鐨勬椂闂存埑锛堜粠绯荤粺鍚姩浠ユ潵鐨勬绉掓暟锛�
do {
if (PeekMessage(&message, nullptr, 0, 0, PM_REMOVE)) {
- TranslateMessage(&message); // 转换消息
- DispatchMessage(&message); // 处理消息
+ TranslateMessage(&message); // 杞崲娑堟伅
+ DispatchMessage(&message); // 澶勭悊娑堟伅
}
- Sleep(1); // 暂停 1 毫秒,防止过度占用 CPU
- finish = GetTickCount(); // 获取当前的时间戳
- } while ((finish - start) < nDelayMs); // 循环直到经过的时间大于指定的延迟时间
+ Sleep(1); // 鏆傚仠 1 姣锛岄槻姝㈣繃搴﹀崰鐢� CPU
+ finish = GetTickCount(); // 鑾峰彇褰撳墠鐨勬椂闂存埑
+ } while ((finish - start) < nDelayMs); // 寰幆鐩村埌缁忚繃鐨勬椂闂村ぇ浜庢寚瀹氱殑寤惰繜鏃堕棿
}
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;
}
-// 合并网络号和站点号
+// 鍚堝苟缃戠粶鍙峰拰绔欑偣鍙�
short CPerformanceMelsec::CombineStation(const StationIdentifier& station) {
return static_cast<short>(station.nStNo | ((station.nNetNo << 8) & 0xFF00));
}
-// 计算软元件类型
+// 璁$畻杞厓浠剁被鍨�
short CPerformanceMelsec::CalculateDeviceType(const StationIdentifier& station, DeviceType enDevType) {
int nDevType = static_cast<int>(enDevType);
- // 根据软元件类型的特定规则进行计算
+ // 鏍规嵁杞厓浠剁被鍨嬬殑鐗瑰畾瑙勫垯杩涜璁$畻
if (enDevType == DeviceType::LX || enDevType == DeviceType::LY ||
enDevType == DeviceType::LB || enDevType == DeviceType::LW ||
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) {
- // 起始 I/O No. ÷ 16 的值
+ }
+ else if (enDevType == DeviceType::SPG) {
+ // 璧峰 I/O No. 梅 16 鐨勫��
nDevType += 0 / 16;
}
return static_cast<short>(nDevType);
}
-// std::vector<char>转换为std::vector<short>
+// std::vector<char>杞崲涓簊td::vector<short>
void CPerformanceMelsec::ConvertCharToShort(const std::vector<char>& vecChar, std::vector<short>& vecShort) {
- vecShort.resize((vecChar.size() + 1) / 2, 0); // 调整 short 容器大小
+ vecShort.resize((vecChar.size() + 1) / 2, 0); // 璋冩暣 short 瀹瑰櫒澶у皬
for (size_t i = 0; i < vecChar.size(); i++) {
if (i % 2 == 0) {
- vecShort[i / 2] = static_cast<unsigned char>(vecChar[i]); // 低字节
- } else {
- vecShort[i / 2] |= static_cast<unsigned char>(vecChar[i]) << 8; // 高字节
+ vecShort[i / 2] = static_cast<unsigned char>(vecChar[i]); // 浣庡瓧鑺�
+ }
+ else {
+ vecShort[i / 2] |= static_cast<unsigned char>(vecChar[i]) << 8; // 楂樺瓧鑺�
}
}
}
-// std::vector<short>转换为std::vector<char>
+// std::vector<short>杞崲涓簊td::vector<char>
void CPerformanceMelsec::ConvertShortToChar(const std::vector<short>& vecShort, std::vector<char>& vecChar) {
- vecChar.resize(vecShort.size() * 2); // 调整 char 容器大小
+ vecChar.resize(vecShort.size() * 2); // 璋冩暣 char 瀹瑰櫒澶у皬
for (size_t i = 0; i < vecShort.size(); i++) {
- vecChar[i * 2] = static_cast<char>(vecShort[i] & 0xFF); // 低字节
- vecChar[i * 2 + 1] = static_cast<char>((vecShort[i] >> 8) & 0xFF); // 高字节
+ vecChar[i * 2] = static_cast<char>(vecShort[i] & 0xFF); // 浣庡瓧鑺�
+ vecChar[i * 2 + 1] = static_cast<char>((vecShort[i] >> 8) & 0xFF); // 楂樺瓧鑺�
}
}
-// std::vector<uint8_t>转换为std::vector<short>
+// std::vector<uint8_t>杞崲涓簊td::vector<short>
void CPerformanceMelsec::ConvertUint8ToShort(const std::vector<uint8_t>& vecUint8, std::vector<short>& vecShort) {
- vecShort.resize((vecUint8.size() + 1) / 2, 0); // 调整 short 容器大小
+ vecShort.resize((vecUint8.size() + 1) / 2, 0); // 璋冩暣 short 瀹瑰櫒澶у皬
for (size_t i = 0; i < vecUint8.size(); i++) {
if (i % 2 == 0) {
- vecShort[i / 2] = static_cast<short>(vecUint8[i]); // 低字节
- } else {
- vecShort[i / 2] |= static_cast<short>(vecUint8[i] << 8); // 高字节
+ vecShort[i / 2] = static_cast<short>(vecUint8[i]); // 浣庡瓧鑺�
+ }
+ else {
+ vecShort[i / 2] |= static_cast<short>(vecUint8[i] << 8); // 楂樺瓧鑺�
}
}
}
-// std::vector<short>转换为std::vector<uint8_t>
+// std::vector<short>杞崲涓簊td::vector<uint8_t>
void CPerformanceMelsec::ConvertShortToUint8(const std::vector<short>& vecShort, std::vector<uint8_t>& vecUint8) {
- vecUint8.resize(vecShort.size() * 2); // 调整 uint8_t 容器大小
+ vecUint8.resize(vecShort.size() * 2); // 璋冩暣 uint8_t 瀹瑰櫒澶у皬
for (size_t i = 0; i < vecShort.size(); i++) {
- vecUint8[i * 2] = static_cast<uint8_t>(vecShort[i] & 0xFF); // 低字节
- vecUint8[i * 2 + 1] = static_cast<uint8_t>((vecShort[i] >> 8) & 0xFF); // 高字节
+ vecUint8[i * 2] = static_cast<uint8_t>(vecShort[i] & 0xFF); // 浣庡瓧鑺�
+ vecUint8[i * 2 + 1] = static_cast<uint8_t>((vecShort[i] >> 8) & 0xFF); // 楂樺瓧鑺�
}
}
-// std::vector<uint32_t>转换为std::vector<short>
+// std::vector<uint32_t>杞崲涓簊td::vector<short>
void CPerformanceMelsec::ConvertUint32ToShort(const std::vector<uint32_t>& vecUint32, std::vector<short>& vecShort) {
- vecShort.resize(vecUint32.size() * 2); // 每个 uint32_t 转换为两个 short
+ vecShort.resize(vecUint32.size() * 2); // 姣忎釜 uint32_t 杞崲涓轰袱涓� short
for (size_t i = 0; i < vecUint32.size(); i++) {
- vecShort[i * 2] = static_cast<short>(vecUint32[i] & 0xFFFF); // 低16位
- vecShort[i * 2 + 1] = static_cast<short>((vecUint32[i] >> 16) & 0xFFFF); // 高16位
+ vecShort[i * 2] = static_cast<short>(vecUint32[i] & 0xFFFF); // 浣�16浣�
+ vecShort[i * 2 + 1] = static_cast<short>((vecUint32[i] >> 16) & 0xFFFF); // 楂�16浣�
}
}
-// std::vector<short>转换为std::vector<uint32_t>
+// std::vector<short>杞崲涓簊td::vector<uint32_t>
void CPerformanceMelsec::ConvertShortToUint32(const std::vector<short>& vecShort, std::vector<uint32_t>& vecUint32) {
- vecUint32.resize((vecShort.size() + 1) / 2, 0); // 每两个 short 合并为一个 uint32_t
+ 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位
+ 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浣�
}
}
-//============================================模板辅助函数====================================================
-// 验证站点参数和数据有效性
+//============================================妯℃澘杈呭姪鍑芥暟====================================================
+// 楠岃瘉绔欑偣鍙傛暟鍜屾暟鎹湁鏁堟��
template <typename T>
int CPerformanceMelsec::ValidateStationAndData(const StationIdentifier& station, const std::vector<T>& vecData) {
- // 验证站点参数
+ // 楠岃瘉绔欑偣鍙傛暟
const int nRet = ValidateStation(station);
if (nRet != 0) {
- return nRet; // 如果站点验证失败,返回对应错误码
+ return nRet; // 濡傛灉绔欑偣楠岃瘉澶辫触锛岃繑鍥炲搴旈敊璇爜
}
- // 验证数据是否为空
+ // 楠岃瘉鏁版嵁鏄惁涓虹┖
if (vecData.empty()) {
return ERROR_CODE_INVALID_PARAM;
}
- return 0; // 验证通过
+ return 0; // 楠岃瘉閫氳繃
}
-// 由低转高容器的模板(整型)
+// 鐢变綆杞珮瀹瑰櫒鐨勬ā鏉匡紙鏁村瀷锛�
template <typename T, typename U>
void CPerformanceMelsec::ConvertLowToHigh(const std::vector<T>& vecLow, std::vector<U>& vecHigh) {
static_assert(std::is_integral<T>::value && std::is_integral<U>::value, "T and U must be integral types");
- // 自动计算 nGroupSize
+ // 鑷姩璁$畻 nGroupSize
constexpr size_t nGroupSize = sizeof(U) / sizeof(T);
- // 如果 T 和 U 的大小相等,直接转换
+ // 濡傛灉 T 鍜� U 鐨勫ぇ灏忕浉绛夛紝鐩存帴杞崲
if (sizeof(T) == sizeof(U)) {
vecHigh.assign(vecLow.begin(), vecLow.end());
return;
}
- // 如果 U 的大小是 T 的倍数,正常组合
+ // 濡傛灉 U 鐨勫ぇ灏忔槸 T 鐨勫�嶆暟锛屾甯哥粍鍚�
static_assert(sizeof(U) > sizeof(T), "Size of U must be greater than or equal to size of T");
- // 计算完整组的数量
- size_t nHighSize = (vecLow.size() + nGroupSize - 1) / nGroupSize; // 向上取整
+ // 璁$畻瀹屾暣缁勭殑鏁伴噺
+ size_t nHighSize = (vecLow.size() + nGroupSize - 1) / nGroupSize; // 鍚戜笂鍙栨暣
vecHigh.resize(nHighSize, 0);
- // 合并低位数据到高位数据
+ // 鍚堝苟浣庝綅鏁版嵁鍒伴珮浣嶆暟鎹�
for (size_t i = 0; i < vecLow.size(); i++) {
vecHigh[i / nGroupSize] |= (static_cast<U>(vecLow[i]) << ((i % nGroupSize) * CHAR_BIT * sizeof(T)));
}
@@ -1325,27 +1340,27 @@
return vecHigh;
}
-// 由高转低容器的模板(整型)
+// 鐢遍珮杞綆瀹瑰櫒鐨勬ā鏉匡紙鏁村瀷锛�
template <typename T, typename U>
void CPerformanceMelsec::ConvertHighToLow(const std::vector<T>& vecHigh, std::vector<U>& vecLow) {
static_assert(std::is_integral<T>::value && std::is_integral<U>::value, "T and U must be integral types");
- // 自动计算 nGroupSize
+ // 鑷姩璁$畻 nGroupSize
constexpr size_t nGroupSize = sizeof(T) / sizeof(U);
- // 如果 T 和 U 的大小相等,直接转换
+ // 濡傛灉 T 鍜� U 鐨勫ぇ灏忕浉绛夛紝鐩存帴杞崲
if (sizeof(T) == sizeof(U)) {
vecLow.assign(vecHigh.begin(), vecHigh.end());
return;
}
- // 如果 T 的大小是 U 的倍数,正常分解
+ // 濡傛灉 T 鐨勫ぇ灏忔槸 U 鐨勫�嶆暟锛屾甯稿垎瑙�
static_assert(sizeof(T) > sizeof(U), "Size of T must be greater than or equal to size of U");
- size_t nLowSize = vecHigh.size() * nGroupSize; // 低容器的大小
+ size_t nLowSize = vecHigh.size() * nGroupSize; // 浣庡鍣ㄧ殑澶у皬
vecLow.resize(nLowSize, 0);
- // 分解高位数据到低位数据
+ // 鍒嗚В楂樹綅鏁版嵁鍒颁綆浣嶆暟鎹�
for (size_t i = 0; i < vecHigh.size(); i++) {
for (size_t j = 0; j < nGroupSize; j++) {
vecLow[i * nGroupSize + j] = static_cast<U>((vecHigh[i] >> (j * CHAR_BIT * sizeof(U))) & ((1ULL << (CHAR_BIT * sizeof(U))) - 1));
diff --git a/SourceCode/Bond/Servo/ServoDlg.cpp b/SourceCode/Bond/Servo/ServoDlg.cpp
index a8a340b..292467f 100644
--- a/SourceCode/Bond/Servo/ServoDlg.cpp
+++ b/SourceCode/Bond/Servo/ServoDlg.cpp
@@ -652,6 +652,38 @@
Invalidate();
}
+void CServoDlg::UpdateDeviceStatus(int id, DeviceStatus status)
+{
+ // 根据状态设置颜色
+ COLORREF newBackgroundColor;
+ COLORREF newFrameColor1;
+ COLORREF newFrameColor2;
+
+ switch (status) {
+ case Online:
+ newBackgroundColor = RGB(255, 0, 0);
+ newFrameColor1 = RGB(22, 22, 22);
+ newFrameColor2 = RGB(255, 127, 39);
+ break;
+ case Offline:
+ newBackgroundColor = RGB(0, 255, 0);
+ newFrameColor1 = RGB(22, 22, 22);
+ newFrameColor2 = RGB(255, 127, 39);
+ break;
+ default:
+ newBackgroundColor = RGB(255, 255, 255); // 默认白色背景
+ newFrameColor1 = RGB(0, 0, 0); // 默认黑色框架1
+ newFrameColor2 = RGB(0, 0, 0); // 默认黑色框架2
+ break;
+ }
+
+ m_pGraph->UpdateIndicateBox1Colors(id, newBackgroundColor, newFrameColor1, newFrameColor2);
+
+ // 刷新界面
+ Invalidate();
+ UpdateWindow();
+}
+
void CServoDlg::OnSize(UINT nType, int cx, int cy)
{
CDialogEx::OnSize(nType, cx, cy);
diff --git a/SourceCode/Bond/Servo/ServoDlg.h b/SourceCode/Bond/Servo/ServoDlg.h
index 0a760d0..b6bec04 100644
--- a/SourceCode/Bond/Servo/ServoDlg.h
+++ b/SourceCode/Bond/Servo/ServoDlg.h
@@ -8,6 +8,10 @@
#include "LogDlg.h"
#include "TerminalDisplayDlg.h"
+enum DeviceStatus {
+ Online, // 在线
+ Offline, // 离线
+};
// CServoDlg 对话框
class CServoDlg : public CDialogEx
@@ -26,6 +30,7 @@
void UpdateLogBtn();
void UpdateRobotPosition(float percentage);
void RotateRobot(float angleInDegrees);
+ void UpdateDeviceStatus(int id, DeviceStatus status);
private:
diff --git a/SourceCode/Bond/Servo/ServoGraph.cpp b/SourceCode/Bond/Servo/ServoGraph.cpp
index 3e815c8..daf274a 100644
--- a/SourceCode/Bond/Servo/ServoGraph.cpp
+++ b/SourceCode/Bond/Servo/ServoGraph.cpp
@@ -634,6 +634,25 @@
}
}
+void CServoGraph::UpdateIndicateBox1Colors(int id, COLORREF newBackgroundColor, COLORREF newFrameColor1, COLORREF newFrameColor2)
+{
+ INDICATEBOX* pIndicateBox = GetIndicateBox(id);
+ if (pIndicateBox != nullptr) {
+ pIndicateBox->box1BackgroundColor = newBackgroundColor;
+ pIndicateBox->box1FrameColor[0] = newFrameColor1;
+ pIndicateBox->box1FrameColor[1] = newFrameColor2;
+ }
+}
+
+void CServoGraph::UpdateIndicateBox2Colors(int id, COLORREF newBackgroundColor, COLORREF newFrameColor)
+{
+ INDICATEBOX* pIndicateBox = GetIndicateBox(id);
+ if (pIndicateBox != nullptr) {
+ pIndicateBox->box2BackgroundColor = newBackgroundColor;
+ pIndicateBox->box2FrameColor = newFrameColor;
+ }
+}
+
void CServoGraph::DrawImage(HDC hMemDC, IMAGE& item)
{
// 载入BMP
diff --git a/SourceCode/Bond/Servo/ServoGraph.h b/SourceCode/Bond/Servo/ServoGraph.h
index 0a58675..d409660 100644
--- a/SourceCode/Bond/Servo/ServoGraph.h
+++ b/SourceCode/Bond/Servo/ServoGraph.h
@@ -170,6 +170,8 @@
void UpdateImageCoordinates(int id, int newX, int newY);
void UpdateIndicateBoxCoordinates(int id, int newX, int newY);
void UpdateImageAngle(int id, float angle);
+ void UpdateIndicateBox1Colors(int id, COLORREF newBackgroundColor, COLORREF newFrameColor1, COLORREF newFrameColor2);
+ void UpdateIndicateBox2Colors(int id, COLORREF newBackgroundColor, COLORREF newFrameColor);
private:
void DrawImage(HDC hMemDC, IMAGE& item);
--
Gitblit v1.9.3