python-3.xserial-portwindows-subsystem-for-linuxpyserial

Connecting and receive data from a serial USB port to WSL 2


I have a problem encountered while connecting a USB serial port to windows and trying to get data from that port into WSL2.

The problem is that I can list usb devices in windows powershell PS C:\> usbipd list results in:

BUSID VID:PID DEVICE STATE

6-4 0403:6001 USB Serial Converter Attached

I can attach the USB to WSL by using

PS C:\> usbipd attach --wsl --busid 6-4

It is attached to WSL.


From WSL side, I can show usb connections in WSL by lsusb, giving:

lsusb

To check the device name:

sudo dmesg | grep tty

gives this: device name


From the Python side, I want to communicate with the serial port to get data etc. The code is:

import serial 
import time 
import time 
import os

ser = serial.Serial('/dev/ttyUSB0')   # device name on your computer 

print(ser.name) # check which port was really used
ser.write(b'serail port on wsl \n') # write a string
s = ser.readline()
print(s)
ser.close()

This code cannot write nor readline() and does not give any output, so basically it is not reading any data from the serial port.


I have checked some issues but those did not solve my problems, here-1, here-2, here-3, here-4.

Is there anything I can further investigate and solve this issue?


Solution

  • The problem is simply solved by defining baudrate as 460800 in serial.Serial("/dev/ttyUSB0", baudrate).