python - blocking read(1) with timeout in pyserial -


i use following piece of code read serial port until terminating character.

"""read until see terminating character timeout"""     response=[]     byte_read=''     break_yes=0     time_now = time.clock()     while ((not (byte_read=='\r') ) , (break_yes==0)):         byte_read = self.ser.read(1)         if (not(len(byte_read)== 0) , (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. check code"             break_yes=1     if break_yes==0:         return ''.join(response)     else:         return 'fail' 

this works because of while loop, cpu resources taken up.

i think having blocking read(1) timeout save of cpu. flag looking c "min == 0, time > 0 (read timeout)" in termios

i looking similar flag in python.

i use io.readline read till '\r', want stick pyserial as possible without other dependency.

would appreciate advice. let me know if should in different way either too.

thanks,

you should read documentation of pyserial: states timeout of 0 pass constructor turn on non-blocking behaviour:

http://pyserial.sourceforge.net/pyserial_api.html#classes

just rid of timeout parameter, , should set.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -