80 lines
2.2 KiB
Python
80 lines
2.2 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(7)
|
||
|
|
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 main():
|
||
|
|
# set comport
|
||
|
|
ser = serial.Serial("COM12", 57600, 8)
|
||
|
|
|
||
|
|
# send survive cmd
|
||
|
|
survive_cmd(ser)
|
||
|
|
|
||
|
|
# send scan cmd
|
||
|
|
peer_addr = scan_cmd(ser, False)
|
||
|
|
|
||
|
|
# send connect cmd with peer addr
|
||
|
|
connect(ser, peer_addr)
|
||
|
|
time.sleep(10)
|
||
|
|
|
||
|
|
# send disconnect cmd
|
||
|
|
disconnect(ser)
|
||
|
|
|
||
|
|
ser.close()
|
||
|
|
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
main()
|