medical_balloon/Defect_Detection/Class/Motor.py
leo890808 a1fb25c89f UP
2024-07-30 16:18:26 +08:00

56 lines
2.0 KiB
Python
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
# 旋轉(COM5)
class Motor_class(QtCore.QThread):
sinOut = pyqtSignal(str) # 聲明一個帶字串參數的信號
def __init__(self, parent=None):
super().__init__(parent)
self.ser_com5 = serial.Serial('COM4', 9600, timeout=1)
self.flag =False
self.expected_2_count = 23
self.received_2_count = 0
def run(self):
time.sleep(2)
print("USB COM5的操作已完成。")
while True:
if self.flag:
while True:
message = self.ser_com5.readline().decode().strip()
if message == "2":
# print(f"接收到 '{message}' 消息,執行拍照...")
self.received_2_count += 1 # 每接收到一個"2"計數器加1
self.sinOut.emit('2')
# 判斷是否接收到了預期的"2"的數量
if self.received_2_count == self.expected_2_count:
# print("接收到了預期的'2'的數量,開始啟動 USB COM4")
self.sinOut.emit('move')
self.received_2_count = 0
break # 跳出循環
elif message == "Motor end":
# print("接收到 'Motor end' 消息,但沒有接收到預期的'2',開始啟動 USB COM4")
self.flag = False
self.sinOut.emit('move')
self.received_2_count = 0
break # 跳出循環
time.sleep(0.05)
def start_run(self):
# time.sleep(1)
self.ser_com5.write(b'1\n')
self.flag=True
print("已自動發送命令给USB COM5")