Jan-07-2019, 11:53 AM
With the help of some earlier coding suggestion, I was trying to select the serial port with which I have connected the wireless modem.
I am using serial.tools.list_ports to list down all the com ports available in the laptop but after selecting the com port I am not able to open that
I am using mentioned below python code
I am using serial.tools.list_ports to list down all the com ports available in the laptop but after selecting the com port I am not able to open that
I am using mentioned below python code
import serial import json import tkinter from tkinter import messagebox from tkinter import * import tkinter as ttk import serial.tools.list_ports ard = serial.Serial(); root = ttk.Tk() root.title("Read Sensor") B = None C = None # Add a grid mainframe = Frame(root) mainframe.grid(column=0,row=0, sticky=(N,W,E,S) ) mainframe.columnconfigure(0, weight = 1) mainframe.rowconfigure(0, weight = 1) mainframe.pack(pady = 100, padx = 100) # Create a Tkinter variable tkvar1 = StringVar(root) tkvar = StringVar(root) #Serial Port a=serial.tools.list_ports.comports() for w in a: print(w.device) k = [w.device] print(k) ser_dict = { i for i in k } print (ser_dict) val1 = tkvar1.get() if val1 in ser_dict: ard.port = str (ser_dict[val1]) ard.open() if C: C.destroy() C = ttk.Button(root, text =val1, command = dropCall) C.pack() # Dictionary with options baud = { '9600','119200','34800'} #Pop Up desciption popupMenu1 = OptionMenu(mainframe, tkvar1, *ser_dict) Label(mainframe, text="Serial Port").grid(row = 1, column = 1) popupMenu1.grid(row = 2, column =1) popupMenu2 = OptionMenu(mainframe, tkvar, *baud) Label(mainframe, text="Baudrate").grid(row = 3, column = 1) popupMenu2.grid(row = 4, column =1) #Serial Callback Functions def helloCallBack(): k = ard.readline().decode('ascii'); if(len(k)>0): print (k); size = len(k); #print (k[0:size-2]); print (size); messagebox.showinfo('Message From Arduino',k[0:size-2]+'\n'+str(size)) #baudrate functions def dropCall(*args): global B value = tkvar.get() ## using a dictionary instead of if statements ## to show how dictionaries are used baud_dict={'9600':value, '34800':value, '119200':value} if value in baud_dict: ard.baudrate = int(baud_dict[value]) if B: B.destroy() B = ttk.Button(root, text =value, command = helloCallBack) B.pack() #Link Function tkvar.trace('w', dropCall)Due to mentioned below error I am not able to access that port
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Misha\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py", line 1702, in __call__ return self.func(*args) File "C:\Users\Misha\Desktop\test\GUItest.py", line 60, in helloCallBack k = ard.readline().decode('ascii'); File "C:\Users\Misha\AppData\Local\Programs\Python\Python36\lib\site-packages\serial\serialwin32.py", line 267, in read raise portNotOpenError serial.serialutil.SerialException: Attempting to use a port that is not openWill looking forward for your suggestions