简体   繁体   English

5秒后如何停止“ serial.read(1)”?

[英]How to stop “serial.read(1)” after 5 seconds?

I create a code to communicate with sensors by a serial port. 我创建一个代码以通过串行端口与传感器进行通信。 I use Python 3.7, with serial library. 我使用带有串行库的Python 3.7。

MY PROBLEM : "serial.read(1)" is reading the serial port to find one byte (which comes from FPGA electronic card). 我的问题:“ serial.read(1)”正在读取串行端口以找到一个字节(来自FPGA电子卡)。 BUT when there is nothing to read, the program is stopping at this instruction, and I am forced to brutally leave it. 但是,当没有什么要读取的内容时,程序将在此指令处停止,而我被迫残酷地离开它。

MY GOAL : If there is something to read, the program shows the byte (with "print()"). 我的目标:如果有什么要读取的内容,程序将显示该字节(带有“ print()”)。 But if there is nothing to read, I want the program to stop reading the serial port after 5 seconds, instead of blocking on this instruction. 但是,如果没有要读取的内容,我希望程序在5秒钟后停止读取串行端口,而不是阻塞此指令。

I am thinking about using threads for a "timer function" : the 1st thread is reading the serial port, while the 2nd thread is waiting 5 sec. 我正在考虑将线程用于“计时器功能”:第一个线程正在读取串行端口,而第二个线程正在等待5秒。 After 5 sec, the 2nd thread stops the 1st thread. 5秒钟后,第二个线程停止第一个线程。

def Timer():
    class SerialLector(Thread):

        """ Thread definition. """

        def __init__(self):
            Thread.__init__(self)
            self.running = False           # Thread is stopping.

        def run(self):

            """ Thread running program. """

            self.running = True    # Thread is looking at the serial port.                                        
            while self.running:
                if ser.read(1):                                             
                    print("There is something !",ser.read(1))

        def stop(self):
            self.running = False




    # Creation of the thread
    ThreadLector = SerialLector()

    # Starting of the thread
    ThreadLector.start()

    # Stopping of the thread after 5 sec
    time.sleep(5)
    ThreadLector.stop()
    ThreadLector.join()
    print("There is nothing to read...")

Result : the program blocks. 结果:程序块。 I don't know how to stop reading after 5 seconds ! 我不知道5秒钟后如何停止阅读!

Python标准库具有一个signal包,该signal包为可能会暂停的功能提供超时功能: https : //docs.python.org/3/library/signal.html

I would prefer to run a timer in the read-thread itself, which will join the thread after 5 seconds. 我希望在读取线程本身中运行一个计时器,该计时器将在5秒钟后加入线程。 You can check this thread how to do this : How to set a timeout to a thread 您可以检查此线程如何执行此操作: 如何为线程设置超时

Easiest solution is to start the thread as daemon and wait 5s for it to produce anything. 最简单的解决方案是将线程作为守护程序启动,并等待5s使其产生任何东西。 After that, just end the program. 之后,结束程序。 Python will terminate the thread on its own then. 然后,Python将自行终止线程。

A more elegant solution would use something like select() which can wait for multiple file descriptors to enter a state where they can provide or receive data and that with a timeout as well. 一个更优雅的解决方案将使用诸如select()类的东西,它可以等待多个文件描述符进入可以提供或接收数据的状态,并且该状态也会超时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM