예제 #1
0
    def encode(self):
        ''' Encodes the response

        :returns: The byte encoded message
        '''
        ############################
        # Packet Start Sequence
        ############################
        self.frame = struct.pack('>BB', 0x10, 0x02)  #DLE STX

        ############################
        # Packet Header Information
        ############################
        data = struct.pack('>BBBBHB', self.dest, self.src, self.cmd, self.sts,
                           self.transaction_id)

        if (self.Address.subElement > 0):
            elementSize = SUBELEMENT_SIZE[self.Address.fileType]
            formatStr = '<' + SUBELEMENT_STRUCT[self.Address.fileType]
        else:
            elementSize = ELEMENT_SIZE[self.Address.fileType]
            formatStr = '<' + ELEMENT_STRUCT[self.Address.fileType]

        for record in self.records:
            data += struct.pack(formatStr, record)

        #######################################
        # Calculate CRC before escaping DLE's
        #######################################
        crc = utilities.computeCRC(data)

        ####################################
        # Escape any DLE's ('\x10') in data
        ####################################
        start = 0
        while (data.find('\x10', start, len(data)) != -1):
            i = data.find('\x10', start, len(data))
            data = data[:i] + '\x10' + data[i:]
            start = i + 2

        self.frame += data

        ###################################
        # Packet End
        ###################################
        self.frame += struct.pack('>BB', 0x10, 0x03)  #DLE ETX
        self.frame += struct.pack('>H', crc)  #crc

        return self.frame
예제 #2
0
    def encode(self):
        """ Encodes the response

        :returns: The byte encoded message
        """
        ############################
        # Packet Start Sequence
        ############################
        self.frame = struct.pack(">BB", 0x10, 0x02)  # DLE STX

        ############################
        # Packet Header Information
        ############################
        data = struct.pack(">BBBBHB", self.dest, self.src, self.cmd, self.sts, self.transaction_id)

        if self.Address.subElement > 0:
            elementSize = SUBELEMENT_SIZE[self.Address.fileType]
            formatStr = "<" + SUBELEMENT_STRUCT[self.Address.fileType]
        else:
            elementSize = ELEMENT_SIZE[self.Address.fileType]
            formatStr = "<" + ELEMENT_STRUCT[self.Address.fileType]

        for record in self.records:
            data += struct.pack(formatStr, record)

        #######################################
        # Calculate CRC before escaping DLE's
        #######################################
        crc = utilities.computeCRC(data)

        ####################################
        # Escape any DLE's ('\x10') in data
        ####################################
        start = 0
        while data.find("\x10", start, len(data)) != -1:
            i = data.find("\x10", start, len(data))
            data = data[:i] + "\x10" + data[i:]
            start = i + 2

        self.frame += data

        ###################################
        # Packet End
        ###################################
        self.frame += struct.pack(">BB", 0x10, 0x03)  # DLE ETX
        self.frame += struct.pack(">H", crc)  # crc

        return self.frame
