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
  How to get OpenCV to display entire camera frame? George713 1 220 Aug-12-2021, 02:45 AM
Last Post: Pedroski55
  Display table field on multiple lines, 'wordwrap' 3python 0 155 Aug-06-2021, 08:17 PM
Last Post: 3python
  Pygame with joystick mmagner2022 1 205 Aug-06-2021, 07:56 PM
Last Post: SheeppOSU
  How to display combobox related values in labels Lalremruata 0 291 Jul-20-2021, 02:45 AM
Last Post: Lalremruata
  how to suppress not to display the package contents from pydoc! maiya 3 667 Mar-19-2021, 03:41 AM
Last Post: bowlofred
  display the result of Dataframe in tabulate format alex80 0 482 Sep-09-2020, 02:22 PM
Last Post: alex80
  matplotlib for better waveform display jefflawr 0 591 Aug-14-2020, 01:39 AM
Last Post: jefflawr
  pyautogui with a display emulator? gumby4231 0 891 Jul-30-2020, 02:46 PM
Last Post: gumby4231
  display single elements in array davidm 3 784 Jul-26-2020, 07:32 AM
Last Post: davidm
  mapping joystick axis to specific 'channels' in python DashOrion 1 721 Jul-07-2020, 04:26 PM
Last Post: DashOrion

Forum Jump:

User Panel Messages

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