我一直在尝试将 self.Arduino 从 GetData 类继承到 GUI 类。所以为了做到这一点,我简单地添加了这行代码。
class GUI(QMainWindow, Ui_MainWindow, GetData):
我以为它会继承 self.Arduino 但它没有。显然我做错了什么,但我不明白是什么。这是我的代码
class GetData(QThread):
ChangedData = pyqtSignal(float, float, float, float)
def __init__(self, parent=None):
QThread.__init__(self, parent)
arduino_ports = [ # automatically searches for an Arduino and selects the port it's on
p.device
for p in serial.tools.list_ports.comports()
if 'Arduino' in p.description
]
if not arduino_ports:
raise IOError("No Arduino found - is it plugged in? If so, restart computer.")
if len(arduino_ports) > 1:
warnings.warn('Multiple Arduinos found - using the first')
self.Arduino = serial.Serial(arduino_ports[0], 9600, timeout=1)
def __del__(self): # part of the standard format of a QThread
self.wait()
def run(self): # also a required QThread func tion, the working part
import time
self.Arduino.close()
self.Arduino.open()
self.Arduino.flush()
self.Arduino.reset_input_buffer()
start_time = time.time()
while True:
while self.Arduino.inWaiting() == 0:
pass
try:
data = self.Arduino.readline()
dataarray = data.decode().rstrip().split(',')
self.Arduino.reset_input_buffer()
Pwm = round(float(dataarray[0]), 3)
Distance = round(float(dataarray[1]), 3)
ArduinoTime = round(float(dataarray[2]), 3)
RunTime = round(time.time() - start_time, 3)
print(Pwm, 'Pulse', ",", Distance, 'CM', ",", ArduinoTime, "Millis", ",", RunTime, "Time Elasped")
self.ChangedData.emit(Pwm, Distance, ArduinoTime , RunTime)
except (KeyboardInterrupt, SystemExit, IndexError, ValueError):
繁花如伊
相关分类