From 183cf4fed369136404bcf5f15cd61ed3e397aabc Mon Sep 17 00:00:00 2001 From: Lucas Hicks Date: Sun, 15 Oct 2023 13:33:13 +1000 Subject: [PATCH] Changes microcontroller error stuff. --- build/figurines/microcontroller_comms.py | 39 +++++++++++++++++++----- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/build/figurines/microcontroller_comms.py b/build/figurines/microcontroller_comms.py index ff36477..c6cd2e8 100644 --- a/build/figurines/microcontroller_comms.py +++ b/build/figurines/microcontroller_comms.py @@ -16,10 +16,12 @@ import requests import socket import json +import logging # =========================== GLOBAL VALUES ================================== # POLL_DELAY = 15 +COM_PORT_PREFIX = "cu.usbmodem" # usbmodem1101 and usbmodem2101 global connect_status, read_state, port, serialPort global serial_count # Optimized counter, used to clear the serial input buffer serial_count = 0 @@ -28,10 +30,12 @@ # =========================== SERIAL HANDLING ================================== # +_LOGGER = logging.getLogger(__name__) +_LOGGER.setLevel(logging.DEBUG) def initialise_serial(port): global serialPort, connect_status - print('PRC [Starting Serial at ' + port + ']') + _LOGGER.info("Starting serial at %s", port) serialPort = serial.Serial( port, baudrate=9600, @@ -46,9 +50,12 @@ def initialise_serial(port): def serial_disconnect(): global serialPort, connect_status + _LOGGER.info("Disconnecting serial port") serialPort.flushInput() serialPort.flushOutput() + _LOGGER.info("Flushing input and output") serialPort.close() + _LOGGER.info("Closing serial port") connect_status = ( False # Set the connection status to False when serial is disconnected ) @@ -57,6 +64,8 @@ def serial_disconnect(): def serial_send_data(data): global read_state, serialPort read_state = False # Pause Reading serial + _LOGGER.info("Sending data to the port") + _LOGGER.debug("Sending %s", data) print('Sending ' + str(data)) serialPort.write(data) read_state = True # Resume Reading serial @@ -73,6 +82,7 @@ def serial_read_data(serialPort): def send_to_controller(value): + _LOGGER.info("Sending data to the microcontroller") com_string = ''.join(str(value[key]) for key in sorted(value)) data = bytes('FIG' + com_string, 'utf-8') serial_send_data(data) @@ -97,7 +107,7 @@ def check_presence(port, interval=0.01): def start_thread(): global port, connect_status - print('THR [Starting Thread at port:', port, ']') + _LOGGER.info("Starting thread at port %s", port) port_controller = threading.Thread(target=check_presence, args=(port, 0.1)) port_controller.daemon = True port_controller.start() @@ -112,10 +122,14 @@ def main(): while True: if not connect_status: - port = serial.tools.list_ports.comports() - coms = [com[0] for com in port] - if coms: - initialise_serial(coms[0]) + port_device = None + ports = serial.tools.list_ports.comports() + for port in ports: + if COM_PORT_PREFIX in port.name: + port_device = port[0] + break + if port_device is not None: + initialise_serial(port_device) start_thread() else: connect_status = False @@ -129,9 +143,18 @@ def main(): figurine_status = response send_to_controller(figurine_status) sleep(POLL_DELAY) - except Exception as e: + except requests.exceptions.ConnectionError as e: + _LOGGER.info("Error making HTTP request") + _LOGGER.debug("Error - %s", e) serial_disconnect() - print(f'Error connecting to serial: {e}') + _LOGGER.info("Disconnecting serial") + break + except serial.SerialException as e: + _LOGGER.info("Error finding serial device or configuring it") + _LOGGER.debug("Error - %s", e) + break + + if __name__ == '__main__':