SerialManager.py 1.12 KB
Newer Older
1 2 3
import serial
import serial.tools.list_ports
import time
4
from PyQt5 import QtWidgets
5

6
class SerialManager(object):
7

8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
    def __init__(self):
        self.portList = serial.tools.list_ports.comports()
        self.portNum=len(self.portList)
        for i in range (0,self.portNum):
            print(self.portList[i].device)

    def startPort(self,port,baud):
        self.ser = serial.Serial(port, baud)  # Inicialização da Serial com baudrate 115200bps
        self.ser.setDTR(False)  # Reset do arduino para reinicio da leitura
        time.sleep(0.022)
        self.ser.setDTR(True)
        self.ser.readline()  # Primeiro valor da serial sai com lixo

    def read(self):
        if(self.ser.inWaiting()==0):
            pass
24
        self.ser.flushInput()
25 26 27 28 29 30 31 32 33
        return self.ser.readline()

    def serialListPanel(self, uiWindow):
        uiWindow.usb = []
        for i in range(0, len(self.portList)):
            uiWindow.usb.append(QtWidgets.QAction(uiWindow.MainWindow))
            uiWindow.usb[i].setText(self.portList[i].device)
            uiWindow.usb[i].setCheckable(True)
            uiWindow.serialMenu.addAction(uiWindow.usb[i])