Using two instances of PlatformIO and two STLinks causes problems

Here is my scenario. I have two target boards connected via two STLink interfaces. I finally got them to coexist (Thanks to manuelbl’s suggestion for serial number identification!). However, now there is a new problem when I try to get both targets running for debugging.
It appears that when one STLink is active in a debug session, then the second STLink will fail when trying to debug or upload to it. From what I can tell from the error messages it looks like GDB cannot manage more than one connection. The first STLink is using Localhost:3333 as port, and I guess the second STLink tries to do the same, so there is a collision resulting in this error message:

Info : rejected ‘gdb’ connection, no more connections allowed

Is there any way to get around this problem and have two debug sessions in two seperate instances of PlatformIO?

Ah, this is tricky. In that case you must use a combination of debug_server and debug_port commands, where you

  1. Tell OpenOCD to use a certain port numbers for opening the GDB server (all others can be random or unused). See docs. I think something like -c "gdb_port 3334" -c "tcl_port disabled" -c "telnet_port disabled" should work? Maybe with a set at the start too…
  2. Tell PlatformIO to use these new port numbers in the debug_port, e.g. localhost:3334

Thanks Max, that is exactly what I tried this morning and it works well.
I added

  debug_tool = custom         
  debug_port = localhost:3334      
  debug_server = $PLATFORMIO_CORE_DIR/packages/tool-openocd/bin/openocd
      gdb_port 3334


 debug_tool = custom         
 debug_port = localhost:3335      
 debug_server = $PLATFORMIO_CORE_DIR/packages/tool-openocd/bin/openocd
     gdb_port 3335

one to each project’s platformio.ini and it runs perfectly, I can simultaneously debug both projects each on it’s own STLink device. Still using the serial number method to keep them identified for now.

This is will come quite handy. Thanks.