119 lines
3.3 KiB
Python
119 lines
3.3 KiB
Python
import serial
|
|
import os
|
|
import time
|
|
|
|
|
|
def survive_cmd(ser):
|
|
send = [0x0A, 0x01, 0xF1]
|
|
ser.write(send)
|
|
read = ser.read(4)
|
|
print("recv: " + " ".join("%02X" % b for b in read))
|
|
|
|
|
|
def scan_cmd(ser, is_raw_data=True):
|
|
send = [0x03, 0x01, 0xF1]
|
|
ser.write(send)
|
|
peer_addr = []
|
|
for i in range(10):
|
|
hci_packet_event = ser.read(2)
|
|
if int.from_bytes(hci_packet_event, "little") != 0x0004:
|
|
continue
|
|
len = ser.read(1)
|
|
payload = ser.read(int.from_bytes(len, "little"))
|
|
peer_addr = payload[0:6]
|
|
if is_raw_data:
|
|
print(
|
|
"recv: "
|
|
+ " ".join("0x%02X" % b for b in hci_packet_event)
|
|
+ " "
|
|
+ " ".join("0x%02X" % b for b in len)
|
|
+ " "
|
|
+ " ".join("0x%02X" % b for b in payload)
|
|
)
|
|
else:
|
|
print("-" * 100)
|
|
print(" peer address: " + ":".join("%02X" % b for b in payload[0:6]))
|
|
print("product module: " + str(payload[6:10], "utf-8"))
|
|
print(" hw version: " + " ".join("%02X" % b for b in payload[10:14]))
|
|
print(" build time: " + " ".join("%02X" % b for b in payload[14:16]))
|
|
print(" Parameter1: " + str(payload[16:19], "utf-8"))
|
|
print(
|
|
" battery volt: "
|
|
+ str(int.from_bytes(payload[19:21], "big") / 1000.0)
|
|
+ "V"
|
|
)
|
|
print(" device name: " + str(payload[21:], "utf-8"))
|
|
return peer_addr
|
|
|
|
|
|
def connect(ser, peer_addr=[]):
|
|
send = [0x05, 0x08, 0x00]
|
|
send += peer_addr
|
|
send += [0xF1]
|
|
ser.write(send)
|
|
read = ser.read(4)
|
|
print("recv: " + " ".join("%02X" % b for b in read))
|
|
|
|
|
|
def disconnect(ser):
|
|
send = [0x08, 0x01, 0xF1]
|
|
ser.write(send)
|
|
read = ser.read(4)
|
|
print("recv: " + " ".join("%02X" % b for b in read))
|
|
|
|
|
|
def write_char(ser: serial.Serial, handle: int, write_data: tuple):
|
|
send = [0x06, len(write_data)+2, handle]
|
|
send += write_data
|
|
send += [0xF1]
|
|
ser.write(send)
|
|
|
|
def read_char(ser: serial.Serial, handle: int):
|
|
send = [0x07, 0x02, handle, 0xF1]
|
|
ser.write(send)
|
|
read = ser.read(0xFFFF)
|
|
print("recv: " + " ".join("%02X" % b for b in read))
|
|
|
|
|
|
def main():
|
|
# set comport
|
|
ser = serial.Serial("COM15", 57600, 8, inter_byte_timeout=0.01)
|
|
|
|
# send survive cmd
|
|
survive_cmd(ser)
|
|
|
|
# send scan cmd
|
|
peer_addr = scan_cmd(ser, False)
|
|
time.sleep(10)
|
|
return
|
|
# send connect cmd with peer addr
|
|
connect(ser, peer_addr)
|
|
time.sleep(3)
|
|
|
|
# send write char to enable notify
|
|
write_char(ser, 0x0024, [0xc4,0xf0,0xf1,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00])
|
|
time.sleep(0.1)
|
|
write_char(ser, 0x0024, [0x74,0x10,0xf1,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00])
|
|
time.sleep(0.1)
|
|
read_char(ser, 0x0021)
|
|
# 130500040000000000000000000000000000001c
|
|
time.sleep(0.2)
|
|
# send write char to enable notify
|
|
#write_char(ser, 0x0028, 1)
|
|
#time.sleep(5)
|
|
|
|
# send write char to disable notify
|
|
#write_char(ser, 0x0028, 0)
|
|
|
|
# send read char to disable notify
|
|
#read_char(ser, 0x0027)
|
|
|
|
# send disconnect cmd
|
|
disconnect(ser)
|
|
|
|
ser.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|