Python Forum
Unable to display joystick's value from Python onto display box - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Unable to display joystick's value from Python onto display box (/thread-30853.html)



Unable to display joystick's value from Python onto display box - MelfoyGray - Nov-10-2020

Hi, I'm new to Python programming and I need desperate help for my project. I've received this coding from the previous batch of mine but I can't seem to solve the problem of displaying the joystick values on a separate window. The joystick is connected to an arduino uno and then to the python coding using the serial port.

Here is the coding. Any help would be greatly appreciated!



import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QWidget, QTextEdit
from PyQt5.QtGui import QIcon, QFont
from PyQt5.QtSerialPort import QSerialPort
from PyQt5.QtCore import QIODevice

class App(QMainWindow):
    def __init__(self):
        super().__init__()
        self.title='Hello, Joystick!'
        self.left=1555
        self.top=900
        self.width=350
        self.height=100

        # open the serial port
        self.m_serial = QSerialPort(self)
        self.m_serial.setPortName('COM3')
        if self.m_serial.open(QIODevice.ReadOnly):
            self.m_serial.setBaudRate(115200)
            self.m_serial.readyRead.connect(self.on_serial_read)
            #self.connect(m_serial, &QSerialPort::readyRead, this, &MainWindow::readData) # need check
            # Send a Control-C
            #self.serial.write(b'\x03')
        else:
            raise IOError("Cannot connect to device on port")
        
        self.initUI()
        
    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left,self.top,self.width,self.height)
        self.textbox=QTextEdit(self)
        self.textbox.move(30,30)
        self.textbox.resize(280,40)
        self.show()

    #def __del__(self):
        #self.m_serial.close()

    def on_serial_read(self):
        #bytes(self.serial.readAll())
        try:
            s = self.m_serial.readAll()
            #print (s)
            if (len(s) > 0):
                axis =  s.split(',')
                if (len(axis) == 2):
                    xval = int(axis[0])
                    yval = int(axis[1])
                    #print(str(xval) + " " + str(yval))
                    self.textbox.setText(str(xval) + " " + str(yval))
        except:
            pass


if __name__=='__main__':
        app=QApplication(sys.argv)
        ex=App()
        sys.exit(app.exec_())
[/python]


RE: Unable to display joystick's value from Python onto display box - deanhystad - Nov-10-2020

Are you sure you are getting values from the joystick? Does on_serial_read() ever get called? Have you tried putting a "print('In on_serial_read')" in the function and seeing if anything gets printed to stdout?

Are you sure the joystick is sending something to read? I would write a quick pyserial program that waits for a message and writes it to stdout. If you aren't getting anything to read you should fix that problem first.


RE: Unable to display joystick's value from Python onto display box - MelfoyGray - Nov-11-2020

Yes I'm getting values from the joystick on my terminal when I put "print"(In on_serial_read") in the function. The only thing is that I am unable to show the values on a separate GUI window.

I'll leave this image here for you to see how it looks like at the moment with the empty GUI window.