VB6 帮助中的 RS232c

发布于 2024-11-07 22:34:04 字数 917 浏览 0 评论 0原文

大家好,我是 RS232c 命令发送方面的新手。这是我的 Yamaha A/V RX-A2000 的文档。

PWR
  [PUT Command] 
     @MAIN:PWR=Parameter
  [GET Command]
     @MAIN:PWR=?
        Operating & retrieving Power state of Main Zone
        Initial Auto Feedback is Available
  [Parameters]
        Standby
          PUT: turining its state to Standby / GET: indicating Standby status.
        On
          PUT: turining its state to On / GET: indicating On status.
        On/Standby
          PUT Only: toggling its status between On/Standby

我对如何通过 MSComm 控件发送这样的命令有点困惑。

MSComm.CommPort = 2
MSComm.Settings = "9600,n,8,1"
MSComm.PortOpen = True

If Not MSComm.PortOpen Then
    MsgBox "not opened"
Else
    MSComm.Output = "@MAIN:PWR=On" & Chr(13)

    Do While MSComm.InBufferCount > 0
         Text1.Text = Text1.Text & MSComm.Input
    Loop
End If

那是正确的吗?

大卫

Hey all i am new at this RS232c command sending. This is what the documents has for my Yamaha A/V RX-A2000.

PWR
  [PUT Command] 
     @MAIN:PWR=Parameter
  [GET Command]
     @MAIN:PWR=?
        Operating & retrieving Power state of Main Zone
        Initial Auto Feedback is Available
  [Parameters]
        Standby
          PUT: turining its state to Standby / GET: indicating Standby status.
        On
          PUT: turining its state to On / GET: indicating On status.
        On/Standby
          PUT Only: toggling its status between On/Standby

I am a little confused as to how to send a command like that through the MSComm control.

MSComm.CommPort = 2
MSComm.Settings = "9600,n,8,1"
MSComm.PortOpen = True

If Not MSComm.PortOpen Then
    MsgBox "not opened"
Else
    MSComm.Output = "@MAIN:PWR=On" & Chr(13)

    Do While MSComm.InBufferCount > 0
         Text1.Text = Text1.Text & MSComm.Input
    Loop
End If

Would that be correct?

David

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

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

发布评论

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

评论(2

暗喜 2024-11-14 22:34:05

该代码是健全的,但有一个小缺陷。请注意此代码:

MSComm.Output = "@MAIN:PWR=On" & Chr(13)

Do While MSComm.InBufferCount > 0
     Text1.Text = Text1.Text & MSComm.Input
Loop

在您发送一些数据后,然后立即期望看到响应。该代码不会进入 Do 循环,因为目前还没有任何内容可供读取。

因此,要么在短暂的停顿后检查 .InBufferCount,要么采用不同的方法。我通常有一个计时器每 200 毫秒左右检查一次端口(取决于紧急程度)。您还可以响应 OnComm 事件,但众所周知,如果处理的数据过多,此事件会将 CPU 使用率提高到 100%。听起来您将处理一小部分数据,因此您应该可以接受这两种方法。

The code is sound but has a small flaw. Note this code:

MSComm.Output = "@MAIN:PWR=On" & Chr(13)

Do While MSComm.InBufferCount > 0
     Text1.Text = Text1.Text & MSComm.Input
Loop

In you send some data, then immediately expect to see a response. The code will not enter the Do loop because there will be nothing to read as of yet.

So, either check for .InBufferCount after a small pause or adopt a different approach. I typically have a timer check the port every 200 ms or so (depending on urgency). You could also respond to the OnComm event, but this event has been known to ratchet the CPU usage to 100% if there is too much data going through. It sounds like you'll be dealing with a small set of data, so you should be ok with either approach.

ヤ经典坏疍 2024-11-14 22:34:04

您的代码看起来有点正确 - 无论如何从概念上讲 - 尽管 Do While 的设置确实不太正确。有关编码示例,请参阅 http://support.microsoft.com/kb/194922
(但是“.Handshaking = 2 - comRTS”部分应该是“.Handshaking = 2”)使用事件是RS232 通信更可靠的方法。

请参阅http://www.vbrad.com/article.aspx?id=37 寻求进一步指导。

如果您的 PC 有超级终端(或其他一些 TTY 程序),请使用它尝试与您的 Yamaha 进行手动通信。希望您的用户手册中有一些您可以尝试的示例。谷歌“超级终端 rs232”以获得其他想法;例如 http://www.connectworld.net/interface-troubleshooting.html。走手动路线至少会确认您可以让您的电脑与您的雅马哈对话,并且它将帮助您考虑如何编写代码。

(Google“超级终端替代 Windows”用于其他 TTY 程序;例如 http://www.windowsreference.com/windows-vista/alternatives-to-hyperterminal-in-vista/

Your code looks kinda right - conceptually anyway - although the setup for the Do While really isn't quite right. See http://support.microsoft.com/kb/194922 for a coding example.
(but the ".Handshaking = 2 - comRTS" part should be ".Handshaking = 2") Using events is the more reliable approach to RS232 communication.

See http://www.vbrad.com/article.aspx?id=37 for further guidance.

If your PC has HyperTerminal (or some other TTY program), use that to attempt manual communication with your Yamaha. Hopefully your users manual has some examples you could try. Google on "hyperterminal rs232" for other ideas; such as http://www.connectworld.net/interface-troubleshooting.html. Going the manual route will at least confirm that you can make your PC talk to your Yamaha, and it will help you think about how to write your code.

(Google "hyperterminal substitute windows" for other TTY programs; such as http://www.windowsreference.com/windows-vista/alternatives-to-hyperterminal-in-vista/)

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