Skip to content Skip to sidebar Skip to footer

Python's Pyserial With Interrupt Mode

I have a device which works on serial communication. I am writing python code which will send some commands to get the data from the device. There are three commands. 1.COMMAND -

Solution 1:

Instead of using time.sleep(), its preferred to use serialport.in_waiting which help to check the number of bytes available in rcv buffer. So once there is some data is rcv buffer then only read the data using read function. so following code sequence can be followed without having any delay

while True:
    bytesToRead = port.in_waiting
    print(bytesToRead)
    if(bytestoRead > 0):
        res = port.read(bytesToRead)
        print(res)
        port.reset_input_buffer()
        # put some check or filter then write data on serial port
        port.write(("time\r".encode()))
        res = port.readline()
        print(res)

Solution 2:

I am taking a stab here: Your time.sleep(5) might be too long. Have you tried making the sleep really short, for example time.sleep(.300)? If the time data gets written back between the sop returns you will catch it before it gets merged with sop, but I am making an assumption here that it will send time data back, else there is anyway nothing more you can do on the server side (the python) code. I do believe it won't hurt to make the sleep less, it is anyway just sitting there waiting (polling) for communication.

Not having the having the same environment on my side, makes it difficult to answer, because I can't test my answer, so I hope this might help.

Post a Comment for "Python's Pyserial With Interrupt Mode"