powershellserial-portnewline

powershell writing data to COM port remove newline


I need to send data to a COM port but without the carriage return. This is what I have so far;

    $port = New-Object System.IO.Ports.SerialPort
    $port.PortName = "COM10"
    $port.BaudRate = 9600
    $port.Parity = "none"
    $port.DataBits = 8
    $port.StopBits = "None"
    $port.Handshake = "None"
    $port.Open()
    $data = $port.ReadExisting()
    if ($data)
    {
        $port.writeLine("OK")
        ...
        ...
    }

The problem I have is $port.writeLine("OK") buts a carriage return and I think a space before the OK. I have a device listening for the OK string put it never gets it.

Is there any way to remove this?

Thanks


Solution

  • Use the method Write() instead of WriteLine(); the former does not include the newline, the latter does. See the System.IO.Ports.SerialPort docs for a full list of properties and methods.