如何使用PowerShell通过串行端口发送十六进制命令给监视器

huangapple go评论106阅读模式
英文:

How to send hexadecimal commands to a monitor over a Serial Port with PowerShell

问题

以下是翻译好的内容:

"这有点奇怪,但由于我完全不知道如何使用串口或PowerShell,我想不出其他地方可以求助。

我尝试的是向一个带有RS232端口的显示器发送基本命令,以控制显示器的属性,例如亮度、对比度、背光等。

我试图使用PowerShell进行测试。我可以在PowerShell中创建$port并将其分配给显示器连接的相应COM#,但我不知道如何实际发送命令,因为显示器上的控制器必须以十六进制形式理解它。
显示器可以使用相同的十六进制布局返回确认,但我找不到在PowerShell控制台上显示该响应的方法。
到目前为止,这是我能够得到的内容。

PS C:\Users\Kingdel> [System.IO.Ports.SerialPort]::getportnames()
COM1
COM2
COM3
COM4
COM5
COM6
PS C:\Users\Kingdel> $port= new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one
PS C:\Users\Kingdel> $port.open()
PS C:\Users\Kingdel> $port.WriteLine("0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94")
PS C:\Users\Kingdel>

有人能指导我如何将此命令发送到显示器并查看返回的确认吗?

我愿意尝试不同的终端,我已经尝试过PuTTy和Termite,但似乎都没有成功。"

英文:

Okay this is a bit of a weird one but due to my complete lack of knowledge on how to use Serial Ports or PowerShell i couldn't think of anywhere else to go.

What I'm trying to do is send basic commands to a monitor that has a RS232 port on it that can be used to control the properties of the monitor, i.e. Brightness, Contrast, Backlight etc.

I'm attempting to use PowerShell to do this for testing purposes. I can create the $port in PowerShell and assign it to the relevant COM# that the monitor is connected to but I'm at a loss as to how to actually send the command to it as it must be Hexadecimal for the controller on the monitor to understand it.
The monitor is capable of returning an acknowledgement using the same Hex layout but I'm unable to find a way of showing that response on the Powershell console.
This is what I have been able to get so far.

PS C:\Users\Kingdel> [System.IO.Ports.SerialPort]::getportnames()
COM1
COM2
COM3
COM4
COM5
COM6
PS C:\Users\Kingdel> $port= new-Object System.IO.Ports.SerialPort COM1,9600,None,8,one
PS C:\Users\Kingdel> $port.open()
PS C:\Users\Kingdel> $port.WriteLine("0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94")
PS C:\Users\Kingdel>

Anyone able to point me in the right direction as to how I can send this command to the monitor and to view the returned acknowledgement.

I am open to trying different terminals, I have tried PuTTy and Termite and neither of them were successful as far as I can tell.

答案1

得分: 1

这是一个非常好的问题。也许我可以帮助你。

SerialPort.WriteLine() 方法接受一个要写入输出缓冲区的字符串,因此使用它,你实际上是发送一个字符串参数。

要将内容发送到 [System.IO.Ports.SerialPort] 对象,你需要使用 SerialPort.Write() 方法,并使用一个 Byte[] 参数。Write() 方法可以使用来自缓冲区的数据将一定数量的字节发送到串行端口。

你还需要发送三个参数,它们是 buffer Byte[]offset Int32count Int32。所以在你的情况下,你可以这样做:

[Byte[]] $hex = 0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94
$port.Write($hex, 0, $hex.Count)

缓冲区参数是包含要写入端口的数据的数组,偏移量是缓冲区数组中从零开始复制字节到端口的基于零的字节偏移量,而计数是要写入的字节数。

英文:

That's a really good question. Maybe I can help with this.

The SerialPort.WriteLine() method takes in a string to write to the output buffer, so using this, you're essentially sending an argument of strings.

To send something over to the [System.IO.Ports.SerialPort] object, you need to use SerialPort.Write() with a Byte[] argument. The Write() method can take in a number of bytes to the serial port using data from a buffer.

You also need to send it three arguments which are buffer Byte[], offset Int32, and a count Int32. So in your case, you can do the following:

[Byte[]] $hex = 0xA6,0x01,0x00,0x00,0x00,0x03,0x01,0x31,0x94
$port.Write($hex, 0, $hex.Count)

The buffer argument is the array that contains the data to write to the port, offset is the zero-based byte offset in the buffer array at which to begin copying the bytes to the port, and the count is the number of bytes to write.

huangapple
  • 本文由 发表于 2020年1月4日 00:51:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582329.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定