开发者

RS232c in VB6 help

开发者 https://www.devze.com 2023-03-06 17:39 出处:网络
Hey all i am new at this RS232c command sending. This is what the documents has for my Yamaha A/V RX-A2000.

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 O开发者_如何学运维nly: 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


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/)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消