mirror of
https://github.com/janishutz/BiogasControllerApp.git
synced 2025-11-25 13:54:24 +00:00
App launching, some porting work complete
This commit is contained in:
@@ -9,11 +9,22 @@ class Com:
|
||||
self._serial: Optional[serial.Serial] = None
|
||||
self._filters = filters if filters != None else [ 'USB-Serial Controller', 'Prolific USB-Serial Controller' ]
|
||||
self._port_override = ''
|
||||
self._baudrate = 19200
|
||||
|
||||
def set_port_override(self, override: str) -> None:
|
||||
"""Set the port override, to disable port search"""
|
||||
self._port_override = override
|
||||
|
||||
def _connection_check(self) -> bool:
|
||||
if self._serial == None:
|
||||
return self._open()
|
||||
if self._serial != None:
|
||||
if not self._serial.is_open:
|
||||
self._serial.open()
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def get_comport(self) -> str:
|
||||
"""Find the comport the microcontroller has attached to"""
|
||||
if self._port_override != '':
|
||||
@@ -34,20 +45,24 @@ class Com:
|
||||
|
||||
return ''
|
||||
|
||||
def connect(self, baud_rate: int, port_override: Optional[str] = None) -> bool:
|
||||
"""Try to find a comport and connect to the microcontroller. Returns the success as a boolean"""
|
||||
def _open(self) -> bool:
|
||||
comport = self.get_comport()
|
||||
|
||||
# Comport search returns empty string if search unsuccessful
|
||||
if comport == '':
|
||||
try:
|
||||
self._serial = serial.Serial(comport, baud_rate, timeout=5)
|
||||
self._serial = serial.Serial(comport, self._baudrate, timeout=5)
|
||||
except:
|
||||
return False
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def connect(self, baud_rate: int) -> bool:
|
||||
"""Try to find a comport and connect to the microcontroller. Returns the success as a boolean"""
|
||||
self._baudrate = baud_rate
|
||||
return self._connection_check()
|
||||
|
||||
def close(self) -> None:
|
||||
"""Close the serial connection, if possible"""
|
||||
if self._serial != None:
|
||||
@@ -58,23 +73,24 @@ class Com:
|
||||
|
||||
def receive(self, byte_count: int) -> bytes:
|
||||
"""Recieve bytes from microcontroller over serial. Returns bytes. Might want to decode using functions from lib.tools"""
|
||||
if self._serial == None:
|
||||
self.connect(19200)
|
||||
self._connection_check()
|
||||
if self._serial != None:
|
||||
return self._serial.read(byte_count)
|
||||
else:
|
||||
raise Exception('ERR_CONNECTION')
|
||||
raise Exception('ERR_CONNECTING')
|
||||
|
||||
def send(self, msg: str) -> None:
|
||||
"""Send a string over serial connection."""
|
||||
if self._serial == None:
|
||||
self.connect(19200)
|
||||
"""Send a string over serial connection. Will open a connection if none is available"""
|
||||
self._connection_check()
|
||||
if self._serial != None:
|
||||
self._serial.write(msg.encode())
|
||||
else:
|
||||
raise Exception('ERR_CONNECTING')
|
||||
|
||||
def send_float(self, msg: float) -> None:
|
||||
"""Send a float number over serial connection"""
|
||||
if self._serial == None:
|
||||
self.connect(19200)
|
||||
self._connection_check()
|
||||
if self._serial != None:
|
||||
self._serial.write(bytearray(struct.pack('>f', msg))[0:3])
|
||||
else:
|
||||
raise Exception('ERR_CONNECTING')
|
||||
|
||||
@@ -1,22 +1,61 @@
|
||||
from typing import Optional
|
||||
import lib.com
|
||||
import lib.decoder
|
||||
import time
|
||||
|
||||
# TODO: Load filters (for comport search)
|
||||
com = lib.com.Com()
|
||||
decoder = lib.decoder.Decoder()
|
||||
|
||||
class Instructions:
|
||||
def __init__(self) -> None:
|
||||
pass
|
||||
def set_port_override(self, override: str) -> None:
|
||||
com.set_port_override(override)
|
||||
|
||||
def _hook(self, instruction: str, sequence: list[str]) -> bool:
|
||||
# Send instruction to microcontroller to start hooking process
|
||||
com.send(instruction)
|
||||
|
||||
# Record start time to respond to timeout
|
||||
start = time.time()
|
||||
|
||||
# Check for timeout
|
||||
pointer = 0
|
||||
sequence_max = len(sequence) - 1
|
||||
while time.time() - start < 5:
|
||||
if ( decoder.decode_ascii( com.receive(1) ) ) == sequence[pointer]:
|
||||
pointer += 1
|
||||
else:
|
||||
pointer = 0
|
||||
|
||||
if pointer == sequence_max:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
def change_temperature(self, new_temps: list[float]) -> None:
|
||||
pass
|
||||
def _change_data(self, instruction: str, readback: list[str], data: list[float], readback_length: int) -> None:
|
||||
# Hook to stream
|
||||
if self._hook(instruction, readback):
|
||||
while len(data) > 0:
|
||||
if com.receive(readback_length) != '':
|
||||
com.send_float(data.pop(0))
|
||||
else:
|
||||
com.close()
|
||||
raise Exception('Failed to transmit data. No response from controller')
|
||||
com.close()
|
||||
else:
|
||||
com.close()
|
||||
raise ConnectionError('Failed to hook to controller data stream. No fitting response received')
|
||||
|
||||
def change_config(self, new_config: list[float]) -> None:
|
||||
pass
|
||||
self._change_data('PR', ['\n', 'P', 'R', '\n'], new_config, 3)
|
||||
|
||||
def change_temperature(self, temperatures: list[float]) -> None:
|
||||
self._change_data('PT', ['\n', 'P', 'T', '\n'], temperatures, 3)
|
||||
|
||||
def enable_fastmode(self) -> None:
|
||||
com.send('FM')
|
||||
com.close()
|
||||
|
||||
def disable_fastmode(self) -> None:
|
||||
com.send('NM')
|
||||
com.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user