From 2b69be2874abae99ef4cd1dd49ba4851bf8b91df Mon Sep 17 00:00:00 2001 From: sean Date: Thu, 23 May 2019 18:54:00 -0700 Subject: [PATCH] Finally working? --- Main.py | 32 ++++++++++++++++++++------------ Messages.py | 3 +-- ProcessList.py | 30 ++++++++++++++++++++++++++++++ plotVoltages.py | 24 ++++++++++++++++++++++++ processString.py | 9 --------- 5 files changed, 75 insertions(+), 23 deletions(-) create mode 100644 ProcessList.py create mode 100644 plotVoltages.py delete mode 100644 processString.py diff --git a/Main.py b/Main.py index 557e8fa..cbea637 100644 --- a/Main.py +++ b/Main.py @@ -1,47 +1,55 @@ - import Messages import time -import processString +from ProcessList import processList from PCANBasic import * from PCANBasic import TPCANTimestamp, TPCANMsg PCAN = PCANBasic() # type: PCANBasic res = PCAN.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K) +PCAN.FilterMessages(PCAN_USBBUS1, 1979, 1979, PCAN_MESSAGE_STANDARD) while True: print("new") - v = "" + v = [] + time.sleep(.005) PCAN.Write(PCAN_USBBUS1, Messages.rCellVInit) - #time.sleep(.050) - cellMessage = False # type: bool + cellMessage = False # Keep reading messages until a cell voltage message is received while (cellMessage == False): print("in loop") time.sleep(.016) MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] - #print(MSG.ID) + print(MSG.ID) if MSG.ID == 0x7bb: cellMessage = True print("out of loop") - v = "%s%s%s%s" % (MSG.DATA[4], MSG.DATA[5], MSG.DATA[6], MSG.DATA[7]) + s = "%s%s%s%s" % (MSG.DATA[4], MSG.DATA[5], MSG.DATA[6], MSG.DATA[7]) + v.append(int(s)) print(v) + #PCAN.Write(PCAN_USBBUS1, Messages.rCellV) for i in range(0, 28): - print("in for #%s " % i) + print("*************** i = #%s " % i) #time.sleep(.016) PCAN.Write(PCAN_USBBUS1, Messages.rCellV) #print("message sent") cellMessage = False while (cellMessage == False): - time.sleep(.5) + time.sleep(.001) MSG = PCAN.Read(PCAN_USBBUS1) MSG = MSG[1] - print(MSG.ID) + if MSG.ID != 0: + print(MSG.ID) + time.sleep(.010) if MSG.ID == 0x7bb: cellMessage = True print("true!") - v = v + "%s%s%s%s%s%s%s" % (MSG.DATA[1], MSG.DATA[2], MSG.DATA[3], MSG.DATA[4], MSG.DATA[5], MSG.DATA[6], MSG.DATA[7]) + s = s + "%s%s%s%s%s%s%s" % (MSG.DATA[1], MSG.DATA[2], MSG.DATA[3], MSG.DATA[4], MSG.DATA[5], MSG.DATA[6], MSG.DATA[7]) + v.append(int(s)) + + print(v) + print(len(v)) + #processString(v) - processString(v) \ No newline at end of file diff --git a/Messages.py b/Messages.py index 683d41a..ed7f15d 100644 --- a/Messages.py +++ b/Messages.py @@ -1,5 +1,4 @@ from PCANBasic import * -import array rCellVInit = TPCANMsg() @@ -8,7 +7,7 @@ rCellVInit.MSGTYPE = 0x00 rCellVInit.LEN = 8 rCellVInit.DATA[0] = 0x02 rCellVInit.DATA[1] = 0x21 -rCellVInit.DATA[2] = 0x04 +rCellVInit.DATA[2] = 0x02 rCellVInit.DATA[3] = 0x00 rCellVInit.DATA[4] = 0x00 rCellVInit.DATA[5] = 0x00 diff --git a/ProcessList.py b/ProcessList.py new file mode 100644 index 0000000..77994cf --- /dev/null +++ b/ProcessList.py @@ -0,0 +1,30 @@ +from math import ceil + + +def processList(v): + voltages = [] + voltages[0] = v[0] & 0x00000000ffff0000 + voltages[1] = v[1] & 0x000000000000ffff + for i in range(1, 27): + if i%2 == 1: + v1 = (v[i] & 0x00ffff0000000000) >> 40 + v2 = (v[i] & 0x000000ffff000000) >> 20 + v3 = (v[i] & 0x0000000000ffff00) >> 8 + vh = v[i] & 0x00000000000000ff + voltages[ceil(2 + 3.5 (i - 1))] = v1 + voltages[ceil(2 + 3.5 * (i - 1) + 1)] = v2 + voltages[ceil(2 + 3.5 * (i - 1) + 2)] = v3 + voltages[ceil(2 + 3.5 * (i - 1) + 3)] = vh << 8 + elif i%2 == 0: + vh = (v[i] & 0x00ff000000000000) >> 48 + v1 = (v[i] & 0x0000ffff00000000) >> 28 + v2 = (v[i] & 0x00000000ffff0000) >> 16 + v3 = v[i] & 0x000000000000ffff + voltages[ceil(2 + 3.5 * (i - 1) - 1)] = voltages[ceil(2 + 3.5 * (i -1 ) - 1)] & vh + voltages[ceil(2 + 3.5 * (i - 1))] = v1 + voltages[ceil(2 + 3.5 * (i - 1) + 1)] = v2 + voltages[ceil(2 + 3.5 * (i - 1) + 2)] = v3 + voltages[96] = voltages[96] & ((v[28] & 0x00ff000000000000) >> 48) + for i in range(0, 95): + print("Cell " + str(z + 1) + "= %.2f" % (voltages[i])) + return diff --git a/plotVoltages.py b/plotVoltages.py new file mode 100644 index 0000000..30aea4f --- /dev/null +++ b/plotVoltages.py @@ -0,0 +1,24 @@ +import matplotlib.pyplot as plt +import time + + +def plotVoltages(vList): + if ~plt.fignum_exists(1): + print("true") + plt.ylabel("Voltage (V)") + plt.xlabel("Cell Number") + plt.bar(vList, [1,2,3,4]) + plt.show() + else: + plt.clf() + plt.draw() + return + + + +x = [1, 2, 3, 4] +plotVoltages(x) +time.sleep(3) +plt.clf() +x = [4,3,2,1] +plotVoltages(x) diff --git a/processString.py b/processString.py deleted file mode 100644 index 41dc4c4..0000000 --- a/processString.py +++ /dev/null @@ -1,9 +0,0 @@ -def processString(s): - voltages = [] - for i in range(0, 96): - x = s[i*4:i*4+4] - x = int(x, 16)/100 - voltages.append(x) - for z in range(0, 96): - print("Cell " + str(z + 1) + "= %.2f" % (voltages[z])) - return