使用 SerialPort 将整数发送到 COM1

发布于 2024-08-31 21:28:33 字数 294 浏览 7 评论 0原文

我在将整数发送到串行端口时遇到困难...我正在尝试这样的东西,它运行良好,但我没有在端口上拾取任何东西。

Private Sub fireToPort()
    Dim sPort As New SerialPort("COM1", 56000, Parity.None, 8, StopBits.One)
    sPort.Open()
    sPort.Write(New Byte() {Hex(1), 255}, 0, 0)
    sPort.Close()
End Sub

有什么建议吗?

I'm having difficulty sending an integer to a serial port... I'm trying stuff like this, which run fine but I'm not picking anything up at the port.

Private Sub fireToPort()
    Dim sPort As New SerialPort("COM1", 56000, Parity.None, 8, StopBits.One)
    sPort.Open()
    sPort.Write(New Byte() {Hex(1), 255}, 0, 0)
    sPort.Close()
End Sub

Any advice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月苍老的讽刺 2024-09-07 21:28:33

sPort.Write 的最后一个参数不应该是您要发送的字节数吗?

例如

  sPort.Write(New Byte() {Hex(1), 255}, 0, 2)

Shouldn't the last argument to sPort.Write be the number of bytes you want to send?

e.g.

  sPort.Write(New Byte() {Hex(1), 255}, 0, 2)
帅气尐潴 2024-09-07 21:28:33

获取Mark Russinovich(前 SysInternals)的 PortMon

这将帮助您确认端口是否已打开、正确配置并写入。
您应该会看到进程执行 IRP_MJ_WRITE 操作,结果为 SUCCESS

如果一切正常,那么问题可能是您连接到 COM1 的任何内容都需要不同的波特率。

Get hold of PortMon By Mark Russinovich (formerly of SysInternals).

This will help you confirm that the port is being opened, configured correctly and written to.
You should see your process making an IRP_MJ_WRITE operation with a result of SUCCESS.

If that is all working then the issue is probably that whatever you have attached to COM1 is expecting a different baud rate.

一抹淡然 2024-09-07 21:28:33

试试这个

    'I looped my serial port on COM5
    Dim sPort As New IO.Ports.SerialPort("COM5", 57600, IO.Ports.Parity.None, _
                                         8, IO.Ports.StopBits.One)

    sPort.Handshake = IO.Ports.Handshake.None '<<< pick the correct one

    sPort.Open()
    Dim b() As Byte = New Byte() {1, 255, 0, 0}
    sPort.Write(b, 0, b.Length)
    sPort.BaseStream.Flush()

    'because I looped the port I should be able to read it
    Dim b1(3) As Byte
    sPort.Read(b1, 0, 4)
    sPort.Close()

Try this

    'I looped my serial port on COM5
    Dim sPort As New IO.Ports.SerialPort("COM5", 57600, IO.Ports.Parity.None, _
                                         8, IO.Ports.StopBits.One)

    sPort.Handshake = IO.Ports.Handshake.None '<<< pick the correct one

    sPort.Open()
    Dim b() As Byte = New Byte() {1, 255, 0, 0}
    sPort.Write(b, 0, b.Length)
    sPort.BaseStream.Flush()

    'because I looped the port I should be able to read it
    Dim b1(3) As Byte
    sPort.Read(b1, 0, 4)
    sPort.Close()
初见 2024-09-07 21:28:33
sPort.Write(New Byte() {Hex(1), 255}, 0, 0)

最后一个参数应该是255,这是您将发送的数据的长度。

sPort.Write(New Byte() {Hex(1), 255}, 0, 0)

The last argument is supposed to be 255 which is the length of data that you will send.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文