import serial
import time

s = serial.Serial()
s.baudrate = 115200
s.port = "com4"
s.parity = "N"
s.stopbits = 1
s.xonxoff = 0
s.timeout = 1
s.open()

def send_at_command(command):
    s.write(bytes(command+"\r", encoding='ascii'))
    print(s.read(300).decode('ascii').strip())
    time.sleep(0.1)

print('\n------BQ27426 Fuel Gauge------')
print('---Wake up---')		
send_at_command("[0x40 0x03 0x10]")
send_at_command("[0x40 0x01 0x0f]")
send_at_command("[0x40 0x01 0x2f]")
time.sleep(0.1)
send_at_command("[0x40 0x01 0x0f]")
send_at_command("[0x40 0x03 0x30]")
print('---Data Memory Parameter Update Example---')
print('---Unseal')
send_at_command("[0xaa 0x00 0x00]")
send_at_command("[0xaa 0x01 0x80]")
send_at_command("[0xaa 0x00 0x00]")
send_at_command("[0xaa 0x01 0x80]")
print('---Send SET_CFGUPDATE subcommand')
send_at_command("[0xaa 0x00 0x13 0x00]")
print('---Confirm CFGUPDATE mode')
send_at_command("[0xaa 0x00 0x06 0x00]")
send_at_command("[0xaa 0x06 [0xab rr]")
time.sleep(2)
print('---Enable block data memory control')
send_at_command("[0xaa 0x61 0x00]")
print('---Access the State subclass')
send_at_command("[0xaa 0x3e 0x52]")							
print('---Write the block offset location')
send_at_command("[0xaa 0x3f 0x00]")							
print('---Read the 1-byte checksum')
send_at_command("[0xaa 0x60 [0xab r]")							
print('---Read Design Capacity bytes (3200mAh)')
send_at_command("[0xaa 0x4A [0xab r]")
send_at_command("[0xaa 0x4B [0xab r]")
print('---Write Design Capacity bytes (3200mAh)')
send_at_command("[0xaa 0x4A 0x0C]")
send_at_command("[0xaa 0x4B 0x80]")					
print('---Write check sum')
send_at_command("[0xaa 0x60 0x53]")							
print('---Exit CFGUPDATE mode')
send_at_command("[0xaa 0x00 0x42 0x00]")							
print('---Confirm CFGUPDATE has been exited')
send_at_command("[0xaa 0x00 0x06 0x00]")
send_at_command("[0xaa 0x06 [0xab rr]")				
time.sleep(2)
print('---Return to SEALED mode')
send_at_command("[0xaa 0x00 0x20 0x00]")
print('---Chemistry Profile Change Example---')
print('---Unseal')
send_at_command("[0xaa 0x00 0x00]")
send_at_command("[0xaa 0x01 0x80]")
send_at_command("[0xaa 0x00 0x00]")
send_at_command("[0xaa 0x01 0x80]")	
print('---Read CHEMICAL ID')
send_at_command("[0xaa 0x00 0x08 0x00]")
send_at_command("[0xaa 0x00 [0xab rr]")				
print('---Send SET_CFGUPDATE subcommand')
send_at_command("[0xaa 0x00 0x13 0x00]")							
print('---Confirm CFGUPDATE mode') 
send_at_command("[0xaa 0x00 0x06 0x00]")
send_at_command("[0xaa 0x06 [0xab rr]")
time.sleep(2)
print('---Change Chemistry to CHEM_C (chem ID = 1202, 4.2 V).')	
send_at_command("[0xaa 0x00 0x31 0x00]")															
print('---Exit CFGUPDATE mode')
send_at_command("[0xaa 0x00 0x42 0x00]")							
print('---Confirm CFGUPDATE has been exited')	
send_at_command("[0xaa 0x00 0x06 0x00]")
send_at_command("[0xaa 0x06 [0xab rr]")
time.sleep(2)
print('---Read CHEMICAL ID')
send_at_command("[0xaa 0x00 0x08 0x00]")
send_at_command("[0xaa 0x00 [0xab rr]")				
print('---Return to SEALED mode')
send_at_command("[0xaa 0x00 0x20 0x00]")

s.close()
