adafruit / circuitpython_jupyter_kernel

Jupyter kernel to interact with CircuitPython board over USB
https://circuitpython-kernel.readthedocs.io/en/latest/
Other
37 stars 27 forks source link

Multiple CDC ports causing issues with Jupyter kernel? #28

Open mintakka opened 3 years ago

mintakka commented 3 years ago

Description

I am trying to use a Feather M4 Express with circuitpython (version 6.1.0) and Jupyter. I am intermittently having an issue where the kernel fails to automatically connect to the correct CDC port (circuitpython is presenting on two ports in my OS).

What I Did

Here are my serial ports:

❯ pio device list
/dev/cu.Bluetooth-Incoming-Port
-------------------------------
Hardware ID: n/a
Description: n/a

/dev/cu.usbmodem14303
---------------------
Hardware ID: USB VID:PID=239A:8026 SER=4CBE1F385364933502020233339110FF LOCATION=20-3
Description: Feather M4 Express - CircuitPython CDC2 data

/dev/cu.usbmodem14301
---------------------
Hardware ID: USB VID:PID=239A:8026 SER=4CBE1F385364933502020233339110FF LOCATION=20-3
Description: Feather M4 Express - CircuitPython CDC2 data

Here is an instance where the kernel automatically attempted to connect to the usbmodem140303 port. Notebook commands run in this state hang and never complete. It seems like the kernel always connects to whichever port for the board is listed first. In this error case, that is usbmodem14303. If the other port, usbmodem14301 is listed first, then the jupyter kernel will attempt to connect to that one. In that state things are functional.

circuitpy ❯ jupyter notebook
[I 15:34:47.261 NotebookApp] Serving notebooks from local directory: /Users/MYNAME/Desktop
[I 15:34:47.261 NotebookApp] Jupyter Notebook 6.2.0 is running at:
[I 15:34:47.261 NotebookApp] http://localhost:8888/?    token=28f9c073a686bd4938db0b8574b4158b08d286b24f46e205
[I 15:34:47.261 NotebookApp]  or http://127.0.0.1:8888/?    token=28f9c073a686bd4938db0b8574b4158b08d286b24f46e205
[I 15:34:47.261 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 15:34:47.264 NotebookApp] 

    To access the notebook, open this file in a browser:
        file:///Users/MYNAME/Library/Jupyter/runtime/nbserver-26094-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=28f9c073a686bd4938db0b8574b4158b08d286b24f46e205
     or http://127.0.0.1:8888/?token=28f9c073a686bd4938db0b8574b4158b08d286b24f46e205
[W 15:34:48.781 NotebookApp] 404 GET /apple-touch-icon-precomposed.png (::1) 8.050000ms referer=None
[W 15:34:48.785 NotebookApp] 404 GET /apple-touch-icon.png (::1) 1.250000ms referer=None
[I 15:34:54.107 NotebookApp] Creating new notebook in 
[I 15:34:55.045 NotebookApp] Kernel started: 412927db-5709-4a7c-b62e-cf65d6c6e615, name: circuitpython
DEBUG:asyncio:Using selector: KqueueSelector
DEBUG:asyncio:Using selector: KqueueSelector
DEBUG:circuitpython_kernel.kernel:circuitpython_kernel version 0.3.0.dev
DEBUG:circuitpython_kernel.board:/dev/cu.Bluetooth-Incoming-Port
DEBUG:circuitpython_kernel.board:/dev/cu.usbmodem14303
DEBUG:circuitpython_kernel.board:CircuitPython Board Found at: /dev/cu.usbmodem14303
DEBUG:circuitpython_kernel.board:Connected? False
DEBUG:circuitpython_kernel.board:connect: open /dev/cu.usbmodem14303
DEBUG:circuitpython_kernel.board:serial already open
DEBUG:circuitpython_kernel.board:* enter raw repl ...
[I 15:35:17.427 NotebookApp] Kernel interrupted: 412927db-5709-4a7c-b62e-cf65d6c6e615
DEBUG:circuitpython_kernel.kernel:no connection failed to enter raw repl with /dev/cu.usbmodem14303
DEBUG:circuitpython_kernel.kernel:Error:  'No connection to CiruitPython VM: failed to enter raw repl with     /dev/cu.usbmodem14303'
DEBUG:circuitpython_kernel.board:/dev/cu.Bluetooth-Incoming-Port
DEBUG:circuitpython_kernel.board:/dev/cu.usbmodem14303
DEBUG:circuitpython_kernel.board:CircuitPython Board Found at: /dev/cu.usbmodem14303
DEBUG:circuitpython_kernel.board:Connected? False
DEBUG:circuitpython_kernel.board:connect: open /dev/cu.usbmodem14303
DEBUG:circuitpython_kernel.board:serial already open
DEBUG:circuitpython_kernel.board:* enter raw repl ...
dhalbert commented 3 years ago

Discussed on discord (thanks @mintakka). It looks like if we just sort the ports it should work. My experience has been that MacOS always assigns a higher-numbered port to the CDC2 device.

dhalbert commented 3 years ago

Alternatively, we can use the lookup code in https://github.com/pyserial/pyserial/pull/566/files.

dhalbert commented 3 years ago

This problem will occur on other OS's as well. My experience has been that they also assign a lower port or device number to the REPL channel, so sorting should work in general, though if we can look up the interface name, that would be best.

psychemedia commented 3 years ago

On a Mac, if the Mu editor is open and connected, then the notebook kernel doesn't seem to want to play.

I'm not sure what happens if you have more then one notebook open at any one time?

TomFahey commented 2 years ago

This is also a problem when using the USB_cdc circuitpython library.

The kernel tries to select /dev/ttyACM1, which I'm using as a serial data transport, instead of the correct /dev/ttyACM0 that the REPL is available on.