RFExplorer / RFExplorer-for-Python

RF Explorer libraries and examples for Python 3
GNU Lesser General Public License v3.0
51 stars 25 forks source link

serial connection autodetect fails for multiple detected serial devices #22

Closed rbw42 closed 2 years ago

rbw42 commented 2 years ago

Hi, Using RFE_Example_USB_1.py as an example on my mac (10.13), the code finds multiple valid serial devices, but subsequent connection with autodetect fails. E.g.

Python 3.8.12 (default, Sep 1 2021, 00:14:23) [Clang 9.1.0 (clang-902.0.39.2)] on darwin Type "help", "copyright", "credits" or "license" for more information.

import RFExplorer
objRFE = RFExplorer.RFECommunicator()
objRFE.GetConnectedPorts()

Detected COM ports:

Detected OS: Darwin /dev/cu.SLAB_USBtoUART is a valid available port. Error in RFCommunicator - IsConnectedPort()[Errno 16] could not open port /dev/cu.Makeblock-ELETSPP: [Errno 16] Resource busy: '/dev/cu.Makeblock-ELETSPP' Error in RFCommunicator - IsConnectedPort()[Errno 16] could not open port /dev/cu.australis-Bluetooth-PDA: [Errno 16] Resource busy: '/dev/cu.australis-Bluetooth-PDA' Error in RFCommunicator - IsConnectedPort()[Errno 16] could not open port /dev/cu.BluetoothMace4-ce-8f-46: [Errno 16] Resource busy: '/dev/cu.BluetoothMace4-ce-8f-46' /dev/cu.SLAB_USBtoUART9 is a valid available port. RF Explorer Valid Ports found: 2 - /dev/cu.SLAB_USBtoUART /dev/cu.SLAB_USBtoUART9 True objRFE.ConnectPort(None, 500000) User COM port: void Error: select a different COM port False

However, if I manually specify the serial device to be /dev/cu.SLAB_USBtoUART9, the connection works.

jcalderon09 commented 2 years ago

Hi rbw42,

​​This is an expected behavior due to a software limitation. It is not possible to know which RF Explorer serial port is.

By default, the variable SERIALPORT is set to None. In this case, if more than one COM port are available, it will fail to connect to generator.

​​As you know, you should specify serial port where device is connected in SERIALPORT = COMx variable in the python example USB you want to run.

rbw42 commented 2 years ago

Hi jcalderon09,

Thanks for your reply. If this is expected behaviour, then can you please consider changing GetConnectedPorts() to return a list of valid ports instead of just printing them to screen and returning a boolean. It can return a None to indicate failure.

As it stands, the code cannot be used in a script, because ConnectPort() cannot be run until GetConnectedPorts() is run, and the latter only prints to screen. I can apply my own heuristics to choose which serial port to use if there is more than one, but this is not possible as things are.

Thanks, Randall.

philipclaesson commented 2 years ago

​​This is an expected behavior due to a software limitation. It is not possible to know which RF Explorer serial port is.

It should be possible to know which serial port belongs to an RF Explorer, at least on Linux and macOS.

for example, this works fine:

for port in serial.tools.list_ports.comports():
    if "CP2102" in port.description:
        return port
arocholl commented 2 years ago

Unfortunately that points to any CP2102 device, not only RF Explorer.