Labels

Thursday, 5 February 2026

Python can replace BAT

 Intro

For a long while I have put up with the limitations of Windows antiquated 50 year old batch file system.  With plenty of trial and error I can set up batch files to do simple tasks but it continually frustrating that they have none of the capabilitiy or power of linux shell scripts (which are even older!).  I need some better way to control BEN2 processes and I foolishly thought to try PowerShell as an alternative.  As you would expect it is horrendous, a whole new useless language to help you dig into the cesspit of Windows.

It then occured to me to use python; after all it is a scripting language, windows friendly, easy to use and has an interface to the underlying Operating System.

Serial Port Control


The arses at Microsoft have seen fit to reduce serial port functionality in Windows 11.  Windows 10 shared ports quite happily with WSL programs.  By default Serial ports are now not available within WSL.  You need to install a program called usbipd from github to attach ports to WSL. 

The instructions are not trivial.  You have to identify the correct device in a list that usbipd gives you then "bind" and "attach"  it to wsl.

This isn't something I am capable of doing within a batch file.


Python

Our python script first reads in output from the command "usbipd list" to see if it can find the COM port, which will be labelled as "USB serial converter".
If we find it we capture BUSID and STATE values for that line.

Once we have these values we can attach the device (BUSID=3-3) in this example to WSL.  The bind command only has to be done once so we dont issue it every time in the script.


Outro

Note that the BUSID can change depending on what is attached to the PC.
Also, although device 3-3 is called COM3 in Windows it is called /dev/ttyUSB0 in WSL.
Windows doesn't help me with working this out.

A very irritating situation :(



No comments:

Post a Comment