Bms giving response but not to our expectation it giving ' 'b like this.
b'\xa5\x80\x90\x08\x00\x00\x00\x00\x00\x00\x00\x00\xbd\n'
. b'\xa5\x80\x90\x08\x00\x00\x00\x00\x00\x00\x00\x00\xbd
. b'\xa5\x01\x90\x08\x02\x10\x00\x00uo\x03\xbc\xf3'
. b'\xa5\x80\x98\x08\x00\x00\x00\x00\x00\x00\x00\x00\xc5\n
. '/0x90'
. b'/0x90'
. DD A5 04 00 FF FC 77.
. uint8_t message[] = {0xDD, 0xA5, 0x05, 0x00, 0xFF, 0xFB, 0x77 };
I tried every format which is shown above but I think my UART communication format is wrong. please help me what is wrong here, please.
Also, I tried to send the request using python and Arduino code But the response is the same.
############################# code1 ##########################
import serial
s = serial.Serial("COM6", 9600)
command = b'\xa5\x80\x90\x08\x00\x00\x00\x00\x00\x00\x00\x00\xbd\n'
s.write(command)
ser = s.read()
print(ser)
for i in ser:
print(i)
# Not get any response here
######################### Code 2 ##############################
import serial
import time
arduino = serial.Serial(port='COM6', baudrate=9600, timeout=.1)
def write_read(x):
arduino.write(bytes(x, 'utf-8'))
time.sleep(0.05)
data = arduino.readline()
return data
while True:
num = '/0x90' # Taking input from user
# num = b'/0x90' # tried this also
value = write_read(num)
print(value) # printing the value
Output is :
PS C:\Users\Pradip\Desktop\python_code> python .\bmsserial.py
b''
b''
b''
b''
############################ code 3 ############################
import serial
import time
## Vars ################
port = "COM6"
baud = 9600
########################
## Command components ???
startFlag = bytes.fromhex("A5")
moduleAddress = bytes.fromhex("80")
commandID = bytes.fromhex("90")
dataLength = bytes.fromhex("08")
data = bytes.fromhex("00" * 8)
checksum = 0
_cmd = startFlag + moduleAddress + commandID + dataLength + data
# calc checksum - guess 1
for b in _cmd:
checksum += b
# get low bytes only
checksum = checksum & 0xff
# convert to hex
checksum = f"{checksum:02X}"
checksum = bytes.fromhex(checksum)
print(f"checksum: {checksum}")
cmd = _cmd + checksum + b"\n"
print (f"sending command {cmd}")
with serial.serial_for_url(port, baud) as s:
s.timeout = 1
s.write_timeout = 1
s.flushInput()
s.flushOutput()
bytes_written = s.write(cmd)
print (f"wrote {bytes_written} bytes")
for _ in range(10):
response_line = s.readline()
time.sleep(10)
print (f"Got response: {response_line}")
Output is: Again output is the same as what I do
PS C:\Users\Pradip\Desktop\python_code> python cmd.py
checksum: b'\xbd'
sending command b'\xa5\x80\x90\x08\x00\x00\x00\x00\x00\x00\x00\x00\xbd\n'
wrote 14 bytes
Got response: b''
Got response: b''
Im not really familiar with windows - essentially above you are not getting a response (it is timing out) Windows has different formats - maybe try "\r\n" instead of "\n" in the send command Maybe the port designation is incorrect? try the below to confirm the port is as you expect (also some people have add problems with the cable, so try another cable)
import serial.tools.list_ports as port_list
ports = list(port_list.comports())
for p in ports:
print (p)