| 24 | } |
| 25 | |
| 26 | std::vector<uint8_t> VFDSimulator::processModbusMessage(const std::vector<uint8_t>& request) { |
| 27 | if (request.size() < 4) { |
| 28 | log("Invalid message: too short"); |
| 29 | return {}; |
| 30 | } |
| 31 | |
| 32 | // Validate message format and CRC |
| 33 | if (!validateModbusMessage(request)) { |
| 34 | log("Invalid message: CRC or format error"); |
| 35 | return {}; |
| 36 | } |
| 37 | |
| 38 | uint8_t addr = request[0]; |
| 39 | if (addr != _modbus_addr) { |
| 40 | // Not for this device, ignore |
| 41 | return {}; |
| 42 | } |
| 43 | |
| 44 | uint8_t function = request[1]; |
| 45 | const uint8_t* data = &request[2]; |
| 46 | size_t data_length = request.size() - 4; // Exclude addr, function, and 2-byte CRC |
| 47 | |
| 48 | log("Processing command - Function: 0x" + std::to_string(function) + ", Data length: " + std::to_string(data_length)); |
| 49 | |
| 50 | std::vector<uint8_t> response; |
| 51 | |
| 52 | switch (function) { |
| 53 | // Huanyang does not follow the standard Modbus command set |
| 54 | case 1: |
| 55 | response = handleHuanyangCmd1(data, data_length); |
| 56 | break; |
| 57 | case 3: |
| 58 | response = handleHuanyangCmd3(data, data_length); |
| 59 | break; |
| 60 | case 4: |
| 61 | response = handleHuanyangCmd4(data, data_length); |
| 62 | break; |
| 63 | case 5: |
| 64 | response = handleHuanyangCmd5(data, data_length); |
| 65 | break; |
| 66 | default: |
| 67 | log("Unsupported function: 0x" + std::to_string(function)); |
| 68 | response = createModbusError(function, 0x01); // Illegal function |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | return response; |
| 73 | } |
| 74 | |
| 75 | std::vector<uint8_t> VFDSimulator::handleHuanyangCmd1(const uint8_t* data, size_t length) { |
| 76 | if (length < 4) { |