Skip to content

Commit

Permalink
Changes microcontroller error stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
lucashicks1 committed Oct 15, 2023
1 parent 7534e30 commit 183cf4f
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions build/figurines/microcontroller_comms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -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__':
Expand Down

0 comments on commit 183cf4f

Please sign in to comment.