Python Serial Read Timeout Example

  
Python Serial Read Timeout Example Rating: 9,7/10 2469votes
Astrill Read Time OutRead Time Out Error

I use the following piece of code to read the serial port until i get a terminating character. ''Read until you see a terminating character with a timeout'' response=[] byte_read=' break_yes=0 time_now = time.clock() while ((not (byte_read==' r') ) and (break_yes==0)): byte_read = self.ser.read(1) if (not(len(byte_read)== 0) and (not (byte_read ==' r'))): response.append(byte_read) if ( time.clock() - time_now >1 ): if self.DEBUG_FLAG: print '[animatics Motor class] time out occured.

Mar 27, 2017 - LineReader (usage example at the bottom of the page); try to see what its doing on the serial. With serial.Serial('COM3', 9600, timeout=1) as s: s.write(b'acq r') time.sleep(10) s.write(b'stp r') t1 = time.time() reading = s. 5.1 Surround Mp3 Download. readall() t2 = time.time().

Check code' break_yes=1 if break_yes==0: return '.join(response) else: return 'FAIL' This works well but because of the while loop, the cpu resources are taken up. I think that having a blocking read(1) with a timeout will save some of the cpu. The flag i am looking for C is 'MIN == 0, TIME >0 (read with timeout)' in termios i am looking for a similar flag in Python. I could also use the io.readline to read till i get ' r', but i want to stick to pyserial as much as possible without any other dependency. Would greatly appreciate advice. Zacchaeus Template. Do let me know if i should do it in a completely different way either too. According to the documentation, read() should be a blocking operation.

You don't show how you open the serial port, please supply that part, too. Beyond that, your code is quite unpythonic.

Python has bools, don't use integers as flags. It also has a!=-operator, no need to use a confusing 'not a == b'. Python has a break-statement to leave loops, no need to emulate that with a flag. Using that would remove more or less 80% of your code, while keeping the same semantics.

Returning strings with 'FAIL' as error-message when the expected result is also a string calls for trouble. – Jun 18 '15 at 22:47 •. Narciso E Boccadoro Audiolibri Mp3. Aight, so I found out a way.

Instead of polling with the no timeout, I use the select module in python, which is similar to the one in C. It returns if any data is available immediately, or waits for the timeout period and exits, which is precisely what i wanted. I took deets comments for cleaning up the code and it looks like so now. Def readOnly(self): ''Read until you see a terminating character with a timeout'' response=[] byte_read=' while (not (byte_read==' r')): reading,_,_ = select.select([self.ser], [], [], 1) #returns immediately if there is data on serial port.

Waits 1 second to timeout if not. If reading!=[]: #something is to be read on the file descriptor byte_read = self.ser.read(1) if (byte_read!=' r'): response.append(byte_read) else: #' r' received return '.join(response) break else: if self.DEBUG_FLAG: print '[Motor class] time out occured. Check code' return 'FAIL' break ` This decreased the cpu usage from 50% to 5% so life is better now.