Exemplo n.º 1
0
 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))
Exemplo n.º 2
0
 def send_request(self, frequency, duration) -> None:
     """
     :return: None
     """
     request_string = str(self.header + ',' + self.tag + ',' +
                          str(frequency) + ',' + str(duration) + '\n')
     self.serial_manager.send_request(request_string)
     if self.debug:
         log.debug("Tone request message: {}".format(request_string))
Exemplo n.º 3
0
 def __init__(self, debug: bool = False) -> None:
     self.isReady = False
     self.conn = SerialConnection()
     self.ports = self.conn.list_available_ports()
     self.selected_port = self.ports[0]
     self.debug = debug
     if self.debug:
         log.debug("Available ports: {}".format(self.ports))
     if not self.isReady:
         self.open_serial()
     log.info("Serial port: %s opened successfully" % self.selected_port)
Exemplo n.º 4
0
 def set_motor(self, speed: int) -> None:
     """
     Function in which speed value is being validate in terms of range and type, then send request is being made
     :param speed:
     :return:
     """
     # Speed should be between -100 and +100
     if speed > 100:
         speed = 100
     if speed < -100:
         speed = -100
     if self.debug:
         log.debug("Setting motor id:{} to speed:{}".format(
             self.motor_id, speed))
     self.send_request(speed)
Exemplo n.º 5
0
 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")
Exemplo n.º 6
0
 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')
Exemplo n.º 7
0
 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:])