示例#1
0
    def decode(self, event):
        """ Decodes the event message to its status bits

        :param event: The event to decode
        """
        if event != self.__encoded:
            raise ParameterException('Invalid decoded value')
示例#2
0
    def __delitem__(self, slave):
        """ Wrapper used to access the slave context

        :param slave: The slave context to remove
        """
        if not self.single and (0xf7 >= slave >= 0x00):
            del self.__slaves[slave]
        else:
            raise ParameterException('slave index out of range')
示例#3
0
    def __setitem__(self, slave, context):
        """ Used to set a new slave context

        :param slave: The slave context to set
        :param context: The new context to set for this slave
        """
        if self.single:
            slave = Defaults.UnitId
        if 0xf7 >= slave >= 0x00:
            self.__slaves[slave] = context
        else:
            raise ParameterException('slave index out of range')
示例#4
0
    def __getitem__(self, slave):
        """ Used to get access to a slave context

        :param slave: The slave context to get
        :returns: The requested slave context
        """
        if self.single:
            slave = Defaults.UnitId
        if slave in self.__slaves:
            return self.__slaves.get(slave)
        else:
            raise ParameterException(
                'slave does not exist, or is out of range'
            )
示例#5
0
    def from_coils(cls, coils, endian=Endian.Little):
        """ Initialize a payload decoder with the result of
        reading a collection of coils from a modbus device.

        The coils are treated as a list of bit(boolean) values.

        :param coils: The coil results to initialize with
        :param endian: The endianess of the payload
        :returns: An initialized PayloadDecoder
        """
        if isinstance(coils, list):
            payload = pack_bitstring(coils)
            return cls(payload, endian)
        raise ParameterException('Invalid collection of coils supplied')
示例#6
0
    def __implementation(method):
        """ Returns the requested framer

        :method: The serial framer to instantiate
        :returns: The requested serial framer
        """
        method = method.lower()
        if method == 'ascii':
            return ModbusAsciiFramer(ClientDecoder())
        elif method == 'rtu':
            return ModbusRtuFramer(ClientDecoder())
        elif method == 'binary':
            return ModbusBinaryFramer(ClientDecoder())
        elif method == 'socket':
            return ModbusSocketFramer(ClientDecoder())
        raise ParameterException('Invalid framer method requested')
示例#7
0
    def from_registers(cls, registers, endian=Endian.Little):
        """ Initialize a payload decoder with the result of
        reading a collection of registers from a modbus device.

        The registers are treated as a list of 2 byte values.
        We have to do this because of how the data has already
        been decoded by the rest of the library.

        :param registers: The register results to initialize with
        :param endian: The endianess of the payload
        :returns: An initialized PayloadDecoder
        """
        if isinstance(registers, list):  # repack into flat binary
            payload = b''.join(pack('>H', x) for x in registers)
            return cls(payload, endian)
        raise ParameterException('Invalid collection of registers supplied')
示例#8
0
    def __init__(self, values):
        """ Initializes the datastore

        Using the input values we create the default
        datastore value and the starting address

        :param values: Either a list or a dictionary of values
        """
        if isinstance(values, dict):
            self.values = values
        elif isinstance(values, Iterable):
            self.values = dict(enumerate(values))
        else:
            raise ParameterException(
                'Values for datastore must be a list or dictionary')
        self.default_value = self.values.values().__next__().__class__()
        self.address = self.values.keys().__next__()