| | |
| | | |
| | | #ifdef _DEBUG |
| | | #undef THIS_FILE |
| | | static char THIS_FILE[] = __FILE__; |
| | | static char* THIS_FILE = __FILE__; |
| | | #define new DEBUG_NEW |
| | | #endif |
| | | |
| | |
| | | } |
| | | |
| | | // 通用读数据 |
| | | int CPerformanceMelsec::ReadData(const StationIdentifier& station, const short nDevType, const short nDevNo, short nSize, std::vector<short>& vecData) { |
| | | int CPerformanceMelsec::ReadData(const StationIdentifier& station, const long nDevType, const long nDevNo, long nSize, std::vector<short>& vecData) { |
| | | // 验证站点参数和数据有效性 |
| | | int nRet = ValidateStationAndSize(station, nSize); |
| | | if (nRet != 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); |
| | | nRet = mdReceiveEx(m_nPath, station.nNetNo, station.nStNo, nDevType, (long)(unsigned short)nDevNo, &nSize, pData); |
| | | } |
| | | |
| | | if (nRet != 0) { |
| | |
| | | } |
| | | |
| | | // 通用写数据 |
| | | int CPerformanceMelsec::WriteData(const StationIdentifier& station, const short nDevType, const short nDevNo, short nSize, short* pData) { |
| | | int CPerformanceMelsec::WriteData(const StationIdentifier& station, const long nDevType, const long nDevNo, long nSize, short* pData) { |
| | | // 验证站点参数 |
| | | int nRet = ValidateStation(station); |
| | | if (nRet != 0) { |
| | |
| | | { |
| | | std::lock_guard<std::mutex> lock(m_mtx); |
| | | nSize *= sizeof(short); |
| | | nRet = mdSend(m_nPath, CombineStation(station), nDevType, nDevNo, &nSize, pData); |
| | | nRet = mdSendEx(m_nPath, station.nNetNo, station.nStNo, nDevType, nDevNo, &nSize, pData); |
| | | } |
| | | |
| | | if (nRet != 0) { |
| | |
| | | return 0; |
| | | } |
| | | |
| | | // 扩展读取位数据 |
| | | long CPerformanceMelsec::ReadBitDataEx(const StationIdentifier& station, DeviceType enDevType, long nDevNo, long nBitCount, BitContainer& vecData) { |
| | | long nRet = ValidateStationAndSize(station, nBitCount); |
| | | if (nRet != 0) { |
| | | UpdateLastError(nRet); |
| | | return nRet; |
| | | } |
| | | |
| | | if (nDevNo % 8 != 0) { |
| | | UpdateLastError(ERROR_CODE_INVALID_PARAM); |
| | | return ERROR_CODE_INVALID_PARAM; |
| | | } |
| | | |
| | | const short nDevType = CalculateDeviceType(station, enDevType); |
| | | const long nWordCount = (nBitCount + 15) / 16; |
| | | const long nByteSize = nWordCount * sizeof(short); |
| | | |
| | | std::vector<char> vecRaw; |
| | | nRet = ReadDataEx(station, nDevType, nDevNo, nByteSize, vecRaw); |
| | | if (nRet != 0) { |
| | | return nRet; |
| | | } |
| | | |
| | | vecData.clear(); |
| | | for (long i = 0; i < nWordCount; ++i) { |
| | | short word = static_cast<unsigned char>(vecRaw[i * 2]) | |
| | | (static_cast<unsigned char>(vecRaw[i * 2 + 1]) << 8); |
| | | for (int j = 0; j < 16; ++j) { |
| | | vecData.push_back((word & (1 << j)) != 0); |
| | | if (vecData.size() >= static_cast<size_t>(nBitCount)) { |
| | | return 0; |
| | | } |
| | | } |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | // 扩展读取字数据 |
| | | long CPerformanceMelsec::ReadWordDataEx(const StationIdentifier& station, DeviceType enDevType, long nDevNo, long nWordCount, WordContainer& vecData) { |
| | | long nRet = ValidateStationAndSize(station, nWordCount); |
| | | if (nRet != 0) { |
| | | UpdateLastError(nRet); |
| | | return nRet; |
| | | } |
| | | |
| | | const short nDevType = CalculateDeviceType(station, enDevType); |
| | | const long nByteSize = nWordCount * sizeof(short); |
| | | |
| | | std::vector<char> vecRaw; |
| | | nRet = ReadDataEx(station, nDevType, nDevNo, nByteSize, vecRaw); |
| | | if (nRet != 0) { |
| | | return nRet; |
| | | } |
| | | |
| | | vecData.clear(); |
| | | for (long i = 0; i < nWordCount; ++i) { |
| | | short value = static_cast<unsigned char>(vecRaw[i * 2]) | |
| | | (static_cast<unsigned char>(vecRaw[i * 2 + 1]) << 8); |
| | | vecData.push_back(value); |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | // 扩展读取双字数据 |
| | | long CPerformanceMelsec::ReadDWordDataEx(const StationIdentifier& station, DeviceType enDevType, long nDevNo, long nDWordCount, DWordContainer& vecData) { |
| | | long nRet = ValidateStationAndSize(station, nDWordCount); |
| | | if (nRet != 0) { |
| | | UpdateLastError(nRet); |
| | | return nRet; |
| | | } |
| | | |
| | | const short nDevType = CalculateDeviceType(station, enDevType); |
| | | const long nByteSize = nDWordCount * sizeof(uint32_t); |
| | | |
| | | std::vector<char> vecRaw; |
| | | nRet = ReadDataEx(station, nDevType, nDevNo, nByteSize, vecRaw); |
| | | if (nRet != 0) { |
| | | return nRet; |
| | | } |
| | | |
| | | vecData.clear(); |
| | | for (long i = 0; i < nDWordCount; ++i) { |
| | | uint32_t val = static_cast<unsigned char>(vecRaw[i * 4 + 0]) | |
| | | (static_cast<unsigned char>(vecRaw[i * 4 + 1]) << 8) | |
| | | (static_cast<unsigned char>(vecRaw[i * 4 + 2]) << 16) | |
| | | (static_cast<unsigned char>(vecRaw[i * 4 + 3]) << 24); |
| | | vecData.push_back(val); |
| | | } |
| | | |
| | | return 0; |
| | | } |
| | | |
| | | // 扩展写数据 |
| | | long CPerformanceMelsec::WriteDataEx(const StationIdentifier& station, long nDevType, long nDevNo, const std::vector<char>& vecData) { |
| | | // 验证站点参数和数据有效性 |
| | |
| | | return nRet; |
| | | } |
| | | |
| | | // 扩展写位数据 |
| | | long CPerformanceMelsec::WriteBitDataEx(const StationIdentifier& station, DeviceType enDevType, long nDevNo, const BitContainer& vecData) { |
| | | long nRet = ValidateStationAndData(station, vecData); |
| | | if (nRet != 0) { |
| | | UpdateLastError(nRet); |
| | | return nRet; |
| | | } |
| | | |
| | | if (nDevNo % 8 != 0) { |
| | | UpdateLastError(ERROR_CODE_INVALID_PARAM); |
| | | return ERROR_CODE_INVALID_PARAM; |
| | | } |
| | | |
| | | const short nDevType = CalculateDeviceType(station, enDevType); |
| | | const size_t nWordCount = (vecData.size() + 15) / 16; |
| | | |
| | | std::vector<short> vecWordBuffer(nWordCount, 0); |
| | | for (size_t i = 0; i < vecData.size(); ++i) { |
| | | if (vecData[i]) { |
| | | vecWordBuffer[i / 16] |= (1 << (i % 16)); |
| | | } |
| | | } |
| | | |
| | | // 转换 short -> char |
| | | std::vector<char> vecByteBuffer; |
| | | vecByteBuffer.resize(nWordCount * sizeof(short)); |
| | | for (size_t i = 0; i < nWordCount; ++i) { |
| | | vecByteBuffer[i * 2] = static_cast<char>(vecWordBuffer[i] & 0xFF); |
| | | vecByteBuffer[i * 2 + 1] = static_cast<char>((vecWordBuffer[i] >> 8) & 0xFF); |
| | | } |
| | | |
| | | return WriteDataEx(station, nDevType, nDevNo, vecByteBuffer); |
| | | } |
| | | |
| | | // 扩展写字数据 |
| | | long CPerformanceMelsec::WriteWordDataEx(const StationIdentifier& station, DeviceType enDevType, long nDevNo, const WordContainer& vecData) { |
| | | long nRet = ValidateStationAndData(station, vecData); |
| | | if (nRet != 0) { |
| | | UpdateLastError(nRet); |
| | | return nRet; |
| | | } |
| | | |
| | | const short nDevType = CalculateDeviceType(station, enDevType); |
| | | std::vector<char> vecByteBuffer; |
| | | vecByteBuffer.resize(vecData.size() * sizeof(short)); |
| | | |
| | | for (size_t i = 0; i < vecData.size(); ++i) { |
| | | vecByteBuffer[i * 2] = static_cast<char>(vecData[i] & 0xFF); |
| | | vecByteBuffer[i * 2 + 1] = static_cast<char>((vecData[i] >> 8) & 0xFF); |
| | | } |
| | | |
| | | return WriteDataEx(station, nDevType, nDevNo, vecByteBuffer); |
| | | } |
| | | |
| | | // 扩展写双字数据 |
| | | long CPerformanceMelsec::WriteDWordDataEx(const StationIdentifier& station, DeviceType enDevType, long nDevNo, const DWordContainer& vecData) { |
| | | long nRet = ValidateStationAndData(station, vecData); |
| | | if (nRet != 0) { |
| | | UpdateLastError(nRet); |
| | | return nRet; |
| | | } |
| | | |
| | | const short nDevType = CalculateDeviceType(station, enDevType); |
| | | std::vector<char> vecByteBuffer; |
| | | vecByteBuffer.resize(vecData.size() * sizeof(uint32_t)); |
| | | |
| | | for (size_t i = 0; i < vecData.size(); ++i) { |
| | | vecByteBuffer[i * 4] = static_cast<char>(vecData[i] & 0xFF); |
| | | vecByteBuffer[i * 4 + 1] = static_cast<char>((vecData[i] >> 8) & 0xFF); |
| | | vecByteBuffer[i * 4 + 2] = static_cast<char>((vecData[i] >> 16) & 0xFF); |
| | | vecByteBuffer[i * 4 + 3] = static_cast<char>((vecData[i] >> 24) & 0xFF); |
| | | } |
| | | |
| | | return WriteDataEx(station, nDevType, nDevNo, vecByteBuffer); |
| | | } |
| | | |
| | | // 扩展软元件随机读取 |
| | | long CPerformanceMelsec::ReadRandomDataEx(const StationIdentifier& station, const std::vector<SoftElement>& vecSoftElements, std::vector<char>& vecData) { |
| | | if (vecSoftElements.empty()) { |