Monday, March 6, 2017

Python - Active COMPORT Detection & Verifying BLE device


"""
==> Connect the BLE devices and normal USB devices to the PC, then run this Python script, which will give a list of BLE devices.
"""

import serial
import serial.tools.list_ports
def dev_identify():
    ports = list(serial.tools.list_ports.comports()) # will give list of Active COM Ports
    for p in ports:                                                  
        #print(str(p))
        port = str(p)[:4]    #slicing for exact COMport number
        baud = 9600
        #global ser
        ser = serial.Serial(port, baud, timeout=1)
       # print(ser)
       # open the serial port
        if ser.isOpen():
            print(ser.name + ' is open')
            cmd="ATI\r"
            ser.write(cmd.encode())
            msg=ser.read(64)
            print(msg)  #print(type(msg))==> str
            #print(len(msg))
            print(msg[35:52])
            s=msg[35:52]
            if s == "":
                print("This is Not a BLE device")
    return

dev_identify()

                                Request: Please provide your valuable comments

No comments:

Post a Comment