How to force closing / freeing the serial ports opened by other programs in cmd or Powershell
DevCon Restart: Stops and restarts the specified devices. Valid only on the local computer.
Example 38: Restart a device
The following command uses the DevCon Restart operation to restart the loopback adapter on the local computer. The command limits the search to the Net setup class and, within that class, specifies the device instance ID of the loopback adapter, ROOT*MSLOOP\0000. The at character (@) identifies the string as an device instance ID. The single quote character ('), which requests a literal search, prevents DevCon from interpreting the asterisk in the ID as a wildcard character.
devcon restart =net @'ROOT\*MSLOOP\0000
Or otherwise, drilling into what is available and what you can take action on.
Understanding - Writing and Reading info from Serial Ports
http://www.powertheshell.com/reference/wmireference/root/cimv2/Win32_SerialPort http://www.powertheshell.com/reference/wmireference/root/cimv2/Win32_SerialPort/Reset https://devblogs.microsoft.com/powershell/writing-and-reading-info-from-serial-ports
(Get-WMIObject Win32_SerialPort).DeviceID
# COM3
Get-CimClass -ClassName Win32_SerialPort | Format-List -Force
...
CimClassMethods : {SetPowerState, Reset}
...
Methods
The Win32_SerialPort class has these methods.
Method Description
Reset Not implemented. To implement this method, see the Reset method in CIM_SerialController.
SetPowerState Not implemented. To implement this method, see the SetPowerState method in CIM_SerialController.
https://docs.microsoft.com/en-us/windows/desktop/CIMWin32Prov/win32-serialport
Get-CimClass -ClassName CIM_LogicalDevice | Format-List -Force
Get-CimInstance -ClassName CIM_LogicalDevice |
Where-Object -Property DeviceID -EQ $((Get-WMIObject Win32_SerialPort).DeviceID) |
Select-Object -Property '*' | Get-Member
Get-CimInstance -ClassName CIM_LogicalDevice |
Where-Object -Property DeviceID -EQ $((Get-WMIObject Win32_SerialPort).DeviceID) |
Select-Object -Property TimeOfLastReset
# Writing to a Serial Port using .Net namespace accelerator
[System.IO.Ports.SerialPort]::getportnames()
COM3
$port = New-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
$port.open()
$port.WriteLine(“Hello world”)
$port.Close()
# Reading from a Serial Port using .Net namespace accelerator
$port = New-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
$port.Open()
$port.ReadLine()
Related videos on Youtube
Foad
Updated on September 18, 2022Comments
-
Foad over 1 year
Following this post, I can use
cmd
'smode
command to see the available / free ports. I can also use the PowerShell (PS) 's oneliner:Get-WMIObject Win32_SerialPort | Select-Object DeviceID
to see all the existing ports. The difference indicates the Open / busy ports.
Now I want to interrupt (force close) the ports which are open in any way, including the ones opened outside
cmd
/PS. Please consider- The port is not necessarily opened inside PS. otherwise I could use
$<PortName>.Close()
to do this. - It has to be a
cmd
or PS command not a GUI solution like editing the registry or restating the operating system. - I don't want to delete / destroy the port. I just want to be sure they are free (ready to be opened)
- It should be preferably a one-liner. Something I can run in just one line. using piping is ok.
P.S. I have also posted the question here on Reddit
-
JosefZ about 5 yearsDid you try
Reset
method of theCIM_LogicalDevice
class? -
Foad about 5 years@JosefZ No I haven't. Would you please elaborate? I'm trying this solution for the moment.
- The port is not necessarily opened inside PS. otherwise I could use