12345678910111213141516171819202122232425262728293031323334353637 |
- import time
- import csv
- from threading import Thread
- class MeasuringLoop(Thread):
- def __init__(self, ser, event_voltage, event_current, filename):
- super().__init__()
- self.ser = ser
- self.event_voltage = event_voltage
- self.event_current = event_current
- self.do = True
- self.filename_log = filename.replace('log_', 'logParam_')
- self.filename_log = self.filename_log.replace('txt', 'csv')
- self.log_parameters = open(self.filename_log, 'w', encoding='UTF8', newline='')
- fieldnames = ['time [s]', 'measured voltage [V]', 'measured current [A]']
- self.writer = csv.writer(self.log_parameters)
- self.writer.writerow(fieldnames)
- def run(self):
- self.setDo(True)
- start_time = time.time()
- while self.do:
- volts = bytearray(self.ser.measureVoltage()).decode()
- volts = volts.replace('\n', '')
- ampers = bytearray(self.ser.measureCurrent()).decode()
- ampers = ampers.replace('\n', '')
- self.event_voltage.on_change(volts)
- self.event_current.on_change(ampers)
- self.writer.writerow([time.time() - start_time, volts, ampers])
- self.event_voltage.on_change("0000")
- self.event_current.on_change("0000")
- self.log_parameters.close()
- def setDo(self, flag):
- self.do = flag
|