def decode(cls, data): """ Extracts and decodes the first Icom command found within the supplied buffer. :param data: Series of bytes from which we must extract the incoming command. There is no guarantee that the first byte is the beginning of the transaction (i.e. there might be some trash in the beginning). :type data: array :return: Object containing the transaction and some additional control information :rtype: DecodedTransaction """ trans = bytearray(data) # Find the beginning of the transaction trans_start_index = trans.find(cls.TRANS_START) if trans_start_index == -1: return DecodedTransaction(None, 0) # Find the end of the transaction (must be after trans_start_index) trans_end_index = trans.find(cls.TRANS_END, trans_start_index) if trans_end_index == -1: return DecodedTransaction(None, 0) cmd_idx = trans_start_index + 4 # get the index of the command result_dic = dict() if trans[cmd_idx] == cls.CFM_POSITIVE: # <------------------------- positive confirm DecodedTransaction.insertPositiveCfm(result_dic) elif trans[cmd_idx] == cls.CFM_NEGATIVE: # <------------------------- negative confirm DecodedTransaction.insertNegativeCfm(result_dic) elif trans[cmd_idx] == cls.SEND_FREQ: # <------------------------- frequency freq = cls.__frequency_from_bcd_to_string(trans[(cmd_idx + 1):trans_end_index]) DecodedTransaction.insertFreq(result_dic, freq) elif trans[cmd_idx] == cls.SEND_MODE: # <------------------------- mode mode = cls.__mode_from_byte_to_string(trans[cmd_idx+1]) DecodedTransaction.insertMode(result_dic, mode) else: # <------------------------- not-supported DecodedTransaction.insertNotSupported(result_dic, misc_utils.getListInHex(trans[trans_start_index:trans_end_index+1])) # Convert to JSON string result_json = DecodedTransaction.toJson(result_dic) logger.debug("input bytes: {0}".format(misc_utils.getListInHex(bytearray(data)))) logger.debug("returns: {0}; bytes removed: {1}".format(result_json, trans_end_index+1)) # return the object with the decoded transaction and the amount of bytes that we have read from the supplied buffer(string) return DecodedTransaction(result_json, trans_end_index+1)
def __transaction(cls, command, sub_command=None, data=None): """ Assembles an Icom specific transaction ready to be send to the transceiver Protocol is the following: [preamble(0xFE), preamble(0xFE), ctrl_id, civ-address, command, sub_command, data...., 0xFD] :param command: Command code. E.g. 0x05 is set frequency) :type command: int :param sub_command: Sub-command code (optional) :type sub_command: int :param data: Additional data bytes(optional) :type data: list :return: The ready transaction bytes :rtype: list """ transaction= [0xFE, 0xFE, cls.CIV_ADDRESS, cls.CTRL_ADDRESS, command] if sub_command is not None: transaction.append(sub_command) if data is not None: transaction += data transaction.append(0xFD) logger.debug("returns: {0}".format(misc_utils.getListInHex(transaction))) return transaction
import misc_utils aa = dict() aa["dfd"] = 1 print aa.__len__() my_list = [1, 2, 3, 4] my_byte_array = bytearray(my_list) print misc_utils.getListInHex(my_byte_array)