Closed kubo39 closed 6 years ago
It is strange that client raises exception when receives error response...
ReadHoldingRegistersResponse readHoldingRegisters(ushort startingAddress, ushort quantity) { ubyte[] data = new ubyte[4]; size_t index = 0; data.write!(ushort, Endian.bigEndian)(startingAddress, &index); assert(index == 2); data.write!(ushort, Endian.bigEndian)(quantity, &index); assert(index == 4); auto length = cast(short)(1 + 1 + data.length); auto req = Request(MBAPHeader(0, PROTOCOL_ID, length, 0), ProtocolDataUnit(FunctionCode.ReadHoldingRegisters, data)); auto res =request(req); checkResponse(res, FunctionCode.ReadHoldingRegisters); // raise exception!!! ubyte byteCount = res.pdu.data.read!(ubyte, Endian.bigEndian); ushort[] registerValue = res.pdu.data.chunks(2) .map!(a => a.read!(ushort, Endian.bigEndian)) .array; return ReadHoldingRegistersResponse(res.header, res.pdu.functionCode, byteCount, registerValue); }
Using raw response type
It is strange that client raises exception when receives error response...