def receive_data(self, run_event, run_services, terminate_all) -> None: if not self.ser.isOpen(): log.error("Connection dropped, please check serial") else: ser_buffer = "" while run_event.is_set(): try: c = self.ser.read( ) # attempt to read a character from Serial c = c.decode('utf-8', errors='ignore') if len(c) == 0: # was anything read? pass else: if c == '\n' or c == '\n': if len(ser_buffer) > 0: ser_buffer += '\n' run_services(ser_buffer) if self.debug: log.debug( "DEBUG: Complete message from serial: {}\n" .format(ser_buffer)) ser_buffer = "" else: ser_buffer += c except serial.SerialTimeoutException: continue # Go to next iteration in case of serial timeout except serial.SerialException as e: log.error( "Caught SerialException in serial read: {}. Listener Thread will now stop" .format(e)) terminate_all() except Exception as e: sys.stdout.write( "Caught exception: {} Listener Thread will NOT stop". format(e))
def process_response(self, message: str) -> bool: """ This function is taking an input, validating by comparing an service tag and id and extracts values A response for a message is something like "@E,e,2,{3000:110,3100:120}" :param message: :return: """ if message[3] != self.tag_response: # We have received a message but it is not an ir reporting event log.error("Unable to process received message: {}".format(message)) else: for attempt in range(1, self.max_retries + 1): try: if attempt == self.max_retries - 1: log.error( "Unable to process received message: {}".format( message)) sys.exit() else: ir_values = message[message.index("{") + 1:message.index("}")].split(",") self.ir_left_value = int(ir_values[0]) self.ir_center_value = int(ir_values[1]) self.ir_right_value = int(ir_values[2]) self.ir_all_values = [ self.ir_left_value, self.ir_center_value, self.ir_right_value ] if self.debug: print("Message: {}".format(message)) return True except ValueError: pass
def process_response(self, message: str) -> None: """ Received message is being processed and displayed to the console using logging :param message: :return: """ if message[3] != self.tag: log.error("Unable to process received message: {}".format(message)) else: self.system_version = message[5:-1]
def open_serial(self) -> None: """ Opens a serial port using specific parameters :return: None """ baud_rate = 57600 my_port = self.selected_port timeout = 1 self.conn.open(my_port, baud_rate, timeout) if self.debug: log.debug("Open serial port. %s" % self.conn.ser) if self.conn.is_open(): if self.conn.send(asip.INFO_REQUEST.encode()): self.isReady = True else: log.error("Failed to open serial port")
def send_request(self, request_string: str) -> None: """ Example request string: str(svc_id + ',' + asip.tag_AUTOEVENT_REQUEST + ',' + str(value) + '\n') :param request_string: :type request_string: str :return: None """ if self.isReady: request_string = request_string.encode() if self.debug: log.debug("Request msg: %s" % (request_string.decode().strip('\n'))) successfully_sent_message = self.conn.send(request_string) if not successfully_sent_message: # If send failed so close port self.close_serial() else: log.error('Serial port is not connected')
def process_response(self, message: str) -> None: """ This function is taking an input, validating by comparing an service tag and id and extracts values A response for a message is something like "@E,e,2,{3000:110,3100:120}" :param message: :return: """ if message[3] != self.tag: # We have received a message but it is not an encoder reporting event log.error("Unable to process received message: {}".format(message)) else: enc_values = message[message.index("{") + 1:message.index("}")].split(",") self.left_values = [int(i) for i in enc_values[0].split(":")] self.right_values = [int(i) for i in enc_values[1].split(":")] self.all_values = [self.left_values, self.right_values] if self.debug: print("Encoders message: {}".format(message))
def initialize_main(self, services_to_run: dict) -> None: self.run_event = threading.Event() self.run_event.set() main_thread = threading.Thread( name='Teensy msgs receiver', target=self.serial_manager.conn.receive_data, args=(self.run_event, self.run_services, self.terminate_all)) self.all_threads = [main_thread] # Start all threads for thread in self.all_threads: try: thread.start() time.sleep(1) log.info("Thread: %s set up successfully" % thread.getName()) except Exception as error: log.error("Could not create a thread %s" % error) # Init all services self.initialize_services(services_to_run) # This delay allows to make sure all background services will run, before any task sleep(2)
def msg_dispatcher(self, msg: str) -> None: """ Function is checking if the message header is an event message or an error message or informational or debug message. Based on the type, function will redirect it to the right functions :param msg: :type msg: str :return: None """ if len(msg) > 0: msg_head = msg[0] else: # TODO Fix problem with incorrect message type log.error( "Problem with with message dispatching. Terminating program") sys.exit() if msg_head == asip.EVENT_HEADER: self.event_dispatcher(msg) elif msg_head == asip.DEBUG_MSG_HEADER: log.debug(msg[1:]) elif msg_head == asip.ERROR_MSG_HEADER: log.error('Error: ' + msg[1:])