PySerial非阻塞读取

时间:2018-11-08 13:02:40

标签: python pyserial

此刻,我必须编写一个代码,该代码不断从我的COM端口读取数据。但是,当我尝试使用read()阻塞程序时,我已经尝试使用线程,但是当我将线程作为目标时,read函数就会继续阻塞。 因此,我添加了一个return,问题是它只会返回按下按钮时读取的值。

此刻,Lees()在我按InsertText()按钮的那一刻读取数据。我希望Lees()在使用界面时继续在后台运行。

接口:

    import sys
    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLineEdit
    import serial_se_re
    import threading

    serialPort = serial_se_re.SerialPort()

    def SendDataCommand():
        serialPort.Stuur("Basic Send")

    def Verbinden():
        serialPort.Open("COM13",19200)

    def InsertText():
        print(serialPort.Lees())
        #tekst_veld.insert(message)

    app = QApplication([])
    window = QWidget()
    layout = QVBoxLayout()
    window.setWindowTitle('Python')

    thread = threading.Thread(target=serialPort.Lees())
    thread.start()

    connect_button = QPushButton('Connect')
    layout.addWidget(connect_button)
    connect_button.clicked.connect(lambda: Verbinden())

    stuur_data_button = QPushButton('Stuur message')
    layout.addWidget(stuur_data_button)
    stuur_data_button.clicked.connect(lambda: SendDataCommand())

    instert_text = QPushButton('add')
    layout.addWidget(instert_text)
    instert_text.clicked.connect(lambda: InsertText())

    tekst_veld = QLineEdit()
    layout.addWidget(tekst_veld)

    window.setLayout(layout)
    window.show()
    app.exec_()

SerialConnection:

import serial
import sys
import _thread
import threading

class SerialPort:
    def __init__(self):
        self.comportName = "COM13"
        self.baud = 19200
        self.isopen = False
        self.timeout = None
        self.serialport = serial.Serial()


    def __del__(self):
        try:
            if self.serialport.is_open():
                self.serialport.close()
        except:
            print("Kan de COM port niet sluiten, error: ", sys.exc_info()[0] )

    def CheckOpen(self):
        return self.isopen

    def Open(self,port,baud):
        if not self.isopen:
            self.serialport.port = port
            self.serialport.baudrate = baud
            try:
                self.serialport.open()
                self.isopen = True
            except:
                print("Kan port niet openen, error: ", sys.exc_info()[0])

    def Sluiten(self):
        if self.isopen:
            try:
                self.serialport.close()
                self.isopen = False
            except:
                print("Kan port niet sluiten, error: ", sys.exc_info()[0])

    def Stuur(self,message):
        if self.isopen:
            try:
                # Ensure that the end of the message has both \r and \n, not just one or the other
                newmessage = message.strip()
                newmessage += '\r\n'
                self.serialport.write(newmessage.encode('utf-8'))
            except:
                print("Error sending message: ", sys.exc_info()[0] )
            else:
                return True
        else:
            return False

    def Lees(self):
        if self.isopen:
            try:
                while(1):
                    message = self.serialport.read()
                    return(message)
            except Exception:
                print("error")
        else:
            print("Cannot open serial port")

0 个答案:

没有答案