1
votes

I'm trying to create a powershell script that sends a string from 1 COM port to another one. The string should show up in a teraterm screen of the receiving port. But the I can't get it to work, although it should be straight forward. The teraterm screen is just blank.

Setup: I have 2 usb serial adapter (COM3, COM4), both connected with a physical serial cable. teraterm setup new-line setup 2

The powershell code:

[System.IO.Ports.SerialPort]::getportnames()
#prints COM1 COM3 COM4

$STX = [char] 2
$ETX = [char] 3

$port= new-Object System.IO.Ports.SerialPort COM4,9600,None,8,one
$port.open()
$port.Write("Hello world\r\n")
$port.close()

$port.open()
$port.Write("Hello world\n")
$port.close()

$port.open()
$port.Write("Hello world\r")
$port.close()

$port.open()
$port.WriteLine("Hello world")
$port.close()

$port.open()
$port.WriteLine($STX+"Hello world"+$ETX)
$port.close()

read-host

Anything I missed or any pointers?

Extra info: Also tried sending strings with CMD and C#
We were able to send a string from an old handheld device (so cable is fine)

1
Have you tried plugging in the handheld and sending data to COM4, and then doing $Port.Open();$Port.ReadExisting();$Port.Close() to see if you are able to read from the port? With it being a USB adapter PowerShell may not be seeing it like you would expect, so you may be sending data into nothing. - TheMadTechnician
The string "Hello world\r\n"... do you get a different response if you try "Hello world`r`n". Backtick is the PowerShell escape character. - Matt
@Matt doesn't change still no response - dwana
This blog just craps all over IO.Ports.SerialPort as being largely unreliable. sparxeng.com/blog/software/…. Still reading but it talks about using the WinAPI instead. Have you tried to read and send bytes instead? - Matt
This might also help. blogs.msdn.microsoft.com/bclteam/2006/10/10/…. I cannot test any of this as I dont have the setup for it. - Matt

1 Answers

0
votes

Turns out that the driver for the usb convertors were bad. Fixed the issue with virtual serial ports. Also note that in my situation i had to use a ctrl+c (ETX) to send the message.

$port= new-Object System.IO.Ports.SerialPort COM3,9600,None,8,one
$ETX = [char] 3
$port.open()
$port.WriteLine('Hello')
$port.Write($ETX)
$port.Close()