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

73 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

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
class Arduino_class(QtCore.QThread):
sinOut = pyqtSignal(str) # 聲明一個帶字串參數的信號
def __init__(self, parent=None):
super().__init__(parent)
self.ser_com5 = serial.Serial('COM4', 9600, timeout=1)
self.run_flag = False
self.img_count=0
self.move_count=0
def run(self):
time.sleep(2)
print("Arduino 已連線")
self.Home()
while True:
if self.run_flag:
while True:
message = self.ser_com5.readline().decode().strip()
if message == "Turn_finsh":
if self.img_count == 21:
self.move_one_fov()
else:
time.sleep(0.02)
self.sinOut.emit('Turn_finsh')
if message == "Motor_finsh":
if self.move_count <= 5:
self.sinOut.emit('Motor_finsh')
time.sleep(0.1)
self.turn_one_angle()
else:
self.move_count = 0
self.sinOut.emit('end')
if message == "Motor_in_End":
print(message)
self.sinOut.emit('Motor_finsh')
if message == "Motor_in_Home":
print(message)
self.sinOut.emit('Motor_in_Home')
self.run_flag = False
break
time.sleep(0.05)
def start_run(self):
print("已自動發送命令给Arduino")
self.run_flag = True
self.turn_one_angle()
def turn_one_angle(self):
self.img_count = self.img_count+1
self.ser_com5.write(b'turn_one_angle\n')
def move_one_fov(self):
self.ser_com5.write(b'move_one_fov\n')
self.move_count = self.move_count+1
self.img_count = 0
def Home(self):
self.ser_com5.write(b'move_home\n')
self.run_flag = True
print("已自動發送命令给USB COM5")