Beispiel #1
0
    def recv_data(self, data):
        tmp_str = ''
        msg_valid = True
        for i in range(len(data)):
            tmp_str += data[2 * i:2 * (i + 1)] + ' '
        print time.strftime("%H:%M:%S",
                            time.localtime()) + ' Recv: ' + tmp_str.upper()

        self.buf_data = str_to_hex(data, 2)
        for i in range(len(self.buf_data)):
            if self.buf_data[i] == 0x68:
                break
        del self.buf_data[:i]
        list_len = len(self.buf_data)
        if list_len == 14 or list_len == 16:
            checksum = calc_checksum(self.buf_data[:list_len - 2])
            if self.buf_data[7] != 0x68 or self.buf_data[
                    8] & 0x40 or checksum != self.buf_data[
                        list_len - 2] or self.buf_data[list_len - 1] != 0x16:
                print '提示: 解析数据报文失败!'
                msg_valid = False
        else:
            print '提示: 解析数据报文失败!'
            msg_valid = False

        list_log = []
        list_log.extend([time.strftime("%H:%M:%S", time.localtime())])  #发生时间
        list_log.extend(['接收'])  #传输方向
        if msg_valid:
            DI = ''
            if list_len == 14:
                for i in range(2):
                    DI += '%02X' % ((self.buf_data[11 - i] - 0x33) & 0xff)
            else:
                for i in range(4):
                    DI += '%02X' % ((self.buf_data[13 - i] - 0x33) & 0xff)
            list_log.extend(['\'' + DI + '\''])  #数据标识
        else:
            list_log.extend([''])
        list_log.extend([tmp_str.upper()])  #数据报文
        addr = ''
        for i in range(6):
            addr += "%02X" % self.buf_data[6 - i]

        self.save_log(addr, list_log)
        return msg_valid
    def recv_data(self, data):
        tmp_str = ''
        msg_valid = True
        for i in range(len(data)):
            tmp_str += data[2*i: 2*(i+1)] + ' '
        print time.strftime("%H:%M:%S", time.localtime()) + ' Recv: ' + tmp_str.upper()

        self.buf_data = str_to_hex(data, 2)
        for i in range(len(self.buf_data)):
            if self.buf_data[i] == 0x68:
                break
        del self.buf_data[:i]
        list_len = len(self.buf_data)
        if list_len == 14 or list_len == 16:
            checksum = calc_checksum(self.buf_data[:list_len - 2])
            if self.buf_data[7] != 0x68 or self.buf_data[8] & 0x40 or checksum != self.buf_data[list_len - 2] or self.buf_data[list_len - 1] != 0x16:
                print '提示: 解析数据报文失败!'
                msg_valid = False
        else:
            print '提示: 解析数据报文失败!'
            msg_valid = False

        list_log = []
        list_log.extend([time.strftime("%H:%M:%S", time.localtime())]) #发生时间
        list_log.extend(['接收']) #传输方向
        if msg_valid:
            DI = ''
            if list_len == 14:
                for i in range(2):
                    DI += '%02X' % ((self.buf_data[11 - i] - 0x33) & 0xff)
            else:
                for i in range(4):
                    DI += '%02X' % ((self.buf_data[13 - i] - 0x33) & 0xff)
            list_log.extend(['\'' + DI + '\'']) #数据标识
        else:
            list_log.extend([''])
        list_log.extend([tmp_str.upper()]) #数据报文
        addr = ''
        for i in range(6):
            addr += "%02X" % self.buf_data[6-i]

        self.save_log(addr, list_log)
        return msg_valid
    def send_data(self):
        send_data = []
        send_data.extend(self.buf_data[:8]) #拷贝电表地址

        DI = 0
        list_DI = []
        data_len = self.buf_data[9]
        for i in range(data_len):
            list_DI.extend([self.buf_data[10 + i]])
            self.buf_data[10 + i] = (self.buf_data[10 + i] - 0x33) & 0xff
            DI += self.buf_data[10 + i] << (i*8)

        tmp_list = []
        for i in range(len(protocol_list)):
            if data_len == 2 and protocol_list[i][0] != 'DLT645-1997.csv':
                continue
            if data_len == 4 and protocol_list[i][0] != 'DLT645-2007.csv':
                continue

            for j in range(2, len(protocol_list[i])):
                if DI != int('0x' + protocol_list[i][j][0], 16):
                    continue
                tmp_list = protocol_list[i][j]
                break
            break

        if len(tmp_list) == 0:
            if data_len == 2:
                send_data.extend([0xC1]) #97表异常回复控制字
                print 'Error: DLT645-1997.csv规约库没有数据标识 %04X.' % DI
            elif data_len == 4:
                send_data.extend([0xD1]) #07表异常回复控制字
                print 'Error: DLT645-2007.csv规约库没有数据标识 %08X.' % DI
            send_data.extend([0x01]) #Len
            send_data.extend([0x01]) #ERR
        else:
            fmt = tmp_list[1]
            size = int(tmp_list[2], 10)
            unit = tmp_list[3]
            name = tmp_list[5]
            data = tmp_list[6]

            if data_len == 2:
                send_data.extend([0x81])
            else:
                send_data.extend([0x91])
            send_data.extend([size + data_len])
            send_data.extend(list_DI)

            tmp_len = 0
            start_pos = 0
            while tmp_len < size:
                end_pos = data.find(';', start_pos)
                if end_pos == -1:
                    break
                tmp_str = data[start_pos:end_pos]
                tmp = str_to_hex(tmp_str, 2)
                tmp.reverse()
                send_data.extend(tmp)
                start_pos = end_pos + 1
                tmp_len += len(tmp)

            for i in range(size):
                send_data[10+data_len+i] = (send_data[10+data_len+i] + 0x33) & 0xff

        send_data.extend([calc_checksum(send_data)])
        send_data.extend([0x16])

        ret = ''
        for i in range(len(send_data)):
            ret += '%02X' % send_data[i] + ' '

        print time.strftime("%H:%M:%S", time.localtime()) + ' Send: ' + ret

        list_log = []
        list_log.extend([time.strftime("%H:%M:%S", time.localtime())])
        list_log.extend(['发送'])
        if len(tmp_list) == 0:
            list_log.extend([''])
        else:
            if data_len == 2:
                list_log.extend(['\'' + str('%04X' % DI) + '\'']) #数据标识
            else:
                list_log.extend(['\'' + str('%08X' % DI) + '\'']) #数据标识
        list_log.extend([ret])
        if len(tmp_list) == 0:
            list_log.extend(['异常应答'])
        else:
            list_log.extend([tmp_list[5] + '(' + tmp_list[1] + '): ' + tmp_list[6]])
        addr = ''
        for i in range(6):
            addr += "%02X" % send_data[6-i]
        self.save_log(addr, list_log)

        ret = ret.replace(' ', '')
        ret = ret.decode('hex')
        return ret
