Python Sandbox

Trunet's Place - Portfolio
Jump to: navigation, search

pypi package update

# git tag -s -m "Version 0.0.5" 0.0.5
# python setup.py sdist upload
# python setup.py bdist_egg upload

INT to 8-bits chunks

Converting INT(16-bits) to 8-bits chunks to be transferred using serial and/or xbee.

delay = 10000
d1 = delay & 0xff
d2 = delay >> 8
ser.write(chr(d1) + chr(d2))

Decode 2 x 8-bits chunks to INT

This will decode 2 x 8-bits chunks into an INT variable.

from struct import unpack
 
def decodeInt(var):
	text = ""
	for i in range(0, len(var)):
		text += var[i]
	return unpack('h', text)[0]
 
decodeInt(\x40\x5D)


FLOAT to 8-bits chunks

Converting FLOAT(32-bits) to 8-bits chunks to be transferred using serial and/or xbee.

from struct import unpack, pack
 
vcal = 0.270419
vcal = unpack('<L', pack('<f', vcal))[0]
v1 = vcal & 0xff
v2 = (vcal >> 8) & 0xff
v3 = (vcal >> 16) & 0xff
v4 = (vcal >> 24) & 0xff
ser.write(chr(v1) + chr(v2) + chr(v3) + chr(v4))

Decode 4 x 8-bits chunks to FLOAT

This will decode 4 x 8-bits chunks into a FLOAT variable.

from struct import unpack
 
def decodeFloat(var):
	text = ""
	for i in range(0, len(var)):
		text += var[i]
	return unpack('f', text)[0]
 
decodeFloat(\x40\x5D\x35\x04)