Monday 6 March 2017

Python code to get ini data with Example

"""
Write the required data to be passed in a notepad & save that file with "ini" extention.
Place the below code in the python file.
"""
Example:



Code:
def getIniData(section):
    config = configparser.ConfigParser()
    config.optionxform = str
    config.read("testconfig.ini")
    section_dict = {}
    for option in config.options(section):
        section_dict[option] = config.get(section, option)
    return section_dict



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