
import Messages
import time
import processString
from PCANBasic import *
from PCANBasic import TPCANTimestamp, TPCANMsg

PCAN = PCANBasic()  # type: PCANBasic
res = PCAN.Initialize(PCAN_USBBUS1, PCAN_BAUD_500K)

while True:
    print("new")
    v = ""
    PCAN.Write(PCAN_USBBUS1, Messages.rCellVInit)
    #time.sleep(.050)
    cellMessage = False  # type: bool
    # 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)
        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])
    print(v)

    for i in range(0, 28):
        print("in for #%s " % i)
        #time.sleep(.016)
        PCAN.Write(PCAN_USBBUS1, Messages.rCellV)
        #print("message sent")
        cellMessage = False
        while (cellMessage == False):
            time.sleep(.5)
            MSG = PCAN.Read(PCAN_USBBUS1)
            MSG = MSG[1]
            print(MSG.ID)
            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])


    processString(v)