How to quickly read single byte serial data using Python

I am reading serial data in Python using the following code:

port = "COM11"
baud = 460800
timeout=1

ser = serial.Serial()
ser.port = port
ser.baudrate = baud
ser.timeout = timeout
while 1:
     # Read from serial port, blocking
     data =ser.read(1)
     print data
     # some further processing of data

      

I am sending data at a very high speed, but when I use this code, I am receiving data at a very low speed, maybe about 2 - 3 data per second. It's too slow because I want to do real-time plotting.

So, instead of the above code, I tried:

 while 1:
     # Read from serial port, blocking
     data =ser.read(1)
     data1=(data)


     # If there is more than 1 byte, read the rest
     n = ser.inWaiting()
     data1 = (data1 + ser.read(n))
     print data1

      

Now the data refresh rate is the same, but instead of one byte, I check the number of bytes in the input queue and read them. I am getting about 3850 bytes per cycle, so this looks much faster to me, but in fact it is almost the same, the only change is that I am not reading more bytes.

I want to read one byte and check if it is received in real time. For this, I cannot use the second method where I am using ser.inWaiting()

. How can I read single byte data faster than using the above approaches?

+3


source to share


1 answer


Here is some test code I wrote for a project that you can try different baud settings with. Basically it sends some data over Tx (which can be connected directly to Rx) and waits for the data to be recalled back. It then compares the returned data with the submitted data and lets you know if any errors have occurred. Note that if there are no errors, the output will remain blank and at the end of the test it will print "0 Comm Errors".



import serial, time

test_data = "hello this is so freakin cool!!!" + '\r' #Must always be terminated with '\r'
echo_timeout = 1 #the time allotted to read back the test_data string in seconds
cycleNum = 0
errors = 0
try:
        ser = serial.Serial(port="COM1", baudrate=115200, timeout=1)
        ser.flush()
        print "starting test"
        for x in xrange(100):
                cycleNum += 1
                d = ser.write(test_data)
                ret_char = returned = ''
                start_time = time.time()
                while (ret_char <> '\r') and (time.time() - start_time < echo_timeout):
                        ret_char = ser.read(1)
                        returned += ret_char
                if not returned == test_data:
                    errors += 1
                    print "Cycle: %d Sent: %s Received: %s" % (cycleNum, repr(test_data), repr(returned) )
except Exception as e:
        print 'Python Error:', e
finally:
        if 'ser' in locals():
                print "%d Comm Errors" % errors
                if ser.isOpen():
                        ser.close()
                        print "Port Was Successfully Closed"
                else:
                        print "Port Already Closed"
        else:
                print "Serial Variable Was Never Initialized"

      

0


source







All Articles