Skip to content Skip to sidebar Skip to footer

I Am Using Serial Communication Module In Python..but Data Received Is Not Proper..it Is Unstable And Changes Most Of The Time

The basic task of mine is to read some commands from the file and after reading those commands i have to send those serially to the serial port using serial module..i have created

Solution 1:

The only difference between the reduced version of your reader and the extended one is that in the latter you send the command to the device in the other end twice:

def recvCmd(mode):
    ser.write(serial.to_bytes(intCmd))            # Send command to the other side
    time.sleep(0.5)
    if mode==None:
        data_recv=ser.read(2)
        return data_recv
    elif mode==1:                                 # This is the mode that triggers the 4 Kb response
        ser.write(serial.to_bytes(intCmd))        # You send the command A SECOND TIME!
        time.sleep(1)
        data=b''
        timeout=time.time()+3.0
        while ser.inWaiting() or time.time()-timeout<0.0:
            if ser.inWaiting()>0:
                data+=ser.read(ser.inWaiting())
                timeout=time.time()+3.0
        return data
    elif mode==0:
        ser.write(serial.to_bytes(getDataBuff()))
        resp=ser.read(2)
        return resp

Your reading code is as good as you can get it to be on both versions of your reader. There is no reason for one to work fine while the other fails.

If you can't fix the issue removing the duplicate write you might want to:

-Increase the size of your RX buffer (for Windows only), see here, you can do it with ser.set_buffer_size(rx_size = size_bigger_than_4_Kb) and/or using Windows Control Panel. I've never tried this but for a known and one-off non-continuous exchange, this might help.

-Try with a different programming language. Somewhere else you said you had this running with a different software. Python is very good and efficient but it might not be the best there is for what you're trying to do.

-Implement some kind of flow control, hardware or software, either in Python or any other language. The downside is you need flow control to be supported and implemented on both sides of the link. Flow control is the way to go if you want to be completely sure your data flows smoothly.

-Reduce baud rate

Post a Comment for "I Am Using Serial Communication Module In Python..but Data Received Is Not Proper..it Is Unstable And Changes Most Of The Time"