'SerialPort Thread on Windows

I need the give SerialConnection object to ThreadClass how can I do that?

I faced an error on Windows OS. But same code have not any error in Linux (Ubuntu).

I create the SerialConnection() object in MainWindow also self.angleThread created same init function.

class MainWindow(QMainWindow):
    def __init__(self,parent = None):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # self = QMainWindow class
        # self.ui = Ui_MainWindow / user interface class
        loadJsonStyle(self, self.ui)
        QSizeGrip(self.ui.size_grip)
        self.connection = SerialConnection()
        self.Rs232 = RS232_Data()
        self.angleThread = ThreadClass("filename", index)

I need the serial connection in ThreadClass so both function calling in ThreadClass init.

   def __init__(self,  filename = "", index = 0):
        super(ThreadClass, self).__init__()
        self.filename = filename
        self.is_running = True
        self.index = index
        # self.connection.close
        self.Rs232 = RS232_Data()
        self.connection = SerialConnection()
        ...

SerialConnection class:

class SerialConnection():
    def __init__(self):
        portList = serial.tools.list_ports.comports()
        self.serialPort = 0
        for p in portList:
            # print(p.pid)
            if p.pid == 24577:
                print("Confirmed")
                self.getPortName = p.device
                self.serialPort = serial.Serial(port=self.getPortName, baudrate=230400, bytesize=8, timeout=10, stopbits=serial.STOPBITS_ONE)

                break
            else:
                ## Show dialog
                print("There is no device on the line")
    def connection_start(self):
        try:
            # self.serialPort.open()
            1 == 1
        except Exception:
            print("Port is already open")
    def connection_close(self):
        if self.serialPort.is_open:
            self.serialPort.close()
        else:
            print("Port didnt open")
    def serial_write(self, data):
        self.data = data
        self.serialPort.write(self.data)
    def serial_read_string(self):

        readData = self.serialPort.readline()
        return readData
    def serial_read_byte(self,size):
        readData = self.serialPort.read(size)
        # print(len(readData))
        if len(readData) == 0:
            print("unconnceted")
        return readData

But In the windows I get below error:

    raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM12': PermissionError(13, 'Erişim engellendi.', None, 5)

How can I solve this problem or how can I move the alive connection to ThreadClass ?



Solution 1:[1]

Aren't you opening the same port twice? Once in MainWindow constructor and later second time in ThreadClass constructor. As far as I know this is forbidden in Windows.

You can just pass SerialConnection instance as a ThreadClass constructor parameter instead of creating it there.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zuljin