'WSL and serial port

I am using Win10 and Linux Ubuntu on WSL 2.0.

For testing purposes of some programs, I would like to use the serial port of my PC in "loopback" with Linux running through WSL.

Basically I would like a process on Linux/WSL to be able to send/receive data from a Windows process or vice versa, through serial port, but without any hardware hack.

Of course I have already tried to set the Windows process serial as "COM1" (as indicated by the Windows resource manager) and the Linux port on WSL as "/dev/ttyS1", but apparently it doesn't work.

Is there any way to do this?



Solution 1:[1]

Following these steps, I was able to get access to the COM ports from WSL 2 on Windows 11. I plugged in 2 USB-Serial cables as shown, and I was able to use one COM port from Linux (with the code I was writing) and the other from Windows (with Termite).

2 USB-Serial adapters in 1 PC

Solution 2:[2]

Status update

According to other answers, it seems that the addition of product functions has made it possible to use USB devices with WSL2.
Especially in v5.10.93.2, it seems that drivers for two types of USB serial interface chips are built in.

linux-msft-wsl-5.10.93.2

  • Enable CH341 and CP210X USB Serial drivers

linux-msft-wsl-5.10.60.1

  • Enable USB over IP support
  • Enable USB kernel configuration options for interacting with an Arduino over USB

The following is outdated information.

WSL 2.0 does not support serial ports.

Exceptions for using WSL 1 rather than WSL 2


The following options are possible.


Also, if you want to communicate between serial ports even if WSL2 cannot recognize USB serial, this method is also available.
Connecting to serial port (com port) over network

And as you can see from the above explanation, if you want to communicate between the processes of each OS, you can simply use a TCP/IP socket instead of the above mechanism.

Solution 3:[3]

Run this in PowerShell or CMD to see if you use Version 1:

C:\>wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         1

If yes, here's some tested working code to read COM14 from linux command prompt:

#!perl -w

use strict;
$|=1; # autoflush

use Device::SerialPort;

my $port = Device::SerialPort->new("/dev/ttyS14");
$port->baudrate(115200); # Configure below to match your device
$port->databits(8);
$port->parity("none");
$port->stopbits(0);
$port->debug(1);

$port->read_char_time(0);       # don't wait for each character
$port->read_const_time(1);      # 0.001 second per unfulfilled "read" call

while (1) {
  my ($count_in, $str_in) = $port->read(255); # Supposedly must be 255 always
  if($count_in) {
    print $str_in;
  }
}

If you are running WSL2, you can backup, convert or copy your distribution to WSL1 via wsl --export, wsl --import, and wsl --set-version. See this question among others for details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nullromo
Solution 2
Solution 3 NotTheDr01ds