serial port - python 2.7 - converting float to bytes and looping through the bytes -


i'm trying send float series of 4 bytes on serial. have code looks works:

ser.write(b'\xcd') #sending byte representation of 0.1 ser.write(b'\xcc') ser.write(b'\xcc')  ser.write(b'\x3d') 

but want able send arbitary float.

i want able go through each byte individually won't example:

bytes = struct.pack('f',float(0.1)) ser.write(bytes) 

because want check each byte.

i'm using python 2.7 how can this?

you can use struct module pack float binary data. loop through each byte of bytearray , write them output.

import struct  value = 13.37  # arbitrary float  bin = struct.pack('f', value)  b in bin:     ser.write(b) 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -