Ejemplo n.º 1
0
    def read_settings(self):
        for command in self.protocol_config['settings']['commandSequence']:
            data = bmscore.getbmsdat(self.serial, bytes.fromhex(command))

        reply = {}
        for key in self.protocol_config['settings']['registers']:
            data = bmscore.getbmsdat(
                self.serial,
                bytes.fromhex(self.protocol_config['settings']['registers']
                              [key]['command']))
            reply[key] = int(binascii.hexlify(data), 16)

        return reply
Ejemplo n.º 2
0
def getdat(port='/dev/ttyUSB0'):
    """ Get data from BMS board"""
    ser = bmscore.openbms(port)
    command = bytes.fromhex('DD A5 03 00 FF FD 77')
    dat = bmscore.getbmsdat(ser, command)
    dat = bmscore.getbmsdat(ser, command)
    rawi = int.from_bytes(dat[2:4], byteorder='big', signed=True)
    rawv = int.from_bytes(dat[0:2], byteorder='big', signed=True)
    balance = bin(int.from_bytes(dat[12:14], byteorder='big', signed=True))
    state = int.from_bytes(dat[16:18], byteorder='big', signed=True)
    fets = bin(int.from_bytes(dat[20:21], byteorder='big', signed=True))
    print("V={} I={} bal={} state={} fets={}".format(rawv, rawi, balance,
                                                     bin(state), fets))
    errors=['Cell Overvoltage','Cell Undervoltage','Battery Overvoltage', \
            'Battery Undervoltage','Charge Overtemp','Charge Undertemp', \
            'Discharge Overtemp','Discharge Undertemp','Charge Overcurrent' \
            'Discharge Overcurrent','Short Circuit','IC Fault','Software MOS lock']
    for i in range(state.bit_length()):
        if 2**i & state:
            print(errors[i])

#  line1 = [ 0 for i in range(int(len(dat)))]
#  for i in range(0,int(len(dat))):
#    print (dat[i*2:i*2+2])
#    print (int.from_bytes(dat[i:i+1], byteorder = 'big'))
#    line1[i] = int.from_bytes(dat[i:i+1], byteorder = 'big')
    print(binascii.hexlify(dat))
    #  print (line1)

    # voltages
    command = bytes.fromhex('DD A5 04 00 FF FC 77')
    voltages = bmscore.getbmsdat(ser, command)
    ser.close
    print(binascii.hexlify(voltages))
    rawv = [0.0 for i in range(15)]
    for i in range(15):
        rawv[i] = int.from_bytes(voltages[i * 2:i * 2 + 2],
                                 byteorder='big') / 1000.00
#    rawv[i] = rawv[i]+rawv[i-1]
    print(rawv)

    command = bytes.fromhex('DD A5 05 00 FF FB 77')
    dat = bmscore.getbmsdat(ser, command)

    #  line1 = [ 0 for i in range(int(len(dat)))]
    #  for i in range(0,int(len(dat))):
    #    print (dat[i*2:i*2+2])
    #    print (int.from_bytes(dat[i:i+1], byteorder = 'big'))
    #    line1[i] = int.from_bytes(dat[i:i+1], byteorder = 'big')
    print(binascii.hexlify(dat))
Ejemplo n.º 3
0
    def get_basic_info(self):
        data = bmscore.getbmsdat(
            self.serial,
            bytes.fromhex(self.protocol_config['basicInfo']['command']))
        reply = {}
        if data is not None:
            for key in self.protocol_config['basicInfo'][
                    'replyDataStringBytes']:
                byte_from = self.protocol_config['basicInfo'][
                    'replyDataStringBytes'][key]['from']
                byte_to = self.protocol_config['basicInfo'][
                    'replyDataStringBytes'][key]['to']
                reply[key] = int(binascii.hexlify(data[byte_from:byte_to]), 16)
            # reply['overall_current'] = int(binascii.hexlify(data[2:4]), 16)
            # reply['residual_capacity'] = int(binascii.hexlify(data[4:6]), 16)
            # reply['nominal_capacity'] = int(binascii.hexlify(data[6:8]), 16)
            # reply['cycle_times'] = int(binascii.hexlify(data[8:10]), 16)
            # reply['rsoc'] = int(binascii.hexlify(data[19:20]), 16)
            # reply['battery_serial_number'] = int(binascii.hexlify(data[21:22]), 16)
            # reply['ntc_quantity'] = int(binascii.hexlify(data[22:23]), 16)

            start_byte = 23
            for i in range(0, reply['ntc_quantity']):
                reply['ntc_' + str(i) + '_kelvin'] = int(
                    binascii.hexlify(data[start_byte:start_byte + 2]), 16)
                reply['ntc_' + str(i) + '_celsius'] = round(
                    self.kelvin2celsius(
                        int(binascii.hexlify(data[start_byte:start_byte + 2]),
                            16) / 10) * 100) / 100
                start_byte = start_byte + 2
        else:
            return None

        return reply
Ejemplo n.º 4
0
def switchfets(port='/dev/ttyUSB0'):
  """ switch charge and discharge fets """
  print ('(03)=Both FETs off')
  print ('(01)=Discharge FET on, Charge FET off')
  print ('(02)=Discharge FET off, Charge FET on')
  print ('(00)=Both FETs on')
  usercmd = input("Enter numeric option> ")
  ser = bmscore.openbms(port)
  command = bytes.fromhex('DD A5 03 00 FF FD 77')
  print ('command=',binascii.hexlify(command))
  data=bmscore.getbmsdat(ser,command)
  print ('reply=',binascii.hexlify(data))
  command = bytes.fromhex('DD A5 04 00 FF FC 77')
  print ('command=',binascii.hexlify(command))
  data=bmscore.getbmsdat(ser,command)
  print ('reply=',binascii.hexlify(data))
  command = bytes.fromhex('DD 5A 00 02 56 78 FF 30 77')
  data=bmscore.getbmsdat(ser,command)
  print ('reply=',binascii.hexlify(data))
  usercmd=b'\xE1\x02\x00'+bytes.fromhex(usercmd)
  command = b'\xDD\x5A'+usercmd+bmscore.crccalc(usercmd).to_bytes(2, byteorder='big')+b'\x77'
  print (binascii.hexlify(command))
  bmscore.getbmsdat(ser,command)
  command = bytes.fromhex('DD 5A 01 02 00 00 FF FD 77')
  bmscore.getbmsdat(ser,command)
Ejemplo n.º 5
0
 def get_vendor(self):
     data = bmscore.getbmsdat(
         self.serial,
         bytes.fromhex(self.protocol_config['vendor']['command']))
     if data is not None:
         text = str(binascii.unhexlify(binascii.hexlify(data)))
     else:
         text = 'Unknown'
     return text
Ejemplo n.º 6
0
def getdat(port='/dev/ttyUSB0'):
    """ Get data from BMS board"""
    ser = bmscore.openbms(port)
    command = bytes.fromhex('DD A5 03 00 FF FD 77')
    dat = bmscore.getbmsdat(ser, command)
    rawi = int.from_bytes(dat[2:4], byteorder='big', signed=True)
    rawv = int.from_bytes(dat[0:2], byteorder='big', signed=True)
    balance = int.from_bytes(dat[12:14], byteorder='big', signed=True)
    state = int.from_bytes(dat[16:18], byteorder='big', signed=True)
    fets = int.from_bytes(dat[20:21], byteorder='big', signed=True)
    print("V={} I={} bal={} state={} fets={}".format(rawv, rawi, balance,
                                                     state, fets))
    #  line1 = [ 0 for i in range(int(len(dat)))]
    #  for i in range(0,int(len(dat))):
    #    print (dat[i*2:i*2+2])
    #    print (int.from_bytes(dat[i:i+1], byteorder = 'big'))
    #    line1[i] = int.from_bytes(dat[i:i+1], byteorder = 'big')
    print(binascii.hexlify(dat))
    #  print (line1)

    # voltages
    command = bytes.fromhex('DD A5 04 00 FF FC 77')
    voltages = bmscore.getbmsdat(ser, command)
    ser.close
    print(binascii.hexlify(voltages))
    rawv = [0.0 for i in range(15)]
    for i in range(15):
        rawv[i] = int.from_bytes(voltages[i * 2:i * 2 + 2],
                                 byteorder='big') / 1000.00
        rawv[i] = rawv[i] + rawv[i - 1]
    print(rawv)

    command = bytes.fromhex('DD A5 05 00 FF FB 77')
    dat = bmscore.getbmsdat(ser, command)

    #  line1 = [ 0 for i in range(int(len(dat)))]
    #  for i in range(0,int(len(dat))):
    #    print (dat[i*2:i*2+2])
    #    print (int.from_bytes(dat[i:i+1], byteorder = 'big'))
    #    line1[i] = int.from_bytes(dat[i:i+1], byteorder = 'big')
    print(binascii.hexlify(dat))
Ejemplo n.º 7
0
def getdat(port='/dev/ttyUSB0'):
  """ Get data from BMS board"""
  ser = bmscore.openbms(port)
  command = bytes.fromhex('DD A5 03 00 FF FD 77')
  dat = bmscore.getbmsdat(ser,command)
  rawi = int.from_bytes(dat[2:4], byteorder = 'big',signed=True)
  rawv = int.from_bytes(dat[0:2], byteorder = 'big',signed=True)
  balance = int.from_bytes(dat[12:14], byteorder = 'big',signed=True)
  state = int.from_bytes(dat[16:18], byteorder = 'big',signed=True)
  fets = int.from_bytes(dat[20:21], byteorder = 'big',signed=True)
  print ("V={} I={} bal={} state={} fets={}".format(rawv,rawi,balance,state,fets))
#  line1 = [ 0 for i in range(int(len(dat)))]
#  for i in range(0,int(len(dat))):
#    print (dat[i*2:i*2+2])
#    print (int.from_bytes(dat[i:i+1], byteorder = 'big'))
#    line1[i] = int.from_bytes(dat[i:i+1], byteorder = 'big')
  print (binascii.hexlify(dat))
#  print (line1)


  # voltages
  command = bytes.fromhex('DD A5 04 00 FF FC 77')
  voltages = bmscore.getbmsdat(ser,command)
  ser.close
  print (binascii.hexlify(voltages))
  rawv = [ 0.0 for i in range(15)]
  for i in range(15):
    rawv[i] = int.from_bytes(voltages[i*2:i*2+2], byteorder = 'big')/1000.00
    rawv[i] = rawv[i]+rawv[i-1]
  print (rawv)

  command = bytes.fromhex('DD A5 05 00 FF FB 77')
  dat = bmscore.getbmsdat(ser,command)

#  line1 = [ 0 for i in range(int(len(dat)))]
#  for i in range(0,int(len(dat))):
#    print (dat[i*2:i*2+2])
#    print (int.from_bytes(dat[i:i+1], byteorder = 'big'))
#    line1[i] = int.from_bytes(dat[i:i+1], byteorder = 'big')
  print (binascii.hexlify(dat))
Ejemplo n.º 8
0
    def get_cells_info(self):
        while self.num_cells is None:
            self.set_num_cells
            time.sleep(1)

        data = bmscore.getbmsdat(
            self.serial,
            bytes.fromhex(self.protocol_config['cellsInfo']['command']))
        reply = {}
        if data is not None:
            start_byte = 0
            for i in range(0, self.num_cells):
                # print("reading cell: "+str(start_byte)+":"+str(start_byte+2))
                reply['cell_' + str(i)] = int(
                    binascii.hexlify(data[start_byte:start_byte + 2]), 16)
                start_byte = start_byte + 2
        else:
            return None
        return reply
