| | |
| | | #define PLC_CMD_BIT_COUNT 2 // 总共几个命令位(B6CD3=Start, B6CD4=Stop) |
| | | |
| | | // === PLC 应答输出配置(PC -> PLC) === |
| | | #define PLC_ACK_MAX_LIFE 5 // PLC响应信号最大保留周期数(每周期为 m_nIntervalMs 毫秒) |
| | | #define PLC_ACK_MAX_LIFE 25 // PLC响应信号最大保留周期数(每周期为 m_nIntervalMs 毫秒) |
| | | #define PLC_ACK_BASE_BIT 0x1060 // PLC应答起始地址(B1060表示B6CD3的应答;B1061表示B6CD4的应答) |
| | | |
| | | // === PLC软元件类型宏(用于应答、数据写入)=== |
| | |
| | | // === PLC结果寄存器地址配置 === |
| | | #define PLC_RESULT_ADDR_START 0x37B0 // PLC结果寄存器起始地址(如W37B0) |
| | | #define PLC_RESULT_ADDR_COUNT 4 // 结果寄存器数量(如W37B0, W37B2, W37B4, W37B6) |
| | | |
| | | // === PLC 产品ID配置(PLC -> PC)=== |
| | | #define PLC_PRODUCT_ID_ADDR 0x1B160 // 产品ID起始地址 (W1B160) |
| | | #define PLC_PRODUCT_ID_WORDS 10 // 产品ID长度(10个Word) |
| | | |
| | | #define IS_RISING_EDGE(prev, curr) (!(prev) && (curr)) |
| | | |
| | |
| | | m_vecAckSent[i] = true; |
| | | m_vecAckCounter[i] = 0; |
| | | } |
| | | |
| | | std::string strProductID; |
| | | if (ReadProductID(strProductID)) { |
| | | CString msg; |
| | | msg.Format(_T("读取到产品ID:%s"), strProductID); |
| | | LOG_MSG(msg, LOG_TYPE_SUCCESS); |
| | | } |
| | | } |
| | | break; |
| | | |
| | |
| | | } |
| | | |
| | | for (int i = 0; i < PLC_RESULT_ADDR_COUNT; ++i) { |
| | | // 放大100倍并四舍五入,转为PLC整数 |
| | | uint16_t nScaled = static_cast<uint16_t>(std::round(values[i] * 100.0)); |
| | | WordContainer vec = { nScaled }; |
| | | // 放大1000倍并四舍五入,转为PLC整数 |
| | | int32_t nScaled = static_cast<int32_t>(std::round(values[i] * 1000.0)); |
| | | DWordContainer vec = { static_cast<uint32_t>(nScaled) }; |
| | | |
| | | short nTargetAddr = PLC_RESULT_ADDR_START + i * 2; |
| | | int ret = m_pPlc->WriteWordDataEx(m_station, PLC_WORD_DEVICE_TYPE, nTargetAddr, vec); |
| | | int ret = m_pPlc->WriteDWordDataEx(m_station, PLC_WORD_DEVICE_TYPE, nTargetAddr, vec); |
| | | if (ret != 0) { |
| | | CString msg; |
| | | msg.Format(_T("写入OUT%d到地址%d失败,值=%.2f"), i + 1, nTargetAddr, values[i]); |
| | |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | bool CPLCSignalListener::ReadProductID(std::string& strProductID) |
| | | { |
| | | if (!m_pPlc || !m_bConnected) { |
| | | LOG_MSG(_T("PLC未连接或未初始化,无法读取产品ID。"), LOG_TYPE_ERROR); |
| | | return false; |
| | | } |
| | | |
| | | WordContainer vec; |
| | | int ret = m_pPlc->ReadWordDataEx(m_station, PLC_WORD_DEVICE_TYPE, PLC_PRODUCT_ID_ADDR, PLC_PRODUCT_ID_WORDS, vec); |
| | | if (ret != 0 || vec.size() != PLC_PRODUCT_ID_WORDS) { |
| | | CString msg; |
| | | msg.Format(_T("读取产品ID失败,错误码=%d"), ret); |
| | | LOG_MSG(msg, LOG_TYPE_ERROR); |
| | | return false; |
| | | } |
| | | |
| | | strProductID.clear(); |
| | | strProductID.reserve(PLC_PRODUCT_ID_WORDS * 2); |
| | | for (auto w : vec) { |
| | | char c1 = static_cast<char>(w & 0xFF); // 低字节 |
| | | char c2 = static_cast<char>((w >> 8) & 0xFF); // 高字节 |
| | | |
| | | if (c1 == '\0') { |
| | | break; |
| | | } |
| | | strProductID.push_back(c1); |
| | | |
| | | if (c2 == '\0') { |
| | | break; |
| | | } |
| | | strProductID.push_back(c2); |
| | | } |
| | | |
| | | return true; |
| | | } |