Python Forum
Get Serial ports via Function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get Serial ports via Function
#1
I have the below code that works fine by itself but not in a function to retrieve the name of the serial ports.

import serial.tools.list_ports
ports = serial.tools.list_ports.comports(include_links=False)
for port in ports :
    print(port.device)
I want to be able to use the code between windows and linux based systems to pull the current serial ports (active) and populate a combobox for them with their names.

When I put the above code in a function I get an error of : TypeError: _getserial_ports() takes 0 positional arguments but 2 were given

when my checkbox (checkboxsb) is changed it will call the function. The function will currently only print it to the console, i'm just trying to get it to work within a function before tackling adding the items to a combobox.

#!/usr/bin/python3
import os
import sys
import serial.tools.list_ports


from PyQt5 import QtCore, QtGui, QtWidgets, uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "ooeygui.ui", self)

        self.ui.checkboxsb.stateChanged.connect(self._pullcheckboxsb)
        self.ui.checkboxsb.stateChanged.connect(self._getserial_ports)

        self.show()

    def _getserial_ports():
        
        ports = serial.tools.list_ports.comports(include_links=False)
        for port in ports :
            print(port.device)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())

i found my issue with my code. i forgot to put in the 'self' within the def_getserial_ports function...
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Reply
#2
Since you already use Qt framework it would require less dependancies to use their serial port interface as well (full example here):

#!/usr/bin/python3
import sys
from PyQt5 import QtCore, QtWidgets, QtSerialPort


def ports():
    availables = []
    for port in QtSerialPort.QSerialPortInfo.availablePorts():
        availables.append(port.systemLocation())
    if availables:
        availables = str(availables)
        return(availables.translate({ord(c): None for c in "[']"}))
    return("none")

if __name__== '__main__':
    app = QtWidgets.QApplication([])
    print(ports())
    sys.exit(app.exec_())
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 3,331 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Uninstall unused COM ports windows 10 adbrooker 1 1,982 Sep-22-2021, 03:16 AM
Last Post: Larz60+
  How to Properly Open a Serial Port in a Function bill_z 2 4,352 Jul-22-2021, 12:54 PM
Last Post: bill_z
  Mido and virtual midi ports dore_m 2 4,525 Dec-27-2020, 06:02 AM
Last Post: dore_m
  Handling Multiple USB ports in Python samalpramod 0 4,365 Aug-01-2020, 07:40 PM
Last Post: samalpramod
  Function to Continuasly monitor serial port and control other functions ricardons 10 8,047 Mar-04-2018, 10:34 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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