yuvraj
yuvraj

Reputation: 81

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 very fast rate but when I use this code I am getting data at a very slow rate, maybe around 2 - 3 data per second. This is too slow because I want to do real time plotting.

So, instead of 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 speed at which data is updated is the same but instead of a single byte I am checking a number of bytes in input queue and reading them. I am receiving around 3850 bytes per loop so this one appears much faster to me but in fact it is almost the same, the only change is that I am not reading a greater number of bytes.

I want to read a single byte and check for the real time it was received. To do so I cannot use second method where I use ser.inWaiting(). How can I read single byte data faster than using the approaches above?

Upvotes: 3

Views: 7884

Answers (1)

Logic1
Logic1

Reputation: 1847

Here's some test code I wrote for a project that you can try different baud settings with. Basically it sends out some data on Tx (which could be connected directly to Rx) and expects the data to be echoed back. It then compares the returned data with the sent data and lets you know if/when errors occur. Note that if there are no errors then the output will remain blank and at the end of 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"

Upvotes: 1

Related Questions