GUI控制

LED的GUI

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

import serial


class MyWindow(QWidget):

    def __init__(self):
        super(MyWindow, self).__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)

        btn_open = QPushButton('打开LED')
        btn_close = QPushButton('关闭LED')
        btn_toggle = QPushButton('开关LED')

        layout.addWidget(btn_open)
        layout.addWidget(btn_close)
        layout.addWidget(btn_toggle)

        btn_open.clicked.connect(self.click_open)
        btn_close.clicked.connect(self.click_close)
        btn_toggle.clicked.connect(self.click_toggle)

        self.ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200)

    def click_open(self):
        # 字节数据
        data = bytearray([0x01])
        self.ser.write(data)

    def click_close(self):
        # 字节数据
        data = bytearray([0x02])
        self.ser.write(data)

    def click_toggle(self):
        # 字节数据
        data = bytearray([0x03])
        self.ser.write(data)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MyWindow()
    window.show()

    sys.exit(app.exec_())

电机GUI

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys

import serial
import struct


class MyWindow(QWidget):

    def __init__(self):
        super(MyWindow, self).__init__()

        layout = QVBoxLayout()
        self.setLayout(layout)

        self.edit = QLineEdit()
        btn = QPushButton('修改')

        layout.addWidget(self.edit)
        layout.addWidget(btn)

        btn.clicked.connect(self.click)

        self.ser = serial.Serial(port='/dev/ttyUSB0', baudrate=115200)

    def click(self):
        # 字节数据
        pwm = int(self.edit.text())
        pack = struct.pack('h', pwm)
        data = bytearray([pack[0], pack[1]])

        self.ser.write(data)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = MyWindow()
    window.show()

    sys.exit(app.exec_())