Python Multi-threading with PySerial object

前端 未结 1 1013
抹茶落季
抹茶落季 2021-01-20 18:03

I am new to Python and programming in general. I am trying to write a device driver using pyserial. I opened a thread that would read data from the device and send it to std

相关标签:
1条回答
  • 2021-01-20 18:55

    You can do it like this:

    import serial
    from threading import Thread
    from functools import wraps
    
    
    # decorate the function - start another thread
    def run_async(func):
    
            @wraps(func)
            def async_func(*args, **kwargs):
                    func_hl = Thread(target = func, args = args, kwargs = kwargs)
                    func_hl.start()
                    return func_hl
    
            return async_func
    
    @run_async                     #use asyncronously 
    def writeInstruction(ser):
        #reads command from stdin
        #Writes the command to the device (enabling data output)
    
    
    @run_async                     #use asynchronously
    def readData(ser):
        #reads the packets coming from the device
        #prints it through std out
    
    your_arg = 'test'
    writeInstruction(your_arg)
    
    0 讨论(0)
提交回复
热议问题