import serial.tools.list_ports import time ns_val = time.time_ns() serPort = "" int1 = 0 str1 = "" str2 = "" ports = list(serial.tools.list_ports.comports()) for p in ports: print(p) # This causes each port's information to be printed out. # To search this p data, use p[1]. while int1 < 20: # Loop checks "COM0" to "COM8" for Msp Port Info. if "UART1" in p[1]: # Looks for "UART1" in P[1]. str2 = str(int1) # Converts an Integer to a String, allowing: str1 = "COM" + str2 # add the strings together. if "UART1" in p[1] and str1 in p[1]: # Looks for "Msp" and "COM#" print("Found MSP430 on %s" % (str1)) int1 = 20 # Causes loop to end. if int1 == 19: print("MSP not found!") int1 = int1 + 1 int1 = 0 # Set Port ser = serial.Serial(str1, 9600, bytesize=serial.EIGHTBITS) # Put in your speed and timeout value. ser.close() ns_ports = time.time_ns() # get UID from UART data1 = "" ser.close() ser.open() ser.write('0'.encode()) buffer = ser.readline() UID = ser.readline().decode('UTF-8') UID = UID.replace("\n", "") # replace \n so we don't have a paragraph print("NFC UID: ",UID) ser.close() ns_uid = time.time_ns() time_needed_for_port_init = (((ns_ports - ns_val)/1000000)/1000) time_needed_for_read_uid = (((ns_uid - ns_ports)/1000000)/1000) print("Time port init: ",time_needed_for_port_init) print("Time read uid: ",time_needed_for_read_uid)