# Import everything from the DS_RECENT module, which includes the UI
from DS_RECENT import *
# Import the serial module for serial communication
import serial
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, *args, **kwargs):
# Initialize the QMainWindow base class
QtWidgets.QMainWindow.__init__(self, *args, **kwargs)
# Set up the UI defined in the DS_RECENT module
self.setupUi(self)
# Add your custom logic here
try:
# Initialize data_R to 0
self.data_R = 0
# Open serial port COM6 with baud rate 9600
self.serial = serial.Serial('COM6', 9600)
# Print success message if serial port is successfull
print('Succes!')
except:
# Print error message if fails the serial port
print("Error on serial")
# Create a QTimer object
self.timer = QtCore.QTimer()
# Set the timer interval to 1000 milliseconds
self.timer.setInterval(1000)
# Connect the timer's timeout signal to the read_sensor method
self.timer.timeout.connect(self.read_sensor)
# Start the timer
self.timer.start()
def read_sensor(self):
# Read a line from the serial port
self.data_R = self.serial.readline()
# Strip whitespace and split the data by commas
self.tup = self.data_R.strip().split(",")
# Print the tuple to the console
print(self.tup)
# Display the first element of the tuple on lcdNumber
self.lcdNumber.display(self.tup[0])
# Display the second element of the tuple on lcdNumber_3
self.lcdNumber_3.display(self.tup[1])
if __name__ == '__main__':
# Create a QApplication object
app = QtWidgets.QApplication([])
# Create an instance of MainWindow
window = MainWindow()
# Show the main window
window.show()
# Start the Qt event loop
app.exec()