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!
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]