Beispiel #4
0
    def send_data(self):
        send_data = []
        send_data.extend(self.buf_data[:8])  #拷贝电表地址

        DI = 0
        list_DI = []
        data_len = self.buf_data[9]
        for i in range(data_len):
            list_DI.extend([self.buf_data[10 + i]])
            self.buf_data[10 + i] = (self.buf_data[10 + i] - 0x33) & 0xff
            DI += self.buf_data[10 + i] << (i * 8)

        tmp_list = []
        for i in range(len(protocol_list)):
            if data_len == 2 and protocol_list[i][0] != 'DLT645-1997.csv':
                continue
            if data_len == 4 and protocol_list[i][0] != 'DLT645-2007.csv':
                continue

            for j in range(2, len(protocol_list[i])):
                if DI != int('0x' + protocol_list[i][j][0], 16):
                    continue
                tmp_list = protocol_list[i][j]
                break
            break

        if len(tmp_list) == 0:
            if data_len == 2:
                send_data.extend([0xC1])  #97表异常回复控制字
                print 'Error: DLT645-1997.csv规约库没有数据标识 %04X.' % DI
            elif data_len == 4:
                send_data.extend([0xD1])  #07表异常回复控制字
                print 'Error: DLT645-2007.csv规约库没有数据标识 %08X.' % DI
            send_data.extend([0x01])  #Len
            send_data.extend([0x01])  #ERR
        else:
            fmt = tmp_list[1]
            size = int(tmp_list[2], 10)
            unit = tmp_list[3]
            name = tmp_list[5]
            data = tmp_list[6]

            if data_len == 2:
                send_data.extend([0x81])
            else:
                send_data.extend([0x91])
            send_data.extend([size + data_len])
            send_data.extend(list_DI)

            tmp_len = 0
            start_pos = 0
            while tmp_len < size:
                end_pos = data.find(';', start_pos)
                if end_pos == -1:
                    break
                tmp_str = data[start_pos:end_pos]
                tmp = str_to_hex(tmp_str, 2)
                tmp.reverse()
                send_data.extend(tmp)
                start_pos = end_pos + 1
                tmp_len += len(tmp)

            for i in range(size):
                send_data[10 + data_len +
                          i] = (send_data[10 + data_len + i] + 0x33) & 0xff

        send_data.extend([calc_checksum(send_data)])
        send_data.extend([0x16])

        ret = ''
        for i in range(len(send_data)):
            ret += '%02X' % send_data[i] + ' '

        print time.strftime("%H:%M:%S", time.localtime()) + ' Send: ' + ret

        list_log = []
        list_log.extend([time.strftime("%H:%M:%S", time.localtime())])
        list_log.extend(['发送'])
        if len(tmp_list) == 0:
            list_log.extend([''])
        else:
            if data_len == 2:
                list_log.extend(['\'' + str('%04X' % DI) + '\''])  #数据标识
            else:
                list_log.extend(['\'' + str('%08X' % DI) + '\''])  #数据标识
        list_log.extend([ret])
        if len(tmp_list) == 0:
            list_log.extend(['异常应答'])
        else:
            list_log.extend(
                [tmp_list[5] + '(' + tmp_list[1] + '): ' + tmp_list[6]])
        addr = ''
        for i in range(6):
            addr += "%02X" % send_data[6 - i]
        self.save_log(addr, list_log)

        ret = ret.replace(' ', '')
        ret = ret.decode('hex')
        return ret