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 display combobox related values in labels Lalremruata 0 146 Jul-20-2021, 02:45 AM
Last Post: Lalremruata
  how to suppress not to display the package contents from pydoc! maiya 3 544 Mar-19-2021, 03:41 AM
Last Post: bowlofred
  display the result of Dataframe in tabulate format alex80 0 432 Sep-09-2020, 02:22 PM
Last Post: alex80
  matplotlib for better waveform display jefflawr 0 507 Aug-14-2020, 01:39 AM
Last Post: jefflawr
  pyautogui with a display emulator? gumby4231 0 759 Jul-30-2020, 02:46 PM
Last Post: gumby4231
  display single elements in array davidm 3 707 Jul-26-2020, 07:32 AM
Last Post: davidm
  mapping joystick axis to specific 'channels' in python DashOrion 1 614 Jul-07-2020, 04:26 PM
Last Post: DashOrion
  Python Module to display Dictionairies for debugging personalt 3 673 Jun-20-2020, 12:39 PM
Last Post: Yoriz
  How to Display a QR code MattLumb 3 853 Jun-02-2020, 01:38 AM
Last Post: MattLumb
  python display with '\\' when prints with key-value in dictionary maiya 12 2,582 May-30-2020, 05:56 PM
Last Post: maiya

Forum Jump:

User Panel Messages

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