Exemplo n.º 1
0
    def send_msg(self, m_text, chan_index):
        """send message"""
        m_list = common.text2list(m_text)
        send_b = b''.join(map(lambda x: struct.pack('B', int(x, 16)), m_list))
        if self.is_serial_running and chan_index in [-1, 0]:
            try:
                self.serial_handle.write(send_b)
                config.MASTER_WINDOW.send_signal.emit(
                    common.format_text(m_text), 0)
            except Exception:
                print('serial send err')
                self.serial_disconnect()
        if self.is_frontend_running and chan_index in [-1, 1]:
            try:
                self.frontend_handle.sendall(send_b)
                config.MASTER_WINDOW.send_signal.emit(
                    common.format_text(m_text), 1)
            except Exception:
                print('frontend send err')
                self.frontend_disconnect()

        if self.is_server_running and chan_index in [-1, 2]:
            for client_handle, client_addr in self.client_list:
                try:
                    client_handle.sendall(send_b)
                    print('send to client', client_addr)
                except Exception:
                    traceback.print_exc()
                    client_handle.close()
                    self.client_list.remove((client_handle, client_addr))
                    print('del client', client_addr)
            config.MASTER_WINDOW.send_signal.emit(common.format_text(m_text),
                                                  2)
Exemplo n.º 2
0
def add_linkLayer(apdu_list,
                  CA_text='00',
                  SA_text='00000001',
                  logic_addr=0,
                  SA_type=0,
                  C_text='43'):
    """add linklayer"""
    SA_list = commonfun.text2list(SA_text)
    SA_list.reverse()  # 小端
    SA_text = ''.join(SA_list)
    if len(SA_text) % 2 == 1:
        SA_text += '0'
    L_text = '{0:04X}'.format(len(SA_text) // 2 + 9 + len(apdu_list))
    L_text = L_text[2:4] + L_text[0:2]
    SA_param_text = '{0:02X}'.format(((len(SA_text) // 2 - 1) & 0x0f)
                                     | ((logic_addr & 0x03) << 4)
                                     | ((SA_type & 0x03) << 6))
    hcs_clac_aera_text = L_text + C_text + SA_param_text + SA_text + CA_text
    hcs_calc = commonfun.get_fcs(commonfun.text2list(hcs_clac_aera_text))
    hcs_calc = ((hcs_calc << 8) | (hcs_calc >> 8)) & 0xffff  # 低位在前
    fcs_calc_aera_text = hcs_clac_aera_text + '{0:04X}'.format(
        hcs_calc) + commonfun.list2text(apdu_list)
    fcs_calc = commonfun.get_fcs(commonfun.text2list(fcs_calc_aera_text))
    fcs_calc = ((fcs_calc << 8) | (fcs_calc >> 8)) & 0xffff  # 低位在前

    return commonfun.format_text('68' + fcs_calc_aera_text +
                                 '{0:04X}'.format(fcs_calc) + '16')
Exemplo n.º 3
0
 def __init__(self, m_text):
     """init"""
     self.is_ssal = False
     self.is_empty_ssal = False
     self.is_full_msg = True
     self.is_linklayer_sep = False
     self.trans_res = commonfun.TransRes()
     self.source_msg = commonfun.format_text(m_text)
     m_list = commonfun.text2list(m_text)
     self.res_list, self.is_success = self.__trans_all(m_list)
     self.is_access_successed = self.get_access_res()
Exemplo n.º 4
0
    def add_msg_table_row(self, m_text, chan_index, direction):
        """add message row"""
        trans = Translate(m_text)
        brief = trans.get_brief()
        # direction = trans.get_direction()
        client_addr = trans.get_CA()
        if config.IS_FILETER_CA and client_addr != '00' and client_addr != config.COMMU.master_addr:
            print('过滤报文:CA不匹配')
            return
        server_addr = trans.get_SA()
        logic_addr = trans.get_logic_addr()
        chan_text = {0: '串口', 1: '前置机', 2: '服务器'}.get(chan_index)

        # chk to add tmn addr to table
        if direction == '←':
            for row_num in range(self.tmn_table.rowCount()):
                if server_addr == self.tmn_table.item(row_num, 1).text()\
                and logic_addr == self.tmn_table.cellWidget(row_num, 2).value()\
                and chan_index == self.tmn_table.cellWidget(row_num, 3).currentIndex():
                    break
            else:
                is_cb_checked = False if chan_index == 1 else True
                self.add_tmn_table_row(tmn_addr=server_addr, logic_addr=logic_addr,\
                                        chan_index=chan_index, is_checked=is_cb_checked)

        text_color = QtGui.QColor(220, 226, 241) if direction == '→' else\
                    QtGui.QColor(227, 237, 205) if direction == '←' else QtGui.QColor(255, 255, 255)
        row_pos = self.msg_table.rowCount()
        self.msg_table.insertRow(row_pos)

        item = QtGui.QTableWidgetItem(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        # item.setBackground(text_color)
        self.msg_table.setItem(row_pos, 0, item)

        addr_text = '{SA}:{logic}'.format(SA=server_addr, logic=logic_addr)
        item = QtGui.QTableWidgetItem(addr_text)
        # item.setBackground(text_color)
        self.msg_table.setItem(row_pos, 1, item)

        item = QtGui.QTableWidgetItem(chan_text + direction)
        item.setBackground(text_color)
        self.msg_table.setItem(row_pos, 2, item)

        item = QtGui.QTableWidgetItem(brief)
        if brief == '无效报文':
            item.setTextColor(QtCore.Qt.red)
        if brief.find('(访问失败)') == 0:
            item.setTextColor(QtGui.QColor(255, 140, 0))
        self.msg_table.setItem(row_pos, 3, item)

        msg_text = common.format_text(m_text)
        item = QtGui.QTableWidgetItem(msg_text)
        # item.setBackground(text_color)
        self.msg_table.setItem(row_pos, 4, item)

        if row_pos > config.MSG_TABLE_ROW_MAX:
            self.msg_table.removeRow(0)

        self.msg_table.scrollToBottom()

        # log
        self.msg_log.add_log(addr_text, chan_text, direction, brief, msg_text)

        service = trans.get_service()
        if service == '01' and self.is_reply_link:
            reply_apdu_text = reply.get_link_replay_apdu(trans)
            self.send_apdu(reply_apdu_text, tmn_addr=server_addr,\
                            logic_addr=logic_addr, chan_index=chan_index, C_text='01')
        if service[:2] == '88' and self.is_reply_rpt:
            reply_apdu_text = reply.get_rpt_replay_apdu(trans)
            self.send_apdu(reply_apdu_text, tmn_addr=server_addr,\
                            logic_addr=logic_addr, chan_index=chan_index, C_text='03')
        if service == '8505' and self.is_reply_split and trans.is_access_successed:
            reply_apdu_text = reply.get_rpt_replay_split(trans)
            self.send_apdu(reply_apdu_text, tmn_addr=server_addr,\
                            logic_addr=logic_addr, chan_index=chan_index, C_text='43')
Exemplo n.º 5
0
 def __init__(self, m_text):
     """init"""
     self.parse = QGDW103762Class()
     self.source_msg = commonfun.format_text(m_text)
     self.res_str, self.is_success = self.__trans_all(m_text)