Python Forum
How to detect wireless modem connected serially to my laptop in python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to detect wireless modem connected serially to my laptop in python
#1
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

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 open
Will looking forward for your suggestions
Reply
#2
Read the error traceback, it's quite clear, on line 60:
Quote:serial.serialutil.SerialException: Attempting to use a port that is not open
Reply
#3
(Jan-07-2019, 03:26 PM)Larz60+ Wrote: ard.open()

I already mentioned for opening the port as well as code is reading the port but still facing this issue can you suggest what will be useful here?
Reply
#4
Perhaps if val1 in ser_dict is never true and ard.open() is never reached.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to check if Skype call is connected or disconnected in Python? boral 1 358 Dec-28-2023, 08:31 AM
Last Post: buran
  When does Python detect Errors? stamp1t 1 401 Oct-21-2023, 05:53 PM
Last Post: deanhystad
  How to summarize an article that is stored in a word document on your laptop? Mikedicenso87 2 614 Oct-06-2023, 12:07 PM
Last Post: Mikedicenso87
  How to detect abnormal data in big database python vanphuht91 5 1,064 Jun-27-2023, 11:22 PM
Last Post: Skaperen
  When is stdin not connected to a tty, but can still be used interactively? stevendaprano 1 970 Sep-24-2022, 05:06 PM
Last Post: Gribouillis
Question SMS sending using USRobotics modem Jacobus 2 2,423 Mar-11-2021, 09:59 PM
Last Post: nilamo
  Python chess game to detect winner ddddd 1 1,970 Dec-13-2020, 10:24 PM
Last Post: michael1789
  Printing to a printer connected to pi 4 alan 2 2,382 Oct-04-2020, 10:08 PM
Last Post: alan
  wireless access point on raspberry PI zazas321 0 1,902 Oct-02-2020, 05:33 PM
Last Post: zazas321
  I can`t find an IDE functioning in my laptop All_ex_Under 5 2,922 Aug-17-2020, 05:44 AM
Last Post: All_ex_Under

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020