From 2f0d4edb043f4979864e4e56edd3db22d3a7a037 Mon Sep 17 00:00:00 2001 From: mkozlowski <10508687+m-kozlowski@users.noreply.github.com> Date: Mon, 21 Aug 2023 19:57:03 +0200 Subject: [PATCH 1/5] Add com port auto detection on WSL1 --- software/script/chameleon_cli_unit.py | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index d4f78083..ad5dc48b 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -8,6 +8,7 @@ import sys import time import serial.tools.list_ports +from platform import uname import chameleon_com import chameleon_cmd @@ -187,11 +188,22 @@ def before_exec(self, args: argparse.Namespace): def on_exec(self, args: argparse.Namespace): try: if args.port is None: # Chameleon auto-detect if no port is supplied - # loop through all ports and find chameleon - for port in serial.tools.list_ports.comports(): - if port.vid == 0x6868: - args.port = port.device - break + platformname = uname().release + if 'microsoft-standard-WSL2' in platformname: + #wsl2 serial support is broken anyway + pass + elif 'Microsoft' in platformname: + process = subprocess.Popen(["powershell.exe","Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); + res = process.communicate()[0] + _comport = res.decode('utf-8').strip() + if _comport: + args.port = _comport.replace('COM', '/dev/ttyS') + else: + # loop through all ports and find chameleon + for port in serial.tools.list_ports.comports(): + if port.vid == 0x6868: + args.port = port.device + break if args.port is None: # If no chameleon was found, exit print("Chameleon not found, please connect the device or try connecting manually with the -p flag.") return From 6e411dc50cf8179cba5c9e7855df7622432be23d Mon Sep 17 00:00:00 2001 From: mkozlowski <10508687+m-kozlowski@users.noreply.github.com> Date: Mon, 21 Aug 2023 21:33:17 +0200 Subject: [PATCH 2/5] WSL2 doesn't need special handling --- software/script/chameleon_cli_unit.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index ad5dc48b..f9fc59f2 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -189,10 +189,7 @@ def on_exec(self, args: argparse.Namespace): try: if args.port is None: # Chameleon auto-detect if no port is supplied platformname = uname().release - if 'microsoft-standard-WSL2' in platformname: - #wsl2 serial support is broken anyway - pass - elif 'Microsoft' in platformname: + if 'Microsoft' in platformname: process = subprocess.Popen(["powershell.exe","Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); res = process.communicate()[0] _comport = res.decode('utf-8').strip() From 9922dc5b50ded3793a1fbe8cd1a28251b889c4b8 Mon Sep 17 00:00:00 2001 From: mkozlowski <10508687+m-kozlowski@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:22:59 +0200 Subject: [PATCH 3/5] Fallback to default path when powershell.exe is not found in PATH --- software/script/chameleon_cli_unit.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index f9fc59f2..7e51cd7e 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -190,11 +190,19 @@ def on_exec(self, args: argparse.Namespace): if args.port is None: # Chameleon auto-detect if no port is supplied platformname = uname().release if 'Microsoft' in platformname: - process = subprocess.Popen(["powershell.exe","Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); - res = process.communicate()[0] - _comport = res.decode('utf-8').strip() - if _comport: - args.port = _comport.replace('COM', '/dev/ttyS') + path = os.environ["PATH"].split(os.pathsep) + path.append("/mnt/c/Windows/System32/WindowsPowerShell/v1.0/") + for prefix in path: + fn = os.path.join(prefix, "powershell.exe") + if not os.path.isdir(fn) and os.access(fn, os.X_OK): + PSHEXE=fn + break + if PSHEXE: + process = subprocess.Popen(["powershell.exe","Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); + res = process.communicate()[0] + _comport = res.decode('utf-8').strip() + if _comport: + args.port = _comport.replace('COM', '/dev/ttyS') else: # loop through all ports and find chameleon for port in serial.tools.list_ports.comports(): From aa3d3e40b477e98ef70a1b7610aea749c7b07af1 Mon Sep 17 00:00:00 2001 From: mkozlowski <10508687+m-kozlowski@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:23:50 +0200 Subject: [PATCH 4/5] Fallback to default path when powershell.exe is not found in PATH --- software/script/chameleon_cli_unit.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index 7e51cd7e..f3da704f 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -198,7 +198,7 @@ def on_exec(self, args: argparse.Namespace): PSHEXE=fn break if PSHEXE: - process = subprocess.Popen(["powershell.exe","Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); + process = subprocess.Popen([PSHEXE,"Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); res = process.communicate()[0] _comport = res.decode('utf-8').strip() if _comport: From 961b8a58f35d925dae26bbad4c262146f4710ebf Mon Sep 17 00:00:00 2001 From: mkozlowski <10508687+m-kozlowski@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:37:12 +0200 Subject: [PATCH 5/5] Faster COM port detection --- software/script/chameleon_cli_unit.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/software/script/chameleon_cli_unit.py b/software/script/chameleon_cli_unit.py index f3da704f..be635f21 100644 --- a/software/script/chameleon_cli_unit.py +++ b/software/script/chameleon_cli_unit.py @@ -198,7 +198,8 @@ def on_exec(self, args: argparse.Namespace): PSHEXE=fn break if PSHEXE: - process = subprocess.Popen([PSHEXE,"Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); + #process = subprocess.Popen([PSHEXE,"Get-CimInstance -ClassName Win32_serialport | Where-Object {$_.PNPDeviceID -like '*VID_6868&PID_8686*'} | Select -expandproperty DeviceID"],stdout=subprocess.PIPE); + process = subprocess.Popen([PSHEXE,"Get-PnPDevice -Class Ports -PresentOnly | where {$_.DeviceID -like '*VID_6868&PID_8686*'} | Select-Object FriendlyName | % FriendlyName | select-string COM\d+ |% { $_.matches.value }"],stdout=subprocess.PIPE); res = process.communicate()[0] _comport = res.decode('utf-8').strip() if _comport: