7

I have attempted many ways of trying to connect the serial ports, but I'm not sure how as they all show input output error. The system does recognize something is connected as shown on the screenshots.

enter image description here

enter image description here

enter image description here

I have tried edit the coding to /dev/ttyS39 and 40 also /dev/ttyS29 and 30. I'm not sure how to get the ports to connect.

karel
  • 122,292
  • 133
  • 301
  • 332
tariq said
  • 91
  • 1
  • 1
  • 2

5 Answers5

12

Spent around 2 hours finding how to do this. This is how I did it. In short, follow this: https://learn.microsoft.com/en-us/windows/wsl/connect-usb

My config: Windows 10/ WSL2 Steps:

  1. Open Powershell as admin
  2. Install usbip (follow link instructions)
  3. usbipd list
  4. Bind the usb to share usbipd bind --busid 2-7 (for me it was 2-7)
  5. Verify using usbip list that its shared
  6. Attach to wsl usbipd attach --wsl --busid 2-7

Screenshot to commands executed and output

5

Serial ports on WSL get mapped from the Windows side (COM1 through COM4) to the Linux side as /dev/ttyS1 through /dev/ttyS4. It looks like you're using the COM4 name in your python. Use the Linux device name, which matches the Windows port number.

`COM1:` - `/dev/ttyS1`
`COM2:` - `/dev/ttyS2`
`COM3:` - `/dev/ttyS3`

etc.

popey
  • 24,549
1

I was struggling with this myself using WSL2 and Ubuntu, but I beleive I found a work around.

The way I code and use VSC is to open WSL, CD to the folder I want and type 'code .' in the console. Windows opens up VSC and connect it to WSL and from there I use the built in bash terminal for NPM etc. Its much easier than figiting with Windows to get common development tools installed.

For the project I'm working on I need access to the Serial data as I'm working with UART and the Serial Monitor provided /dev/ttyS3 which in theory should have worked but as its been pointed out WSL2 doesn't have direct access to hardward. But it didn't.

What did work? I revealed the project path (shift+alt+r) which opens an Explorter window. My path is:

\\wsl.localhost\Ubuntu\root\dev\uart

I opened a new VSC window, then opened the path manually. Usually when I do this I get stuck with PowerShell for a terminal, but I was suprised to see that it has connected to WSL and I had bash. However, the Serial Monitor now showed COM ports and when I used ESP-Link, I was able to read the data.

Success!

1

This works, but unplugging the USB will unattach it from wsl and will rquire to run attaching command agian. The following command will create a cmd window which runs a loop to auto attach.

usbipd attach --wsl --auto-attach --hardware-id <xxx> (or --busid <x-x>)
0

Accessing physical hardware under Windows Subsystem for Linux is tricky. You don't mention whether you are running WSL1 or WSL2 (you actually didn't mention WSL at all, but it's apparent from the screenshot, thanks to @popey for catching that and editing it into the question tags).

Under WSL2, you definitely don't have direct access to the serial ports inside Ubuntu. Keep in mind that Ubuntu is running inside a namespace container (similar to Docker and other container technologies) inside a managed virtual machine. "Managed" means that WSL itself handles the configuration of the virtual machine, so there's not much user control over it.

For USB, you do have a workaround in the form of USB/IP, but AFAIK this won't work for non-USB serial ports unless you attached a USB<->RS232 serial adapter. Even then, you'd possibly need to build support for it into the kernel.

Under WSL1, the situation may be better, but it will depend on the capabilities that you need. WSL1 works directly on Windows through a "syscall<->WinAPI translation layer", which means that it does have direct access to some hardware. That access, though, is limited to the syscalls that the Windows team has implemented. In many cases, this won't include calls to configure the interface.

If you are using WSL2 (the default), then I would recommend:

  • First see this thread for a mention of using CommTunnel (in Windows) with socat (in Linux) to tunnel the serial port traffic. That's not going to be easy, and it's not something I can help with - Perhaps a dedicated RoS forum might be the place to ask if you need assistance with that.

  • Or you might try to convert your Ubuntu distribution to WSL1. You can do this from PowerShell with:

wsl -l -v
# Confirm the <distro_name>
wsl --set-version <distro-name> 1

If that doesn't work, you'll probably want to use a virtual machine instead of WSL for this.

NotTheDr01ds
  • 22,082