예제 #3
0
    def encode(self):
        """ Encodes the request packet

        :return: The encoded packet
        """

        if not self.skip_encode:
            ############################
            # Packet Start Sequence
            ############################
            self.packet = struct.pack(">BB", 0x10, 0x02)  # DLE STX

            ############################
            # Packet Header Information
            ############################
            data = struct.pack(">BBBBHB", self.dest, self.src, self.cmd, self.sts, self.transaction_id, self.function)

            if self.Address.subElement > 0:
                elementSize = SUBELEMENT_SIZE[self.Address.fileType] * self.size
            else:
                elementSize = ELEMENT_SIZE[self.Address.fileType] * self.size

            data += struct.pack(">B", elementSize)

            ###################################################
            # Packet Address Information
            # Note: Use Little Endian format if using 2 bytes
            ###################################################
            if self.Address.fileNumber > 254:
                data += struct.pack(">B", 0xFF)
                data += struct.pack("<H", self.Address.fileNumber)

            else:
                data += struct.pack(">B", self.Address.fileNumber)

            data += struct.pack(">B", self.Address.fileType)

            if self.Address.eleNumber > 254:
                data += struct.pack(">B", 0xFF)
                data += struct.pack("<H", self.Address.eleNumber)
            else:
                data += struct.pack(">B", self.Address.eleNumber)

            if self.Address.subElement > 254:
                data += struct.pack(">B", 0xFF)
                data += struct.pack("<H", self.Address.subElement)
            else:
                data += struct.pack(">B", self.Address.subElement)

            if self.Address.bitNumber != None:
                mask = 0xFFFF & 2 ** self.Address.bitNumber
                data += struct.pack("<H", mask)

            ##############################################
            # Add Data Values using Little Endian format
            ##############################################
            if self.Address.subElement > 0:
                formatStr = "<" + SUBELEMENT_STRUCT[self.Address.fileType]
            else:
                formatStr = "<" + ELEMENT_STRUCT[self.Address.fileType]

            for value in self.values:
                if self.Address.subElement > 0:
                    data += struct.pack(formatStr, value)
                else:  # Everything else is 16 bits
                    data += struct.pack(formatStr, value)
            #######################################
            # Calculate CRC before escaping DLE's
            #######################################
            crc = utilities.computeCRC(data)

            ####################################
            # Escape any DLE's ('\x10') in data
            ####################################
            start = 0
            while data.find("\x10", start, len(data)) != -1:
                i = data.find("\x10", start, len(data))
                data = data[:i] + "\x10" + data[i:]
                start = i + 2

            self.packet += data

            ###################################
            # Packet End
            ###################################
            self.packet += struct.pack(">BB", 0x10, 0x03)  # DLE ETX
            self.packet += struct.pack(">H", crc)

        else:
            self.packet = packet

        return self.packet
예제 #4
0
    def encode(self):
        ''' Encodes the request packet

        :return: The encoded packet
        '''

        if (not self.skip_encode):
            ############################
            # Packet Start Sequence
            ############################
            self.packet = struct.pack('>BB', 0x10, 0x02)  #DLE STX

            ############################
            # Packet Header Information
            ############################
            data = struct.pack('>BBBBHB', self.dest, self.src, self.cmd,
                               self.sts, self.transaction_id, self.function)

            if (self.Address.subElement > 0):
                elementSize = SUBELEMENT_SIZE[
                    self.Address.fileType] * self.size
            else:
                elementSize = ELEMENT_SIZE[self.Address.fileType] * self.size

            data += struct.pack('>B', elementSize)

            ###################################################
            # Packet Address Information
            # Note: Use Little Endian format if using 2 bytes
            ###################################################
            if (self.Address.fileNumber > 254):
                data += struct.pack('>B', 0xFF)
                data += struct.pack('<H', self.Address.fileNumber)

            else:
                data += struct.pack('>B', self.Address.fileNumber)

            data += struct.pack('>B', self.Address.fileType)

            if (self.Address.eleNumber > 254):
                data += struct.pack('>B', 0xFF)
                data += struct.pack('<H', self.Address.eleNumber)
            else:
                data += struct.pack('>B', self.Address.eleNumber)

            if (self.Address.subElement > 254):
                data += struct.pack('>B', 0xFF)
                data += struct.pack('<H', self.Address.subElement)
            else:
                data += struct.pack('>B', self.Address.subElement)

            if (self.Address.bitNumber != None):
                mask = 0xFFFF & 2**self.Address.bitNumber
                data += struct.pack('<H', mask)

            ##############################################
            # Add Data Values using Little Endian format
            ##############################################
            if (self.Address.subElement > 0):
                formatStr = '<' + SUBELEMENT_STRUCT[self.Address.fileType]
            else:
                formatStr = '<' + ELEMENT_STRUCT[self.Address.fileType]

            for value in self.values:
                if (self.Address.subElement > 0):
                    data += struct.pack(formatStr, value)
                else:  # Everything else is 16 bits
                    data += struct.pack(formatStr, value)
            #######################################
            # Calculate CRC before escaping DLE's
            #######################################
            crc = utilities.computeCRC(data)

            ####################################
            # Escape any DLE's ('\x10') in data
            ####################################
            start = 0
            while (data.find('\x10', start, len(data)) != -1):
                i = data.find('\x10', start, len(data))
                data = data[:i] + '\x10' + data[i:]
                start = i + 2

            self.packet += data

            ###################################
            # Packet End
            ###################################
            self.packet += struct.pack('>BB', 0x10, 0x03)  #DLE ETX
            self.packet += struct.pack('>H', crc)

        else:
            self.packet = packet

        return self.packet