Handshaking With Device

Serial COM port communication works great on all my devices except one. None of them use handshaking and I can’t get the one that requires handshaking to work. Am I setting this up correctly? Right now I’m just using the following commands

$port=new-object system.io.ports.serialport COM5, 9600, None, 8, one
$port.handshake="XOnXOff"
$port.open()

But it gives the following error

Exception calling "Open" with "0" argument(s): "Access to the port 'COM5' is denied."
At line:1 char:11
+ $port.open <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

I can use software or hardware handshaking on the device. Right now I am trying to use the software handshaking option, but it isn’t working. Can anyone tell me what I’m doing wrong? Thank you for your time!

Is something else using the port?

If you run

$port=new-object system.io.ports.serialport COM5, 9600, None, 8, one
$port.handshake="XOnXOff"
$port.open()

$port2=new-object system.io.ports.serialport COM5, 9600, None, 8, one
$port2.handshake="XOnXOff"
$port2.open()

You get the same access denied error when you call the open() method on port 2. This would imply that something is already using the port. You should be able to check with WMI by querying the IsBusy value:

Get-WmiObject Win32_SerialPortConfiguration -Filter 'Caption = "COM5"' | Select IsBusy

If that returns true something else is using COM5.