forked from tomcosk/BMSBatteryMonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbmscore.py
59 lines (50 loc) · 1.96 KB
/
bmscore.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
#!/usr/bin/python
# *****Core routines to retrieve and store data to BMS PCBs*****
# Copyright (C) 2017 Simon Richard Matthews
# Project loaction https://github.com/simat/BatteryMonitor
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import time
import serial
import binascii
import json
def crccalc(data):
"""returns crc as integer from byte stream"""
crc = 0x10000
for i in data:
crc = crc-int(i)
return crc
def rdjson(file):
with open(file, "r") as read_file:
data = json.load(read_file)
return data
def openbms(port='/dev/ttyUSB0'):
ser = serial.Serial(port) # open serial port
ser.timeout = 3
return ser
def getbmsdat(port, command):
""" Issue BMS command and return data as byte data """
""" assumes data port is open and configured """
# print('command=', binascii.hexlify(command))
port.write(command)
reply = port.read(4) # read 4 bytes. Headers commands etc.
# read bytes 3 and 4. length of data
length = int.from_bytes(reply[3:5], byteorder='big')
data = port.read(length) # read data bytes
end = port.read(3)
if len(data) < length:
print('Serial Timeout')
if crccalc(reply[2:4]+data) != int.from_bytes(end[0:2], byteorder='big'):
return None
# print('CRC Error')
# print('reply=', binascii.hexlify(data))
return data