def create_package(exec_str, serial_num=0, servive_id=0x01): package_bytes = bytearray() if config.service_id_enable: service_byte_len = 1 else: service_byte_len = 0 # add protocol id package_bytes.append(ONLINE_CMD_ID) if config.service_id_enable: # add servive id package_bytes.append(servive_id) # add serial num, 2 bytes if config.serial_num_len == 2: serial_num_bytes = serial_num.to_bytes(2, "little") package_bytes += serial_num_bytes elif config.serial_num_len == 1: package_bytes.append(serial_num) # add script bytes if config.script_package_type == config.SCRIPT_PACKAGE_TYPE_BIGSTRING: strbytes = __bigstring_package(exec_str) elif config.script_package_type == config.SCRIPT_PACKAGE_TYPE_STRING: strbytes = __string_package(exec_str) elif config.script_package_type == config.SCRIPT_PACKAGE_TYPE_STRING0: strbytes = __string0_package(exec_str) package_bytes += strbytes console.debug("whole package %s" % package_bytes) return package_bytes
def write(self, frame): if not self.ser.is_open: return console.debug("phy write frame is: %s" % frame) self.write_lock.acquire() self.ser.write(frame) self.write_lock.release()
def parse_data_stream(self, data, d_info=None): self.__fsm(data) if self.frame_list: console.debug("%s" % self.frame_list) for item in self.frame_list: for process in self.frame_process_list: ret = process.process(item, d_info) # process result if ret: pass self.frame_list.remove(item)
def get_value(self, tag, para=None): para = tuple(para) if tag in self.data_tag: if not self.data_tag[tag]['obj'].get_subscribe_flag(para): self.data_tag[tag]['obj'].set_data_new_flag(False, para) console.debug( self.create_subcribe_frame(self.data_tag[tag], para)) self.protocol.send_protocol( self.create_subcribe_frame(self.data_tag[tag], para)) if self.data_tag[tag]['obj'].wait_data_new(para): self.data_tag[tag]['obj'].set_subscribe_flag(True, para) return self.data_tag[tag]['obj'].get_value(para) else: return self.data_tag[tag]['obj'].get_default_value(para) return self.data_tag[tag]['obj'].get_value(para) else: return None
def __fsm(self, stream): # this variable indicate the number of datas should be read next time ret_num = 1 for c in stream: receive_frame = None if len(self.recv_buffer) > self.FRAME_MAX_LEN: self.fsm_state = self.FSM_S_HEAD if (F0F7_PROTOCOL_HEAD == c): self.fsm_state = self.FSM_S_DATA self.recv_buffer = bytearray() self.fsm_state = self.FSM_S_DATA elif (self.fsm_state == self.FSM_S_DATA) and (F0F7_PROTOCOL_END == c): check_sum = 0 for i in range(len(self.recv_buffer) - 1): check_sum = check_sum + self.recv_buffer[i] check_sum = check_sum & 0x7F if check_sum == self.recv_buffer[-1]: receive_frame = self.recv_buffer[:-1] self.fsm_state = self.FSM_S_HEAD elif (self.FSM_S_DATA == self.fsm_state): self.recv_buffer.append(c) if receive_frame: console.debug("F0F7 received frame %s" % receive_frame) # frame_list do not contain the protocol structure datas self.frame_list.append(receive_frame) if len(self.frame_list) > self.FRAME_MAX_NUM: console.warning("F0F7 frame list overflow, clear") self.frame_list.clear() return ret_num
def send_protocol(self, package): console.debug("send_protocol %s" % package) if self.link: self.link.write(create_frame(package))
def __fsm(self, stream): # this variable indicate the number of datas should be read next time ret_num = 1 for c in stream: receive_frame = None if len(self.recv_buffer) > self.FRAME_MAX_LEN: self.fsm_state = self.S_HEAD if (self.FSM_S_HEAD == self.fsm_state): if (F3F4_PROTOCOL_HEAD == c): self.recv_buffer = bytearray() self.recv_buffer.append(c) self.fsm_state = self.FSM_S_HEAD_CHECK elif (self.FSM_S_HEAD_CHECK == self.fsm_state): self.recv_buffer.append(c) self.head_checksum = c self.fsm_state = self.FSM_S_LEN_1 elif (self.FSM_S_LEN_1 == self.fsm_state): self.recv_buffer.append(c) self.data_region_len = c self.fsm_state = self.FSM_S_LEN_2 elif (self.FSM_S_LEN_2 == self.fsm_state): self.recv_buffer.append(c) self.data_region_len += c * 0xFF head_checksum = (self.recv_buffer[0] + self.recv_buffer[2] + self.recv_buffer[3]) & 0xFF if (head_checksum == self.head_checksum): self.fsm_state = self.FSM_S_DATA self.recv_len = 0 # head check successed, read the reserved datas # 2 = checksum(1) + 0xF4(1) ret_num = self.data_region_len + 2 else: self.fsm_state = self.FSM_S_HEAD elif (self.FSM_S_DATA == self.fsm_state): self.recv_buffer.append(c) self.recv_len += 1 if (self.data_region_len == self.recv_len): self.fsm_state = self.FSM_S_DATA_CHECK elif (self.FSM_S_DATA_CHECK == self.fsm_state): self.recv_buffer.append(c) data_checksum = 0 for i in self.recv_buffer[4:-1]: data_checksum += i data_checksum = data_checksum & 0xFF if (data_checksum == self.recv_buffer[-1]): self.fsm_state = self.FSM_S_END else: self.fsm_state = self.FSM_S_HEAD elif (self.FSM_S_END == self.fsm_state): self.recv_buffer.append(c) if (COMMON_PROTOCOL_END == c): receive_frame = self.recv_buffer[:] self.fsm_state = self.FSM_S_HEAD if receive_frame: console.debug("F3F4 received frame %s" % receive_frame) # frame_list do not contain the protocol structure datas self.frame_list.append(receive_frame[4:-2]) if len(self.frame_list) > self.FRAME_MAX_NUM: console.warning("F3F4 frame list overflow, clear") self.frame_list.clear() return ret_num
def process(self, frame, d_info=None): if frame[0] == 0x29: self.__process_subscribe(frame[1:]) elif frame[0] == 0x28: console.debug("process 0x28 frame %s" % frame) self.__process_realtime(frame[1:])