-
Notifications
You must be signed in to change notification settings - Fork 1
/
enmon.py
118 lines (104 loc) · 3.1 KB
/
enmon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env python3
#coding=utf-8
import serial
import time
import struct
class BTPOWER:
setAddrBytes = [0xB4,0xC0,0xA8,0x01,0x01,0x00,0x1E]
readVoltageBytes = [0xB0,0xC0,0xA8,0x01,0x01,0x00,0x1A]
readCurrentBytes = [0XB1,0xC0,0xA8,0x01,0x01,0x00,0x1B]
readPowerBytes = [0XB2,0xC0,0xA8,0x01,0x01,0x00,0x1C]
readRegPowerBytes = [0XB3,0xC0,0xA8,0x01,0x01,0x00,0x1D]
def __init__(self, com="/dev/ttyUSB0", timeout=10.0):
self.ser = serial.Serial(
port=com,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout = timeout
)
if self.ser.isOpen():
self.ser.close()
self.ser.open()
def checkChecksum(self, _tuple):
_list = list(_tuple)
_checksum = _list[-1]
_list.pop()
_sum = sum(_list)
if _checksum == _sum%256:
return True
else:
raise Exception("Wrong checksum")
def isReady(self):
self.ser.write(serial.to_bytes(self.setAddrBytes))
rcv = self.ser.read(7)
if len(rcv) == 7:
unpacked = struct.unpack("!7B", rcv)
if(self.checkChecksum(unpacked)):
return True
else:
raise serial.SerialTimeoutException("Timeout setting address")
def readVoltage(self):
self.ser.write(serial.to_bytes(self.readVoltageBytes))
rcv = self.ser.read(7)
if len(rcv) == 7:
unpacked = struct.unpack("!7B", rcv)
if(self.checkChecksum(unpacked)):
tension = unpacked[2]+unpacked[3]/10.0
return tension
else:
raise serial.SerialTimeoutException("Timeout reading tension")
def readCurrent(self):
self.ser.write(serial.to_bytes(self.readCurrentBytes))
rcv = self.ser.read(7)
if len(rcv) == 7:
unpacked = struct.unpack("!7B", rcv)
if(self.checkChecksum(unpacked)):
current = unpacked[2]+unpacked[3]/100.0
return current
else:
raise serial.SerialTimeoutException("Timeout reading current")
def readPower(self):
self.ser.write(serial.to_bytes(self.readPowerBytes))
rcv = self.ser.read(7)
if len(rcv) == 7:
unpacked = struct.unpack("!7B", rcv)
if(self.checkChecksum(unpacked)):
power = unpacked[1]*256+unpacked[2]
return power
else:
raise serial.SerialTimeoutException("Timeout reading power")
def readRegPower(self):
self.ser.write(serial.to_bytes(self.readRegPowerBytes))
rcv = self.ser.read(7)
if len(rcv) == 7:
unpacked = struct.unpack("!7B", rcv)
if(self.checkChecksum(unpacked)):
regPower = unpacked[1]*256*256+unpacked[2]*256+unpacked[3]
return regPower
else:
raise serial.SerialTimeoutException("Timeout reading registered power")
def readAll(self):
if(self.isReady()):
return(self.readVoltage(),self.readCurrent(),self.readPower(),self.readRegPower())
def close(self):
self.ser.close()
if __name__ == "__main__":
try:
sensor = BTPOWER()
print("Checking readiness")
print(sensor.isReady())
print("Reading voltage")
voltage = sensor.readVoltage()
print(type(voltage))
print("Reading current")
print(sensor.readCurrent())
print("Reading power")
print(sensor.readPower())
print("reading registered power")
print(sensor.readRegPower())
print("reading all")
print(sensor.readAll())
finally:
sensor.close()