powershelltcpstreamreader

Powershell - How to set TCP Send Receive Line End Character? <CR> vs <LF>


I was trying to make a little TCP Client Send and Receive script to send actual date to an industrial camera. The device expects [date]<CR> as end char, and sends back OK<CR> or ER<CR> message.

I've found a base script for send here: https://riptutorial.com/powershell/example/18118/tcp-sender

I've added StreamReader function to it, test with Hercules SETUP Utility and it gives back a return message... if the end char is <LF>... But I need <CR> to send and receive also. Communication manual, page 527.

Function Send-TCPMessage { 
    Param ( 
            [Parameter(Mandatory=$true, Position=0)]
            [ValidateNotNullOrEmpty()] 
            [string] 
            $EndPoint
        , 
            [Parameter(Mandatory=$true, Position=1)]
            [int]
            $Port
        , 
            [Parameter(Mandatory=$true, Position=2)]
            [string]
            $Message
    ) 
    Process {
        # Setup connection 
        $IP = [System.Net.Dns]::GetHostAddresses($EndPoint) 
        $Address = [System.Net.IPAddress]::Parse($IP) 
        $Socket = New-Object System.Net.Sockets.TCPClient($Address,$Port) 
    
        # Setup stream wrtier 
        $Stream = $Socket.GetStream() 
        $Writer = New-Object System.IO.StreamWriter($Stream)
        $Reader = New-Object System.IO.StreamReader($Stream)

        # Write message to stream
        $Message | % {
            $Writer.WriteLine($_)
            $Writer.Flush()
        }

        $Response = $Reader.ReadLine()

        # Close connection and stream
        $Stream.Close()
        $Socket.Close()

        return $Response
    }
}

$retunMessage = Send-TCPMessage -Port 23 -Endpoint 127.0.0.1 -message "My first TCP message !"
Write-Host "Return Message = " $retunMessage

I was trying to test with Hercules SETUP Utility, and Google up the right parameter/setting for modify end character. With no success. Someone could help to identify, what parameters should I set to modify the end char to <CR>?


Solution