def encode(self):
     ''' Encodes response pdu
     :returns: The encoded packet message
     '''
     result = pack_bitstring(self.bits)
     packet = struct.pack(">B", len(result)) + result
     return packet
    def encode(self):
        ''' Encodes response pdu

        :returns: The encoded packet message
        '''
        result = pack_bitstring(self.bits)
        packet = struct.pack(">B", len(result)) + result
        return packet
示例#3
0
    def execute(self, *args):
        ''' Execute the diagnostic request on the given device

        :returns: The initialized response message
        '''
        #if _MCB.isListenOnly():
        register = pack_bitstring(_MCB.getDiagnosticRegister())
        return ReturnDiagnosticRegisterResponse(register)
示例#4
0
    def execute(self, *args):
        ''' Execute the diagnostic request on the given device

        :returns: The initialized response message
        '''
        #if _MCB.isListenOnly():
        register = pack_bitstring(_MCB.getDiagnosticRegister())
        return ReturnDiagnosticRegisterResponse(register)
示例#5
0
文件: events.py 项目: 4rc4n4/pymodbus
    def encode(self):
        ''' Encodes the status bits to an event message

        :returns: The encoded event message
        '''
        bits  = [False] * 3
        bits += [self.overrun, self.listen, self.broadcast, True]
        packet = pack_bitstring(bits)
        return packet
示例#6
0
    def encode(self):
        ''' Encodes the status bits to an event message

        :returns: The encoded event message
        '''
        bits = [False] * 3
        bits += [self.overrun, self.listen, self.broadcast, True]
        packet = pack_bitstring(bits)
        return packet
示例#7
0
    def encode(self):
        ''' Encodes write coils request

        :returns: The byte encoded message
        '''
        count   = len(self.values)
        self.byte_count = (count + 7) // 8
        packet  = struct.pack('>HHB', self.address, count, self.byte_count)
        packet += pack_bitstring(self.values)
        return packet
示例#8
0
    def encode(self):
        ''' Encodes write coils request

        :returns: The byte encoded message
        '''
        count = len(self.values)
        self.byte_count = (count + 7) // 8
        packet = struct.pack('>HHB', self.address, count, self.byte_count)
        packet += pack_bitstring(self.values)
        return packet
示例#9
0
文件: events.py 项目: 4rc4n4/pymodbus
    def encode(self):
        ''' Encodes the status bits to an event message

        :returns: The encoded event message
        '''
        bits = [self.read, self.slave_abort, self.slave_busy,
            self.slave_nak, self.write_timeout, self.listen]
        bits  += [True, False]
        packet = pack_bitstring(bits)
        return packet
示例#10
0
    def add_bits(self, values):
        ''' Adds a collection of bits to be encoded

        If these are less than a multiple of eight,
        they will be left padded with 0 bits to make
        it so.

        :param value: The value to add to the buffer
        '''
        value = pack_bitstring(values)
        self._payload.append(value)
示例#11
0
文件: payload.py 项目: dices/pymodbus
    def add_bits(self, values):
        """ Adds a collection of bits to be encoded

        If these are less than a multiple of eight,
        they will be left padded with 0 bits to make
        it so.

        :param value: The value to add to the buffer
        """
        value = pack_bitstring(values)
        self._payload.append(value)
示例#12
0
    def encode(self):
        ''' Encodes the status bits to an event message

        :returns: The encoded event message
        '''
        bits = [
            self.read, self.slave_abort, self.slave_busy, self.slave_nak,
            self.write_timeout, self.listen
        ]
        bits += [True, False]
        packet = pack_bitstring(bits)
        return packet
示例#13
0
    def fromCoils(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 ModiconPayloadDecoder(payload, endian)
        raise ParameterException('Invalid collection of coils supplied')
示例#14
0
文件: payload.py 项目: dices/pymodbus
    def fromCoils(klass, coils, byteorder=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 byteorder: The endianess of the payload
        :returns: An initialized PayloadDecoder
        """
        if isinstance(coils, list):
            payload = pack_bitstring(coils)
            return klass(payload, byteorder)
        raise ParameterException('Invalid collection of coils supplied')
示例#15
0
    def fromCoils(klass, coils, byteorder=Endian.Little, wordorder=Endian.Big):
        """ 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 byteorder: The endianess of the payload
        :returns: An initialized PayloadDecoder
        """
        if isinstance(coils, list):
            payload = b''
            padding = len(coils) % 8
            if padding:    # Pad zero's
                extra = [False] * padding
                coils = extra + coils
            chunks = klass.bit_chunks(coils)
            for chunk in chunks:
                payload += pack_bitstring(chunk[::-1])
            return klass(payload, byteorder)
        raise ParameterException('Invalid collection of coils supplied')
示例#16
0
    def fromCoils(klass, coils, byteorder=Endian.Little, wordorder=Endian.Big):
        """ 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 byteorder: The endianess of the payload
        :returns: An initialized PayloadDecoder
        """
        if isinstance(coils, list):
            payload = b''
            padding = len(coils) % 8
            if padding:  # Pad zero's
                extra = [False] * padding
                coils = extra + coils
            chunks = klass.bit_chunks(coils)
            for chunk in chunks:
                payload += pack_bitstring(chunk[::-1])
            return klass(payload, byteorder)
        raise ParameterException('Invalid collection of coils supplied')
示例#17
0
    def __set_bit(self, key, offset, values):
        '''

        :param key: The key prefix to use
        :param offset: The address offset to start at
        :param values: The values to set
        '''
        count = len(values)
        s = divmod(offset, self.__bit_size)[0]
        e = divmod(offset + count, self.__bit_size)[0]
        value = pack_bitstring(values)

        current = self.__get_bit_values(key, offset, count)
        current = (r or self.__bit_default for r in current)
        current = ''.join(current)
        current = current[0:offset] + value + current[offset + count:]
        final   = (current[s:s + self.__bit_size] for s in range(0, count, self.__bit_size))

        key = self.__get_prefix(key)
        request = ('%s:%s' % (key, v) for v in range(s, e + 1))
        request = dict(zip(request, final))
        self.client.mset(request)
示例#18
0
 def testBitPacking(self):
     ''' Test all string <=> bit packing functions '''
     self.assertEqual(unpack_bitstring('\x55'), self.bits)
     self.assertEqual(pack_bitstring(self.bits), '\x55')
示例#19
0
 def testBitPacking(self):
     ''' Test all string <=> bit packing functions '''
     self.assertEqual(unpack_bitstring('\x55'), self.bits)
     self.assertEqual(pack_bitstring(self.bits), '\x55')