Ejemplo n.º 9
0
def switchfets(port='/dev/ttyUSB0'):
  """ switch charge and discharge fets """
  print ('(03)=Both FETs off')
  print ('(01)=Charge FET on, Discharge FET off')
  print ('(02)=Charge FET off, Discharge FET on')
  print ('(00)=Both FETs on')
  usercmd = input("Enter numeric option> ")
  ser = bmscore.openbms(port)
  command = bytes.fromhex('DD A5 03 00 FF FD 77')
  print ('command=',binascii.hexlify(command))
  data=bmscore.getbmsdat(ser,command)
  print ('reply=',binascii.hexlify(data))
  command = bytes.fromhex('DD A5 04 00 FF FC 77')
  print ('command=',binascii.hexlify(command))
  data=bmscore.getbmsdat(ser,command)
  print ('reply=',binascii.hexlify(data))
  command = bytes.fromhex('DD 5A 00 02 56 78 FF 30 77')
  getbmsdat(ser,command)
  usercmd=b'\xE1\x02\x00'+bytes.fromhex(usercmd)
  command = b'\xDD\x5A'+usercmd+bmscore.crccalc(usercmd).to_bytes(2, byteorder='big')+b'\x77'
  print (binascii.hexlify(command))
  bmscore.getbmsdat(ser,command)
  command = bytes.fromhex('DD 5A 01 02 00 00 FF FD 77')
  bmscore.getbmsdat(ser,command)
Ejemplo n.º 10
0
def getdat(port='/dev/ttyUSB0'):
  """ Get data from BMS board"""
  ser = bmscore.openbms(port)

  # status
  command = bytes.fromhex('DD A5 03 00 FF FD 77')
  dat = bmscore.getbmsdat(ser,command)
  rawi = int.from_bytes(dat[2:4], byteorder = 'big',signed=True)
  rawv = int.from_bytes(dat[0:2], byteorder = 'big',signed=True)
  balance = bin(int.from_bytes(dat[12:14], byteorder = 'big',signed=True))
  state = int.from_bytes(dat[16:18], byteorder = 'big',signed=True)
  fets = bin(int.from_bytes(dat[20:21], byteorder = 'big',signed=True))
  
  print ("V={} I={} bal={} state={} fets={}".format(rawv,rawi,balance,bin(state),fets))
  errors=['Cell Overvoltage','Cell Undervoltage','Battery Overvoltage', \
          'Battery Undervoltage','Charge Overtemp','Charge Undertemp', \
          'Discharge Overtemp','Discharge Undertemp','Charge Overcurrent' \
          'Discharge Overcurrent','Short Circuit','IC Fault','Software MOS lock']
  for i in range(state.bit_length()):
    if 2**i & state:
      print (errors[i])

  # voltages
  command = bytes.fromhex('DD A5 04 00 FF FC 77')
  voltages = bmscore.getbmsdat(ser,command)
  ser.close
  #print (binascii.hexlify(voltages))
  rawv = [ 0 for i in range(19)]
  rawb = [ 0 for i in range(15)]
  rawt = {}

  total = 0
  mini=0
  maxi=0
  
  for i in range(15):
    rawv[i] = int.from_bytes(voltages[i*2:i*2+2], byteorder = 'big')#/1000.00
    #Suma
    total += rawv[i]
    
    #Serie minima
    if (mini==0):
        mini=rawv[i]
    else:
        if (rawv[i] < mini): 
            mini=rawv[i]
            
    #Serie maxima
    if (rawv[i] > maxi):
        maxi=rawv[i]
    
    if (i == 0):
        rawb[i] = int.from_bytes(dat[12:14], byteorder = 'big',signed=True) & 1
    else:
        rawb[i] = int.from_bytes(dat[12:14], byteorder = 'big',signed=True) >> i & 1

  # temperaturas

  for i in range(int.from_bytes(dat[22:23],'big')): # read temperatures
    rawt['T{0:0=1}'.format(i+1)]=(int.from_bytes(dat[23+i*2:i*2+25],'big')-2731)/10
        
  rawv[15] = total;
  rawv[16] = mini;
  rawv[17] = maxi;
  rawv[18] = maxi - mini;
  
  print (rawv)
  print (rawb)
  print (rawt)
  
  emoncms(rawv,rawb,rawt)