question

FrancoisBondu-8430 avatar image
0 Votes"
FrancoisBondu-8430 asked IanXue-MSFT answered

IOException error in opening a virtual serial port with Powershell

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

cooldadtx avatar image
0 Votes"
cooldadtx answered

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()
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.