IOException error in opening a virtual serial port with Powershell

Francois Bondu 1 Reputation point
2021-04-01T14:55:57.457+00:00

Hello,

I have installed a plug-and-play measurement instrument, with a USB terminal, and it appears as a serial COM port.

In windows device manager, COM3 is the only port.

with properties / main tab, the status says that the device works properly.

I want exchange API commands via powershell. There is an error at the open() step.

Could you help?

Thanks

Powershell (with administrator rights) commands

> $port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,One
> $port.NewLine = '\r'
> $port.ReadTimeout = 100
> $port.WriteTimeout = 100
> Start-Sleep -Milliseconds 100
> $port.open()
> Start-Sleep -Milliseconds 100
> $port

COM3
Exception lors de l'appel de « Open » avec « 0 » argument(s) : « Paramètre incorrect.
»
Au caractère Ligne:7 : 1

  • $port.open()
  • ~~~~~~~~~~~~
  • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  • FullyQualifiedErrorId : IOException

BaseStream :
BaudRate : 9600
BreakState :
BytesToWrite :
BytesToRead :
CDHolding :
CtsHolding :
DataBits : 8
DiscardNull : False
DsrHolding :
DtrEnable : False
Encoding : System.Text.ASCIIEncoding
Handshake : None
IsOpen : False
NewLine : \r
Parity : None
ParityReplace : 63
PortName : COM3
ReadBufferSize : 4096
ReadTimeout : 100
ReceivedBytesThreshold : 1
RtsEnable : False
StopBits : One
WriteBufferSize : 2048
WriteTimeout : 100
Site :

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 47,966 Reputation points
    2021-04-01T16:38:58.077+00:00

    Sounds like the port isn't visible. Get the port names first and make sure the COM3 port shows up.

    [System.IO.Ports.SerialPort]::GetPortNames()
    
    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-04-02T03:08:44.783+00:00

    Hi,

    The serial port could be already in use. You can check with the WMI class Win32_SerialPortConfiguration.

    Get-WmiObject -Class Win32_SerialPortConfiguration -Filter 'Caption = "COM3"' | Select-Object -Property IsBusy  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments