mirror of
https://github.com/janishutz/BiogasControllerApp.git
synced 2025-11-25 13:54:24 +00:00
Updated to Version V2.3.0-stable, installer and compiled version will soon be available
This commit is contained in:
96
BiogasControllerApp-V2.3/bin/lib/communication.py
Normal file
96
BiogasControllerApp-V2.3/bin/lib/communication.py
Normal file
@@ -0,0 +1,96 @@
|
||||
import bin.lib.lib
|
||||
com = bin.lib.lib.Com()
|
||||
|
||||
|
||||
class Communication:
|
||||
def __init__(self):
|
||||
self.__x = 0
|
||||
self.__data_recieve = 0
|
||||
self.__output = ""
|
||||
|
||||
def change_temp(self, data, special_port):
|
||||
com.connect(19200, special_port)
|
||||
com.send("PT")
|
||||
self.go = 0
|
||||
while True:
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "\n":
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "P":
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "T":
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "\n":
|
||||
self.go = 1
|
||||
break
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
if self.go == 1:
|
||||
self.data = data
|
||||
while len(self.data) > 0:
|
||||
self.__data_recieve = com.receive(3)
|
||||
if self.__data_recieve != "":
|
||||
com.send_float(float(self.data.pop(0)))
|
||||
else:
|
||||
print("error")
|
||||
break
|
||||
else:
|
||||
print("Error")
|
||||
com.quitcom()
|
||||
|
||||
def change_all(self, data, special_port):
|
||||
com.connect(19200, special_port)
|
||||
com.send("PR")
|
||||
self.go = 0
|
||||
while True:
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "\n":
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "P":
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "R":
|
||||
self.__data_recieve = com.decode_ascii(com.receive(1))
|
||||
if self.__data_recieve == "\n":
|
||||
self.go = 1
|
||||
break
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
if self.go == 1:
|
||||
self.data = data
|
||||
while len(self.data) > 0:
|
||||
self.__data_recieve = com.receive(3)
|
||||
if self.__data_recieve != "":
|
||||
com.send_float(float(self.data.pop(0)))
|
||||
else:
|
||||
print("error")
|
||||
break
|
||||
else:
|
||||
print("Error")
|
||||
com.quitcom()
|
||||
|
||||
|
||||
class SwitchMode:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def enable_fastmode(self, special_port):
|
||||
com.connect(19200, special_port)
|
||||
com.send("FM")
|
||||
com.quitcom()
|
||||
|
||||
def disable_fastmode(self, special_port):
|
||||
com.connect(19200, special_port)
|
||||
com.send("NM")
|
||||
com.quitcom()
|
||||
22
BiogasControllerApp-V2.3/bin/lib/comport_search.py
Normal file
22
BiogasControllerApp-V2.3/bin/lib/comport_search.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import serial.tools.list_ports
|
||||
|
||||
|
||||
class ComportService:
|
||||
def __init__(self):
|
||||
self.__comport = []
|
||||
self.__import = []
|
||||
self.__working = []
|
||||
|
||||
def get_comport(self, special_port):
|
||||
self.__comport = [comport.device for comport in serial.tools.list_ports.comports()]
|
||||
self.__pos = 0
|
||||
if special_port != "":
|
||||
self.__working = special_port
|
||||
else:
|
||||
while self.__working == []:
|
||||
self.__com_name = serial.tools.list_ports.comports()[self.__pos]
|
||||
if "USB-Serial Controller" or "Prolific USB-Serial Controller" in self.__com_name:
|
||||
self.__working = self.__comport.pop(self.__pos)
|
||||
else:
|
||||
self.__pos += 1
|
||||
return self.__working
|
||||
122
BiogasControllerApp-V2.3/bin/lib/csv_parsers.py
Normal file
122
BiogasControllerApp-V2.3/bin/lib/csv_parsers.py
Normal file
@@ -0,0 +1,122 @@
|
||||
"""@package docstring
|
||||
This is a simplification of the csv module"""
|
||||
|
||||
import csv
|
||||
|
||||
|
||||
class CsvRead:
|
||||
"""This is a class that reads csv files and depending on the module selected does do different things with it"""
|
||||
def __init__(self):
|
||||
self.__imp = ""
|
||||
self.__raw = ""
|
||||
self.__raw_list = ""
|
||||
|
||||
def importing(self, path):
|
||||
"""Returns a list of the imported csv-file, requires path, either direct system path or relative path"""
|
||||
self.__imp = open(path)
|
||||
self.__raw = csv.reader(self.__imp, delimiter=',')
|
||||
self.__raw_list = list(self.__raw)
|
||||
self.__imp.close()
|
||||
return self.__raw_list
|
||||
|
||||
|
||||
class CsvWrite:
|
||||
"""This is a class that modifies csv files"""
|
||||
def __init__(self):
|
||||
self.__impl = []
|
||||
self.__strpop = []
|
||||
self.__removed = []
|
||||
self.__removing = 0
|
||||
self.__change = 0
|
||||
self.__appending = 0
|
||||
self.__imp = []
|
||||
self.__raw = []
|
||||
|
||||
def rem_str(self, path, row):
|
||||
"""Opens the csv-file in write mode which is specified as an argument either as direct or relative path"""
|
||||
self.__imp = open(path)
|
||||
self.__raw = csv.reader(self.__imp, delimiter=',')
|
||||
self.__impl = list(self.__raw)
|
||||
self.__removed = self.__impl.pop(row + 1)
|
||||
with open(path, "w") as removedata:
|
||||
self.__removing = csv.writer(removedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__removing.writerow(self.__impl.pop(0))
|
||||
while len(self.__impl) > 0:
|
||||
with open(path, "a") as removedata:
|
||||
self.__removing = csv.writer(removedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__removing.writerow(self.__impl.pop(0))
|
||||
self.__imp.close()
|
||||
removedata.close()
|
||||
|
||||
|
||||
def chg_str(self, path, row, pos, new_value):
|
||||
"""Opens the csv-file in write mode to change a value, e.g. if a recipes is changed."""
|
||||
self.__imp = open(path)
|
||||
self.__raw = csv.reader(self.__imp, delimiter=',')
|
||||
self.__impl = list(self.__raw)
|
||||
self.__strpop = self.__impl.pop(row)
|
||||
self.__strpop.pop(pos)
|
||||
self.__strpop.insert(pos, new_value)
|
||||
self.__impl.insert(row, self.__strpop)
|
||||
with open(path, "w") as changedata:
|
||||
self.__change = csv.writer(changedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__change.writerow(self.__impl.pop(0))
|
||||
while len(self.__impl) > 0:
|
||||
with open(path, "a") as changedata:
|
||||
self.__removing = csv.writer(changedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__removing.writerow(self.__impl.pop(0))
|
||||
self.__imp.close()
|
||||
changedata.close()
|
||||
|
||||
def chg_str_rem(self, path, row, pos):
|
||||
"""Opens the csv-file in write mode to change a value, e.g. if a recipes is changed."""
|
||||
self.__imp = open(path)
|
||||
self.__raw = csv.reader(self.__imp, delimiter=',')
|
||||
self.__impl = list(self.__raw)
|
||||
self.__strpop = self.__impl.pop(row)
|
||||
self.__strpop.pop(pos)
|
||||
self.__strpop.pop(pos)
|
||||
self.__impl.insert(row, self.__strpop)
|
||||
with open(path, "w") as changedata:
|
||||
self.__change = csv.writer(changedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__change.writerow(self.__impl.pop(0))
|
||||
while len(self.__impl) > 0:
|
||||
with open(path, "a") as changedata:
|
||||
self.__removing = csv.writer(changedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__removing.writerow(self.__impl.pop(0))
|
||||
self.__imp.close()
|
||||
changedata.close()
|
||||
|
||||
def chg_str_add(self, path, row, new_value1, new_value2):
|
||||
"""Opens the csv-file in write mode to change a value, e.g. if a recipes is changed."""
|
||||
self.__imp = open(path)
|
||||
self.__raw = csv.reader(self.__imp, delimiter=',')
|
||||
self.__impl = list(self.__raw)
|
||||
self.__strpop = self.__impl.pop(row)
|
||||
self.__strpop.append(new_value1)
|
||||
self.__strpop.append(new_value2)
|
||||
self.__impl.insert(row, self.__strpop)
|
||||
with open(path, "w") as changedata:
|
||||
self.__change = csv.writer(changedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__change.writerow(self.__impl.pop(0))
|
||||
while len(self.__impl) > 0:
|
||||
with open(path, "a") as changedata:
|
||||
self.__removing = csv.writer(changedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__removing.writerow(self.__impl.pop(0))
|
||||
self.__imp.close()
|
||||
changedata.close()
|
||||
|
||||
def app_str(self, path, value):
|
||||
"""Opens the csv-file in append mode and writes given input. CsvWrite.app_str(path, value).
|
||||
Path can be specified both as direct or relative. value is a list. Will return an error if type of value is
|
||||
not a list."""
|
||||
with open(path, "a") as appenddata:
|
||||
self.__appending = csv.writer(appenddata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__appending.writerow(value)
|
||||
appenddata.close()
|
||||
|
||||
def write_str(self, path, value):
|
||||
with open(path, "w") as writedata:
|
||||
self.__change = csv.writer(writedata, delimiter=',', quoting=csv.QUOTE_MINIMAL)
|
||||
self.__change.writerow(value)
|
||||
writedata.close()
|
||||
73
BiogasControllerApp-V2.3/bin/lib/lib.py
Normal file
73
BiogasControllerApp-V2.3/bin/lib/lib.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import serial
|
||||
import struct
|
||||
import bin.lib.comport_search
|
||||
"""@package docstring
|
||||
This package can communicate with a microcontroller"""
|
||||
|
||||
coms = bin.lib.comport_search.ComportService()
|
||||
|
||||
|
||||
class Com:
|
||||
def __init__(self):
|
||||
self.xr = ""
|
||||
self.output = ""
|
||||
self.str_input = ""
|
||||
self.str_get_input = ""
|
||||
self.xs = ""
|
||||
self.__comport = '/dev/ttyUSB0'
|
||||
|
||||
def connect(self, baudrate, special_port):
|
||||
try:
|
||||
self.__comport = coms.get_comport(special_port)
|
||||
except:
|
||||
pass
|
||||
self.ser = serial.Serial(self.__comport, baudrate=baudrate, timeout=5)
|
||||
|
||||
def quitcom(self):
|
||||
try:
|
||||
self.ser.close()
|
||||
except:
|
||||
pass
|
||||
|
||||
def receive(self, amount_bytes):
|
||||
self.xr = self.ser.read(amount_bytes)
|
||||
return self.xr
|
||||
|
||||
def decode_ascii(self, value):
|
||||
try:
|
||||
self.output = value.decode()
|
||||
except:
|
||||
self.output = "Error"
|
||||
return self.output
|
||||
|
||||
def check_value(self, value_check, checked_value):
|
||||
if value_check == checked_value:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
def decode_int(self, value):
|
||||
self.i = int(value, base=16)
|
||||
return self.i
|
||||
|
||||
def decode_float(self, value):
|
||||
self.fs = str(value, 'ascii') + '00'
|
||||
self.f = struct.unpack('>f', bytes.fromhex(self.fs))
|
||||
return str(self.f[0])
|
||||
|
||||
def decode_float_2(self, value):
|
||||
self.fs = str(value, 'ascii') + '0000'
|
||||
self.f = struct.unpack('>f', bytes.fromhex(self.fs))
|
||||
return str(self.f[0])
|
||||
|
||||
def get_input(self):
|
||||
self.str_get_input = input("please enter a character to send: ")
|
||||
return self.str_get_input
|
||||
|
||||
def send(self, str_input):
|
||||
self.xs = str_input.encode()
|
||||
self.ser.write(self.xs)
|
||||
|
||||
def send_float(self, float_input):
|
||||
ba = bytearray(struct.pack('>f', float_input))
|
||||
self.ser.write(ba[0:3])
|
||||
Reference in New Issue
Block a user