Reputation: 197
I have the following class Ui_MainWindow(object). However I get the Attribute Error, that AttributeError: 'Ui_MainWindow' object has no attribute 'ser'
.ser is before defindet in the check_phone() method. The problem occurs in the sendMessage() method.
How comes that .ser is not recognized anymore?
from PyQt5 import QtCore, QtGui, QtWidgets
import Tkinter as tk
import tkFileDialog as filedialog
import tkMessageBox
import serial
import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(503, 486)
self.pushButton_2 = QtWidgets.QPushButton(self.widget)
self.pushButton_2.setObjectName("pushButton_2")
self.gridLayout.addWidget(self.pushButton_2, 3, 2, 1, 1)
self.pushButton_2.clicked.connect(self.send_sms) #send sms function
def check_phone(self):
ser = serial.Serial('/dev/ttyACM0',
460800,
timeout=5,
xonxoff = False,
rtscts = False,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE)
def sendMessage(self):
self.ser.write('ATZ\r')
time.sleep(1)
self.ser.write('AT+CMGF=1\r')
time.sleep(1)
self.ser.write('''AT+CMGS="''' + self.recipient + '''"\r''')
time.sleep(1)
self.ser.write(self.content + "\r")
time.sleep(1)
self.ser.write(chr(26))
time.sleep(2)
def send_sms(self):
check = self.radioButton.isChecked() #stuff doesnt work yet!
test = self.lineEdit.text()
print(test)
if check == True:
if not self.lineEdit.text():
root = tk.Tk()
root.withdraw()
tkMessageBox.showwarning("Phone Number Missing!", "Please enter a valid phone number")
root.destroy()
root.mainloop()
return
if not self.plainTextEdit.toPlainText():
root = tk.Tk()
root.withdraw()
tkMessageBox.showwarning("Message Missing!", "Please enter a text message")
root.destroy()
root.mainloop()
return
else:
sms = Ui_MainWindow(self.lineEdit.text(), self.plainTextEdit.toPlainText())
sms.check_phone()
sms.sendMessage()
sms.disconnectPhone()
self.plainTextEdit2.setText('message sent successfully')
Upvotes: 3
Views: 11619
Reputation: 9076
As Jon points out, you're assigning serial.Serial
to a (soon-discarded) local variable in check_phone
rather than to an instance attribute.
Replace this:
def check_phone(self):
ser = serial.Serial('/dev/ttyACM0',
460800,
timeout=5,
xonxoff = False,
rtscts = False,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE)
With this:
def check_phone(self):
self.ser = serial.Serial('/dev/ttyACM0',
460800,
timeout=5,
xonxoff = False,
rtscts = False,
bytesize = serial.EIGHTBITS,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE)
Upvotes: 6