kubo39 / vibe-modbus

vibe-core based modbus messaging on TCP/IP implemetation
MIT License
0 stars 0 forks source link

Better handling response in clinet side #7

Closed kubo39 closed 6 years ago

kubo39 commented 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);
    }
kubo39 commented 6 years ago

Using raw response type