Sharing COM port driver between driver instance and Custom Function

For the Labjack driver I can use an instance in the SweepMe Sequencer and also in a Custom Function (in the same sequencer!), using pysweepme.get_driver(…). The port is shared between the two instances and everything works. Nice.

When I try to do the exact same thing with the Ocean Controls KTx-290 driver there is a conflict over the COM port when the custom function tries to make its own instance after the first instance. Here is the debug print:

File “C:\Users\Public\Documents\SweepMe!\DataModules\CustomFunction\Functions\CalAcq4\main.py”, line 92, in connect
self.kta = pysweepme.get_driver(
File “pysweepme\DeviceManager.py”, line 152, in get_driver
File “pysweepme\DeviceManager.py”, line 124, in setup_driver
File “pysweepme\Ports.py”, line 250, in get_port
File “pysweepme\Ports.py”, line 580, in open
File “pysweepme\Ports.py”, line 1051, in open_internal
File “serial\serialwin32.py”, line 64, in open
serial.serialutil.SerialException: could not open port ‘COM5’: PermissionError(13, ‘Access is denied.’, None, 5)

Is this something I can fix on my own?

1 Like

Hi,
thanks a lot for reporting this issue.

SweepMe! uses pysweepme.PortManager, but the get_driver function of pysweepme.DeviceManager is accessing the Port directly without using the PortManager that is actually made to handle open and closed ports.

Felix created a quick workaround (untested) that you could give a try:

from pysweepme.DeviceManager import get_driver_instance
from pysweepme.PortManager import PortManager

port_manager = PortManager()


port_name = "COM5"
driver_name = "Switch-OceanControls_KTx-290"

# use get_driver_instance instead of get_driver so we can provide the port on our own
driver = get_driver_instance("<folder-to-drivers>", driver_name)

# use Port from the PortManager
port = port_manager.get_port(port_name, driver.port_properties)
driver.set_port(port)

# some adjustments so that the driver behaves like loaded from SweepMe! internally
driver.set_parameters({"Port": port_name, "Device": driver_name})

If this works we can implement this as the general solution. However, it could be that other things break. This is why it would be nice to receive your feedback whether it works or follow-up problems are created. We will test as well with some real devices.

Thanks and best,
Axel

1 Like

Gentlemen,
This fix seems to work. I no longer get that error when creating the CF instance. I will do more extensive testing (actually using the device in the CF) and report back within a week.
THANK YOU!

1 Like