Reputation:
I have been trying to Inherit the self.Arduino from the GetData Class to the GUI class. So in order to do this I simply added this line of code.
class GUI(QMainWindow, Ui_MainWindow, GetData):
I thought it would inherit the self.Arduino but it did not.Obviously I am doing something wrong but I don't understand what. Here is my code
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):
pass
class GUI(QMainWindow, Ui_MainWindow, GetData):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.setupUi(self)
self.Run_pushButton.setEnabled(True)
self.Run_pushButton.clicked.connect(self.btn_run)
def Display_data(self):
self.thread = GetData(self)
self.thread.ChangedData.connect(self.onDataChanged)
self.thread.start()
self.Stop_pushButton.setEnabled(True)
self.Stop_pushButton.clicked.connect(self.btn_stop)
def onDataChanged(self, Pwm, Distance, ArduinoTime, RunTime):
self.Humid_lcdNumber_2.display(Pwm)
self.Velocity_lcdNumber_3.display(Distance)
self.Pwm_lcdNumber_4.display(ArduinoTime)
self.Pressure_lcdNumber_5.display(RunTime)
self.widget_2.update_plot(Pwm, RunTime)
def btn_run(self):
p1 = 1
p2 = self.InputPos_Slider.value()
the_bytes = bytes(f'<{p1},{p2}>\n', 'utf-8')
a = self.Arduino.write(the_bytes)
def btn_stop(self):
p1 = 0
p2 = 0
the_bytes = bytes(f'<{p1},{p2}>\n', 'utf-8')
a = self.Arduino.write(the_bytes)
It gave me this error , I thought I can simply inherit self.arduino but failed to do so.
Traceback (most recent call last):
File "C:\Users\Emman\Desktop\5th year\1THESIS\Python Program\C2_Final_PID.py", line 373, in btn_run
self.Arduino.write(the_bytes)
AttributeError: 'GUI' object has no attribute 'Arduino'
Upvotes: 0
Views: 56
Reputation: 2063
Looks like you have forgotten to initialize GetData
:
class GUI(QMainWindow, Ui_MainWindow, GetData):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
GetData.__init__(self, parent) # this is the missing line
self.setupUi(self)
self.Run_pushButton.setEnabled(True)
self.Run_pushButton.clicked.connect(self.btn_run)
(Note you may need to do the same for Ui_MainWindow
.)
Upvotes: 1