Hello everyone, I am using a Python script to get data from a BlueTooth device but it seems there is some problem when reading from the interface on Linux.
I have a three devices (PowerSpy) that measure energy from an outlet (I
plug a computer on a PowerSpy that I plug on an outlet). This device
communicates using BlueTooth. So I connect my laptop via BT to get those
data (using : sudo rfcomm bind /dev/rfcomm0 powerspy_mac_address).
Then, I execute my Python script. This script reads data
from /dev/rfcomm0 (with root priviledge). The first data is
successfully read. The second and the third too. But the fourth time
that I access /dev/rfcomm0, I get some odd caracteres: A
I tried to access to this interface using minicom, no problem, I can
read easily these data. I changed the computer that was under test, no
change. I changed every physical devices by other, no change.
That's why, I think I have a problem with my script.
Here is the error:
Traceback (most recent call last):
4107A6A
File "./PowerSpy.py", line 64, in <module>
powerspy = PowerSpy("/dev/rfcomm0")
File "./PowerSpy.py", line 33, in __init__
u = (struct.unpack('<f', Ucalib.decode('hex'))[0]) # Unpack in
LittleIndian + float (see IEEE 32-bit float representation)
File "/usr/lib/python2.7/encodings/hex_codec.py", line 42, in
hex_decode
output = binascii.a2b_hex(input)
TypeError: Odd-length string
The python file is in the attachement.
I am using Ubuntu 11.04 with Python 2.7.1+.
Do you have some clues to help me resolving this problem ?
Thanks,
Reg <<orange_logo.gif>> ards,
--
Rémi Druilhe
RD-MAPS-GRE
OLNC/MAPS/SHINE/MADE
Phone: +33 (0)4 76 76 24 27
E-mail : [email protected]
Orange Labs Grenoble - 28 chemin du Vieux Chêne - BP98 38243 Meylan
Cedex - France
<<attachment: orange_logo.gif>>
#!/usr/bin/python
"""PowerSpy"""
import os
import sys
import time
import math
import struct
class PowerSpy:
"""Handles reading power values from PowerSpy"""
TTY = 0
Pcalib = 0
def __init__(self, tty):
"""Open the Serial communication file
and start real time mode on the plug"""
self.TTY = open(tty,'a+')
# print(self.TTY)
#Get calibration parameters
self.TTY.write("<V02>")
Ucalib = self.TTY.read(4).strip('<').strip('>')
self.TTY.write("<V03>")
Ucalib = Ucalib + self.TTY.read(4).strip('<').strip('>')
self.TTY.write("<V04>")
Ucalib = Ucalib + self.TTY.read(4).strip('<').strip('>')
# self.TTY.write("<V05>")
# self.TTY.read(4)
self.TTY.write("<V05>")
Ucalib = Ucalib + self.TTY.read(4).strip('<').strip('>')
# print Ucalib
u = (struct.unpack('<f', Ucalib.decode('hex'))[0]) # Unpack in LittleIndian + float (see IEEE 32-bit float representation)
self.TTY.write("<V06>")
Icalib = self.TTY.read(4).strip('<').strip('>')
self.TTY.write("<V07>")
Icalib = Icalib + self.TTY.read(4).strip('<').strip('>')
self.TTY.write("<V08>")
Icalib = Icalib + self.TTY.read(4).strip('<').strip('>')
self.TTY.write("<V09>")
Icalib = Icalib + self.TTY.read(4).strip('<').strip('>')
# print Icalib
i=(struct.unpack('<f', Icalib.decode('hex'))[0])
self.Pcalib = u*i
self.TTY.write("<J50>") #start real time reading averaging power over 50 periods
self.TTY.readline()
def get_value(self):
value = self.TTY.readline().split()[2]
V = int(value,16)
# print(V)
# print(V*V)
return V * self.Pcalib
def close(self):
self.TTY.write("<R>")
self.TTY.close()
if __name__ == "__main__":
powerspy = PowerSpy("/dev/rfcomm0")
print("powerspy reported consumption :")
print("Press Ctrl-c to exit")
while True:
try:
print(powerspy.get_value())
except KeyboardInterrupt:
break
print("finish")
powerspy.close()
-- http://mail.python.org/mailman/listinfo/python-list
