Reputation: 5628
How can i find which serial-port is the active one in Windows Subsystem for Linux? I know about the added support in WSL for using the /dev/ttyS
, but which of these ports are active?
The issue I'm trying to solve is I have a device which keep switching comport, because of the internal chip that reconnects it self on a new port. I want to create a bash script that finds the active serial-ports.
Regular linux commands like: dmesg | grep tty
does not work.
Upvotes: 3
Views: 7051
Reputation: 1549
On the latest WSL 2 dmesg | grep tty
will work just fine.
Getting the devices to work is a pain though.
ADMIN PS> wsl --update
PS> winget install --interactive --exact dorssel.usbipd-win
WSL> sudo apt install linux-tools-5.4.0-77-generic hwdata
WSL> sudo update-alternatives --install /usr/local/bin/usbip usbip /usr/lib/linux-tools/5.4.0-77-generic/usbip 20
# Open a new ADMIN PS
PS> usbipd wsl list
PS> usbipd wsl attach --busid <busid>
WSL> lsusb
WSL> ll /dev/ttyUSB*
PS> usbipd wsl detach --busid <busid> # when you're done with your device
Upvotes: 1
Reputation: 5628
I created a solution combining both Powershell and WSL.
$DeadComport = 3
$COMportList = [System.IO.Ports.SerialPort]::getportnames()
if ($COMportList.Count -cgt 2) {
Write-Output "Too many com-ports connnected! "
Write-Host -NoNewline "Com-ports found:" $COMportList.Count
}else{
ForEach ($COMport in $COMportList) {
$temp = new-object System.IO.Ports.SerialPort $COMport
$portNr = $temp.PortName.SubString(3)
if ($portNr -eq $DeadComport){
continue
}
Write-Output $portNr
$temp.Dispose()
}
}
- You can debug this code in PowerShell ISE and adjust it to meet your preference.
Preferably in the home/your-username/bin
folder, that makes the bash-script globally executable.
#!/bin/bash
echo "Active com-port"
powershell.exe -File "c:\your-folder\comports.ps1"
Now you can just call comscript.sh and it will output the active comport, if more then one device is found it will throw an error message.
Be aware that I'm filtering out com-port 3, since it's always there and active on my computer.
Upvotes: 3