from PyQt5.QtWidgets import QWidget, QFileDialog,QMainWindow, QLabel, QSizePolicy, QApplication, QAction, QHBoxLayout from PyQt5.QtCore import * from PyQt5 import QtCore, QtGui, QtWidgets import sys # 串口通信 import serial import time import threading import torch # 平移(COM4) class Pan_class(QtCore.QThread): sinOut = pyqtSignal(str) # 聲明一個帶字串參數的信號 def __init__(self, parent=None): super().__init__(parent) # 打开串口COM4,波特率为250000 # self.ser_com4 = serial.Serial('COM4', 9600, timeout=1) # time.sleep(2) # print("USB COM4的操作已完成。") # self.flag = False def run(self): # 打开串口COM4,波特率为250000 self.ser_com4 = serial.Serial('COM4', 9600, timeout=1) time.sleep(2) print("USB COM4的操作已完成。") self.move_home() self.flag = False while True: message = self.ser_com4.readline().decode().strip() # print(str(message)) if str(message) =="Motor emergency stop 010": self.sinOut.emit('Motor emergency stop 010') if str(message) =="Motor emergency stop 100": self.sinOut.emit('Motor emergency stop 100') if self.flag == True: self.ser_com4.write(b'1\n') time.sleep(0.02) self.flag = False time.sleep(1.5) self.sinOut.emit('move_end') time.sleep(0.05) def move(self): # 自动发送命令给COM4中的Arduino # self.ser_com4.write(b'1\n') self.flag = True print("已自動發送命令给USB COM4!") def move_home(self): # time.sleep(0.02) self.ser_com4.write(b'2\n')