Python Forum
Unable to display joystick's value from Python onto display box
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to display joystick's value from Python onto display box
#1
Information 
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]
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  add svg to 2 thread program and display tabel in GUI Nietzsche 5 1,359 May-06-2023, 01:25 PM
Last Post: Nietzsche
  How to display <IPython.core.display.HTML object>? pythopen 3 45,704 May-06-2023, 08:14 AM
Last Post: pramod08728
  Dynamic pixel display jerryf 2 675 Mar-17-2023, 07:26 PM
Last Post: jerryf
  How to horizontally align and display images side-by-side in an email using Python? shantanu97 0 968 Feb-22-2023, 11:41 PM
Last Post: shantanu97
  Display value in the another entry field instead of message box adisc 6 1,504 Jun-25-2022, 02:30 PM
Last Post: rob101
  Write to a LCD display using i2c to a Raspberry Pi using i2c 6398 4x20 LCD Aggie64 4 1,533 Apr-28-2022, 07:26 PM
Last Post: Aggie64
  Display transparent image as overlay jds8086 0 1,182 Apr-17-2022, 11:43 AM
Last Post: jds8086
  How can I display dictionary in a gui window in Python? C0D3R 2 1,661 Apr-07-2022, 07:33 PM
Last Post: C0D3R
  Real time Detection and Display Gilush 0 1,764 Feb-05-2022, 08:28 PM
Last Post: Gilush

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020