3

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')
1
  • 2
    Your indentation is off, but it's pretty clear that you don't actually assign to self. ser in the check_phone method. It's just a local variable that's dereferenced as soon as the method call ends. Commented Jan 8, 2017 at 23:15

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. In fact that was the problem. In fact ser was just local! I have not seen this at all.
@fahrradlaus - accept this answer so we know its right at a glace.... and Tagc gets 15 shiny points to boot!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.