6. May 2021

How to find COM port of connected IoT device on Windows

When you need to talk to an IoT device over RS232 connected to the computer via USB cable, the first thing that you need to know is the name of the port. Windows assigns port numbers like COM3, COM7, COM21 to connected devices. How to find the number?
The quick way is to type the following command:

The quick way is to type following command:

chgport

The output:

COM10 = \Device\VCP1
COM9 = \Device\VCP0

For older versions of Windows you can use:

mode

The output:

Status for device COM9:
-----------------------
    Baud:            1200
    Parity:          None
    Data Bits:       7
    Stop Bits:       1
    Timeout:         OFF
    XON/XOFF:        OFF
    CTS handshaking: OFF
    DSR handshaking: OFF
    DSR sensitivity: OFF
    DTR circuit:     ON
    RTS circuit:     ON

You can find all these hints also on SuperUser.

But what to do when the device is connected and the list of COM ports is empty?

The most common reason is that Windows is missing one of the following drivers:

Steps to resolve the problem:

The system should pick the installed driver and assign it to the device.

You can use also idf-env tool to install the drivers by following PowerShell commands:

Invoke-WebRequest 'https://dl.espressif.com/dl/idf-env/idf-env.exe' -OutFile .\idf-env.exe
.\idf-env.exe driver install --ftdi --silabs

Footnote: following PowerShell command might NOT list all connected IoT COM devices, but you can find it on many forums on the internet. Often it displays just COM3 and COM4 which are not associated with IoT device.

Get-WMIObject Win32_SerialPort | Select-Object Name,DeviceID,Description
Facebook Comments

3. May 2021

How to add/remove device driver from command-line on Windows

Windows operating system contains a tool that allows manipulation with system drivers stored in .INF and .CAT files. The operation requires Administrator privileges.

Command to install the driver:

pnputil /add-driver my-driver.inf /install

The correct installation display the following result:

Microsoft PnP Utility

Adding driver package:  my-driver.inf
Driver package added successfully.
Published Name:         oem3.inf

Total driver packages:  1
Added driver packages:  1

To verify the result you can plug the device and it should pick the correct driver. The system copied content from my-driver.inf to C:\Windows\INF\oem3.inf. The number in file name might be different.

The remove the driver from the system and perform uninstallation of the driver from devices type following command:

pnputil /remove-driver my-driver.inf /uninstall

Expected result:

Microsoft PnP Utility

Driver package deleted successfully.
Facebook Comments

27. April 2021

Quick way to clear all Windows Defender exclusions

Windows Defender has support for a list of exclusions that won’t be considered for scanning. The UI for manipulating with exclusions is quite simple and does not allow operations on multiple exclusions at once. Fortunately, there are PowerShell commands which can help.

These commands are Add-MpPreference, Get-MpPreference, and Remove-MpPreference.

Following commands allows to quickly remove all exclusions of Path. The command must be started in Administrator’s shell:

$Paths=(Get-MpPreference).ExclusionPath
foreach ($Path in $Paths) { Remove-MpPreference -ExclusionPath $Path }

A similar principle can be applied on the rest of the exclusions: ExclusionExtension, ExclusionIpAddress, ExclusionProcess

$Extensions=(Get-MpPreference).ExclusionExtension
foreach ($Extension in $Extensions) { Remove-MpPreference -ExclusionExtension $Extension }

$Processes=(Get-MpPreference).ExclusionProcess
foreach ($Process in $Processes) { Remove-MpPreference -ExclusionProcess $Process }
Facebook Comments

22. April 2021

How to run Linux GUI application on Windows WSL2 with MobaXterm and Windows Terminal

MobaXterm is a great software that allows running applications from Linux on Windows in a nice integrated way without the need of installing extra XServer.

MobaXterm has also support for WSL2 which makes it easy to launch Linux GUI applications from Linux on Windows.

After installation of Linux distribution like Ubuntu or openSUSE on WSL2, you will find new sessions with the prefix WSL in MobaXterm. Simply click the session label and a new terminal will be automatically configured to talk to the local XServer.

Now you can start any GUI command, e.g. xeyes.

The same XServer from MobaXterm can be used also from other terminals and shells like Windows Terminal. It’s sufficient to export environment variable DISPLAY in the Linux WSL2 session:

export DISPLAY=127.0.0.1:0.0
xeyes

Note: xeyes are part of x11-apps, you can install them by command

sudo apt install x11-apps
Facebook Comments

20. April 2021

How to statically link Rust application for Windows

Rust compiler can generate a single binary that contains all dependencies. This is a great feature for creating stand-alone tools or tools for containers.

There is one gotcha for Windows which does not appear on a developer’s machine. When you move the application to a brand new installation of Windows or you try to start the in Windows Docker Container which contains Windows for Datacenters the app won’t start.

Why? The system is missing Microsoft Visual C++ Redistributable 2015-2019 (vc_redist) which you can download from microsoft.com.

It’s quite inconvenient to force the user to install the package in the case of a stand-alone Rust tool. The package installation even requires elevation of privileges.

The alternative approach to distributing vc_redist is to statically link CRT library into the application. It will result in a slightly bigger application around +100KB.

It’s necessary to tell rustc to perform static linking of CRT.

In the project create file .cargo/config.toml:

[target.x86_64-pc-windows-msvc]
rustflags = ["-C", "target-feature=+crt-static"]

Rebuild the application and the new binary is statically linked and can run even on Windows 8 without vc_redist.

The interesting part is how to determine the proper target name which is on the first line of the example configuration. Many examples on the internet are referring to target.i686-pc-windows-msvc which does not work. Use the following command to determine parts of the name of the target:

rustc --print cfg
target_arch="x86_64"
target_endian="little"
target_env="msvc"
target_family="windows"
target_feature="fxsr"
target_feature="sse"
target_feature="sse2"
target_os="windows"
target_pointer_width="64"
target_vendor="pc"
windows

The target name is composed of values on the lines above.

Facebook Comments