darker
2025-02-14 611471f319d13a13602cae65f2b2bc2afd5b1dbf
1. 修复读写字、读写双字和扩展读写数据函数
已修改1个文件
60 ■■■■■ 文件已修改
SourceCode/Bond/Servo/CCLinkPerformance/PerformanceMelsec.cpp 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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);
    }
@@ -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);
    }
@@ -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);    // 高字节
        }
    }