Example #1
0
    def confirmation(self, pdu):
        """This is a request from a client."""
        if _debug: ModbusServer._debug("confirmation %r", pdu)

        # generic decoding
        mpdu = MPDU()
        mpdu.decode(pdu)
        if _debug: ModbusServer._debug("    - mpdu: %r", mpdu)

        # we don't know anything but MODBUS
        if (mpdu.mpduProtocolID != 0):
            return

        # clients shouldn't be sending exceptions
        if (mpdu.mpduFunctionCode >= 128):
            return

        # map the function code
        klass = request_types.get(mpdu.mpduFunctionCode, None)
        if not klass:
            # create an error for now
            resp = ExceptionResponse(
                mpdu.mpduFunctionCode,
                ExceptionResponse.ILLEGAL_FUNCTION,
                )

            # match the transaction information
            resp.pduDestination = mpdu.pduSource
            resp.mpduTransactionID = mpdu.mpduTransactionID
            if _debug: ModbusServer._debug("    - resp: %r", resp)

            # return the response to the device
            self.request(resp)

        req = klass()
        req.decode(mpdu)
        if _debug: ModbusServer._debug("    - req: %r", req)

        # pass it along to the application
        self.response(req)