Store specific values in VBA after receiving a set value on MSComm

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NiamhB
    New Member
    • Mar 2013
    • 1

    Store specific values in VBA after receiving a set value on MSComm

    Hi I am creating a VBA application that receives 4 bytes from RS232 on the serial port. When the byte with the decimel value 230 is received I want to store the next two bytes as X and Y respectively.

    I want to create some sort of loop or counter where 230 has the count value 0 represents 230 and then count=1 stores the next byte (i.e. X value) and count=2 stores the third byte i.e. Y.

    Code:
    Private Sub MSComm1_OnComm()
    Dim Data As String
    Dim ID As Long
    Dim X As Long
    Dim Y As Long
    Dim Z As Long
    Dim Count as Integer
    
    Select Case MSComm1.CommEvent
    Case comEvReceive
    Data = MSComm1.Input
    
    If (Data) = 230 Then
           Count = Count +1
    End If
    
    Select Case Count
    
    Case 1 
    X = MSComm1.Input
    Case 2
    Y = MSComm1.Input
    
    End Select
    End Sub
    Can anyone help me? I know the counter doesn't really do anything but I'm not sure how to go about changing it

    Thannks,
    Niamh
    Last edited by Rabbit; Mar 15 '13, 03:39 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You need to declare your count as a global variable so it can keep it's value in between event fires. You will also need to add 1 after setting x.

    Comment

    Working...