Page 2 of 3 FirstFirst 123 LastLast
Results 41 to 80 of 86

Thread: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

  1. #41

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Code:
    Private Sub cmdAsk_Click()
    MSComm1.Output = "ask//"
    If (MSComm1.CommEvent = comEvReceive) Then
     
           Attente (300)
            
               MyStrg = MSComm1.Input
    Dim I As Integer
    For I = 1 To Len(MyStrg)
    Dim Relays(15) As Boolean
    
    Next I
    Dim FirstByte As Byte
    Dim SecondByte As Byte
    Dim intRelay As Integer
       FirstByte = Asc(Mid(MyStrg, 1, 1))
       SecondByte = Asc(Mid(MyStrg, 2, 1))
        For I = 7 To 0 Step -1
           Relays(intRelay) = FirstByte And 2 ^ I
           intRelay = intRelay + 1
        Next I
        For I = 7 To 0 Step -1
           Relays(intRelay) = SecondByte And 2 ^ I
           intRelay = intRelay + 1
        Next I
            For I = 0 To 15
         ' "fStatusTrue" & CStr(I + 1) & ".Visible = " & CStr(Relays(I))
        '  Text1.SelText = "fStatus" & CStr(I + 1) & ".backcolor = " & CStr(Relays(I)) & vbCrLf
        Text1.SelText = "Relay state for relay: " & CStr(I + 1) & " is  " & CStr(Relays(I)) & vbCrLf
            
        Dim R As String
        R = CStr(I + 1)
       ' MsgBox R
           If CStr(Relays(I)) = False Then
    
          Text2.SelText = "fStatus" & R & ".BackColor = &HFF&"
            fStatus(R).BackColor = &HFF&
            Else
          Text2.SelText = "fStatus" & R & ".BackColor = &HC000&"
          fStatus(R).BackColor = &HC000&
            End If
        Next I
    
                lstConnection.Text = lstConnection.Text & FirstByte & SecondByte & vbCrLf
                SendMessage lstConnection.hWnd, WM_VSCROLL, SB_BOTTOM, 0
                End If
    End Sub
    So I am hoping to change the color of my 16 Frames Red if False Green if True
    On Form Load I set them all to Red

    Code:
    fStatus1.BackColor = &HFF&
    fStatus2.BackColor = &HFF&
    fStatus3.BackColor = &HFF&
    fStatus4.BackColor = &HFF&
    fStatus5.BackColor = &HFF&
    fStatus6.BackColor = &HFF&
    fStatus7.BackColor = &HFF&
    fStatus8.BackColor = &HFF&
    fStatus9.BackColor = &HFF&
    fStatus10.BackColor = &HFF&
    fStatus11.BackColor = &HFF&
    fStatus12.BackColor = &HFF&
    fStatus13.BackColor = &HFF&
    fStatus14.BackColor = &HFF&
    fStatus15.BackColor = &HFF&
    fStatus16.BackColor = &HFF&

  2. #42

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    OK,
    So now I made them a Control Array and it is working, I will fix little bugs and post back.

  3. #43
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    The reason that you are seeing an error is that you cannot do it that way. If you want to address each frame by number, you must use a control array and the number is the control index. In VB6 you cannot access a control by a String (later versions of VB change these rules).

    The only other alternative, that I know of, is the brute force use of a Select Case or If/Then/Else set of code. Use of a control array will make your life much simpler.

    I can send you a simple project that demonstrates a Frame control array. I created it the way that I offered in the intructions in an earlier reply. Send an email request to [email protected]

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  4. #44

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    I had just change the Frames to a Control away and made the Sub into a function.
    But I messed up some where as it would only work for the first two frames.
    So I deleted and am starting again.
    Code:
    Private Function funAsk()
    MSComm1.InBufferCount = 0
    MSComm1.Output = "ask//"
    If (MSComm1.CommEvent = comEvReceive) Then
     
           Attente (300)
            
               MyStrg = MSComm1.Input
    Dim I As Integer
    For I = 1 To Len(MyStrg)
    Dim Relays(15) As Boolean
    
    Next I
    Dim FirstByte As Byte
    Dim SecondByte As Byte
    Dim intRelay As Integer
       FirstByte = Asc(Mid(MyStrg, 1, 1))
       SecondByte = Asc(Mid(MyStrg, 2, 1))
        For I = 7 To 0 Step -1
           Relays(intRelay) = FirstByte And 2 ^ I
           intRelay = intRelay + 1
        Next I
        For I = 7 To 0 Step -1
           Relays(intRelay) = SecondByte And 2 ^ I
           intRelay = intRelay + 1
        Next I
            For I = 0 To 1
            
           If CStr(Relays(I)) = False Then
            fStatus(I).BackColor = &HFF&
          Else
            fStatus(I).BackColor = &HC000&
            End If
        Next I
    
                lstConnection.Text = lstConnection.Text & FirstByte & SecondByte & vbCrLf
                SendMessage lstConnection.hWnd, WM_VSCROLL, SB_BOTTOM, 0
                End If
    End Function

  5. #45

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    The previous function only seems to change the Backcolor on the first 2 frames fStatus(0) and fStatus(1)

    then skips the rest?

  6. #46
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Your code:

    Code:
            For I = 0 To 1
            
           If CStr(Relays(I)) = False Then
            fStatus(I).BackColor = &HFF&
          Else
            fStatus(I).BackColor = &HC000&
            End If
        Next I
    Should this be:

    Code:
            For I = 0 To 15
            
           If CStr(Relays(I)) = False Then
            fStatus(I).BackColor = &HFF&
          Else
            fStatus(I).BackColor = &HC000&
            End If
        Next I
    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  7. #47

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    OH GOOD Lord!
    In my Cutting and Pasting I cut off the 5!!!
    4 hours lost due to my ignorance

  8. #48

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    I think I am now going to try an add another array with Radio buttons.

    Can you assign a value to all objects in the array?

    example using my frames
    fStatus(ALL FRAMES).backcolor = vbgreen?

    I have googled but not coming up with answer

  9. #49
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    No, you need to loop. However, you know how to do that.

    Code:
    For I = 0 To 15
      fStatus(I).BackColor = vbGreen
    Next I
    Put that code in a subroutine and call the subroutine.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  10. #50

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Thank You Much appreciated!
    Now on to working with Option Button and trying to figure out how to make 16 arrays of 2 (which I think can only be done if you put each set of 2 in its own frame? )

  11. #51
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    OptionButtons work by groups wjem they hosted by a "Parent control." This is one of the main uses of a Frame. Drop your option button pairs in a Frame, PictureBox, or other control that can act as a "parent", and you get independent operation of these now grouped OptionButtons.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  12. #52

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Perfect, this is what I have done!
    Thanx.

    Now the last piece of my puzzle
    Send sequentially:
    "x" - as a char
    a - as HEX number. a[0;FF]. This number correspondends with relays from 1 to 8. The MSB is relay 1.
    b - as HEX number. b[0;FF]. This number correspondends with relays from 9 to 16. The MSB is relay 9.
    "/" - as a char. This is the first part of the delimiter
    "/" - as a char. This is the second part of the delimiter
    Example (a=1A(hex) and b= 05(hex) )
    "x" 1A 05 "/" "/"
    1A(hex)=00011010(bin)
    05(hex)=00000101(bin)
    Relays 1,2,3,6,8,9,10,11,12,13,15 - switched OFF
    Relays 4,5,7,14,16 - switched ON

    Note that there must be minimum 5ms interval between each two commands !
    But this can wait till I get home lol

  13. #53
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Code:
    Dim Command As String
    Dim a As String
    Dim b As String
       a = Chr(&H1A)
       b = Chr(&H05)
       Command = "x" & a & b & "//"
       MSComm1.Output = Command
    If you need a delay use the one that you use in earlier code,

    Attente (5)

    This will result in a 5 mS delay (I'd use a few more mS, myself, for this purpose, 10-15).

    Then create and send the next command.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  14. #54

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    I am going to attach my latest full source.

    I think I may not be doing the arrays for the Frames and Options correctly.

    I have an array of 16 Frames called fRelay
    Each fRelay frame has an array of 2 Option Buttons called OpRelay

    So I am not sure how to get the value for each set

    Example
    fRelay(0) 'Frame
    is OpRelay(0) or OpRelay(1) Selected? 'Option Buttons
    If OpRelay(0) selected then B1 = 0
    If OpRelay(1) selected then B1 = 1
    then go to Next
    fRelay(1) 'Frame
    is OpRelay(0) or OpRelay(1) Selected? 'Option Buttons
    If OpRelay(0) selected then B2 = 0
    If OpRelay(1) selected then B2 = 1

    Do that 8 times

    but because the OpRelay buttons are named the same for each frame, how do I do this?
    Last edited by planethax; Jan 20th, 2011 at 11:25 PM.

  15. #55

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    This doesn't work, but am I CLOSE?
    Seems each B is all 0s or 1s?

    is there a better way?
    am I on the right track?

    Code:
    Private Sub cmdSequential_Click()
    Dim Command As String
    Dim a As String
    Dim b As String
    Dim B0 As String
    Dim B1 As String
    Dim B2 As String
    Dim B3 As String
    Dim B4 As String
    Dim B5 As String
    Dim B6 As String
    Dim B7 As String
    Dim B8 As String
    
    With fRelay(0)
    If OpRelay(0) = True Then
        B0 = 0
    Else: B0 = 1
    End If
    MsgBox B0
    End With
    
    With fRelay(1)
    If OpRelay(0) = True Then
        B1 = 0
    Else: B1 = 1
    End If
    MsgBox B1
    End With
    
    With fRelay(2)
    If OpRelay(0) = True Then
        B2 = 0
    Else: B2 = 1
    End If
    MsgBox B2
    End With
    
    With fRelay(3)
    If OpRelay(0) = True Then
        B3 = 0
    Else: B3 = 1
    End If
    MsgBox B3
    End With
    
    With fRelay(4)
    If OpRelay(0) = True Then
        B4 = 0
    Else: B4 = 1
    End If
    MsgBox B4
    End With
    
    With fRelay(5)
    If OpRelay(0) = True Then
        B5 = 0
    Else: B5 = 1
    End If
    MsgBox B5
    End With
    
    With fRelay(6)
    If OpRelay(0) = True Then
        B6 = 0
    Else: B6 = 1
    End If
    MsgBox B6
    End With
    
    With fRelay(7)
    If OpRelay(0) = True Then
        B7 = 0
    Else: B7 = 1
    End If
    MsgBox B7
    End With
    
    B8 = B0 & B1 & B2 & B3 & B4 & B5 & B6 & B7
    MsgBox B8
    
    '   a = Chr(&H1A)
    '   b = Chr(&H5)
    '   Command = "x" & a & b & "//"
    '   MSComm1.Output = Command
    End Sub
    Edit add:
    Forgot to attach source in prev post...
    Attached Files Attached Files
    Last edited by planethax; Jan 21st, 2011 at 09:21 AM.

  16. #56
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Sonething like this, perhaps
    Code:
    Private Sub cmdSequential_Click()
    Dim Command As String
    Dim byts(1) As Byte
    Dim intI As Integer
    Dim intJ As Integer
    Dim intK As Integer
    '
    ' Assumes that odd numbered Options = on
    '
    intK = 1
    For intI = 0 To 1
        For intJ = 7 To 0 Step -1
        '
        ' Set the appropriate bit if the relay is to be turned on
        '
            If OpRelay(intK).Value = True Then
                byts(intI) = byts(intI) Or 2 ^ intJ
            End If
            intK = intK + 2
        Next intJ
    Next intI
    Command = "x" & byts(0) & byts(1) & "//"
    MSComm1.Output = Command
    End Sub

  17. #57

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Not sure how that is going to work?
    If understand the code you have, it cycles through 8 OpRelay buttons, but there are only 2 per frames
    Frame fRelay(0) has OpRelay(0) and OpRelay(1)
    Frame fRelay(1) has OpRelay(0) and OpRelay(1)
    Frame fRelay(2) has OpRelay(0) and OpRelay(1)
    etc etc.

    I am starting to understand some what more though.
    I believe the code I posted in Post #55 would work, If I renamed each set of OpRelay Option Buttons.
    However, not only is it TOO much/bulky code, I am hoping to learn how to do work with Arrays.

    Cuts my code in half or more lol.
    Last edited by planethax; Jan 21st, 2011 at 09:23 AM.

  18. #58

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Oh wait, I am way off here,
    I never realized how how VB6 named it all, disregard my prev post.

  19. #59
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    I am going to be at a client's office today, so I will not be able to offer any suggestions until Saturday.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  20. #60

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Quote Originally Posted by DickGrier View Post
    I am going to be at a client's office today, so I will not be able to offer any suggestions until Saturday.

    Dick
    Hey No prob!
    I really appreciate your help, hopefully I can figure it out myself before then haha.

    I am having issue with array still, so decide to right it out fully and get functional, then work on array.

    Now I am thinking I am misinterpreting the following, even with the Manufacturers Com tool I can not get the following example to work.

    # Command for many relays setting
    Send sequentially:
    "x" - as a char
    a - as HEX number. a[0;FF]. This number correspondends with relays from 1 to 8. The MSB is relay 1.
    b - as HEX number. b[0;FF]. This number correspondends with relays from 9 to 16. The MSB is relay 9.
    "/" - as a char. This is the first part of the delimiter
    "/" - as a char. This is the second part of the delimiter
    Example (a=1A(hex) and b= 05(hex) )
    "x" 1A 05 "/" "/"
    1A(hex)=00011010(bin)
    05(hex)=00000101(bin)
    Relays 1,2,3,6,8,9,10,11,12,13,15 - switched OFF
    Relays 4,5,7,14,16 - switched ON
    In there tool I send
    x1a05//
    it does nothing but should turn 4,5,7,14,16 ON and 1,2,3,6,8,9,10,11,12,13,15 OFF
    So I must not be sending correctly

    if I just send
    x1a// relays 3,4,8,10,11,16 turn on, rest off
    if I send
    x05// relays 3,4,11,12,14,16 On, rest off

    Can any one help me interpret the instructions? I email manuf but communication is hard because he doesnt know English that well I don't think.
    Last edited by planethax; Jan 21st, 2011 at 02:44 PM.

  21. #61
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Did you try the code I posted in Post #56?

  22. #62

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Quote Originally Posted by Doogle View Post
    Did you try the code I posted in Post #56?
    I did, and it did not do anything (no error or no action)

    So, this is when I went and tried their tool
    sent
    x1a05//
    It also did not do anything.
    So your code maybe right on, but their instructions could be wrong or misunderstood?

  23. #63

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    So I basically sent an email to Manufacturer with the same info as my post 60

    Ask specifically how to exactly send the command.

    This is what I got back

    The whole command consist of 5 bytes.
    Byte N1 is the char "x"
    Byte N2 represents the relays from 1 to 8. The MSB is relay 1. - This means that if you have this byte b10000000 / 128(dec) or 0x80(hex) or the ASCII symbol '€' - relay N1 is ON. If you have b10000001 - relay N1 and relay N8 are ON. What I mean is that when you send this byte you must remember that this byte consist of 8 bits and each bit is one relay. Bit N7 is relay1, bit N6 is relay2...bitN0 is relay 8.
    Byte N3 represents the relays from 9 to 16. The MSB is relay 9. - the same story like byte N2.
    Byte N4 is the char "/"
    Byte N5 is the char "/"
    So I am still unclear what to send as even with their Com Tool
    x1a05// does not work.

    Can some one help me interpret?

  24. #64
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Re my code in Post #56, it is not calling funask so to see the results of the 'Send Sequentially' you'd either need to click on 'ask' or modify the code to:
    Code:
    Private Sub cmdSequential_Click()
    Dim Command As String
    Dim byts(1) As Byte
    Dim intI As Integer
    Dim intJ As Integer
    Dim intK As Integer
    '
    ' Assumes that odd numbered Options = on
    '
    intK = 1
    For intI = 0 To 1
        For intJ = 7 To 0 Step -1
        '
        ' Set the appropriate bit if the relay is to be turned on
        '
            If OpRelay(intK).Value = True Then
                byts(intI) = byts(intI) Or 2 ^ intJ
            End If
            intK = intK + 2
        Next intJ
    Next intI
    Command = "x" & byts(0) & byts(1) & "//"
    MSComm1.Output = Command
    Attente (50)
    funAsk
    End Sub
    also, for Debugging purposes, you may like to include the following into your Form. It will report any Communications problems to the Immediate Window
    Code:
    Private Sub MSComm1_OnComm()
    Select Case MSComm1.CommEvent
        Case comEventBreak
            Debug.Print "Break Received"
        Case comEventDCB
           Debug.Print "Unexpected error retrieving Device Control Block"
        Case comEventFrame
            Debug.Print "Framing Error"
        Case comEventOverrun
            Debug.Print "Overrun"
        Case comEventTxFull
            Debug.Print "Transmit Buffer Full"
        Case comEventRxOver
            Debug.Print "Receive Buffer Overflow"
        Case comEventRxParity
            Debug.Print "Receive Parity Error"
    '
    ' The following are only necessary if Hardware handshaking
    ' is being used
    '
        Case comEventCDTO
            Debug.Print "CD Timeout"
        Case comEventCTSTO
            Debug.Print "CTS Timeout"
        Case comEventDSRTO
            Debug.Print "DSR Timeout"
    End Select
    End Sub
    EDIT: I haven't looked at their Comm Tool but the instructions are quite clear as to what has to be sent.
    The Command consists of 5 bytes.
    1st Byte is "x"
    2nd Byte is a value representing the state of relays 1 to 8.
    3rd Byte is a value representing the state of relays 9 to 16
    4th Byte is "/"
    5th Byte is "/"

    In the second and third bytes, each bit represents one relay, if the bit is set (ie = 1) the relay is to be turned on, if it's not set (ie = 0) the relay is to be turned off.

    In the second byte the most significant bit (MSB, aka 'Leftmost') represents relay 1, the next bit represents relay 2, the next, relay 3, and so on. In the third byte, the MSB represents relay 9, the next bit represents relay 10, the next, relay 11, and so on.

    When you execute the cmdSequential_Click subroutine, it is building bytes 2 and 3 according to the OptionButton settings.
    The 'For I' loop is selecting which byte to process and the 'For J' loop is checking each option in turn and setting the appropriate bit if 'On' has been selected.

    Thus, if you wanted relays 3,6,7,10 and 12 on:

    In the second byte you'd set the 3rd, 6th and 7th bits which would be binary 00100110 = Hex 26 = Decimal 38 = ASCII Character '&'

    In the third byte you'd set the 2nd and 4th bits which would be binary 01010000= Hex 50 = Decimal 80 = ASCII Character 'P'

    So you'd send: x&P// to the device and it should turn relays 3,6,7,10 and 12 on, and all the others off.

    (In your post above, it looks as if, when you're trying to send x1a05// you're sending 7 bytes which is confusing the device.)

    Since values above 127 (Decimal) are in the extented ASCII Table, it may be difficult to enter the appropriate ASCII Character representations for those values from the keyboard, (and those less than 32 Decimal) so I suspect the Comm Tool will have some options / methods which allow you to specify the Character or Binary or Hex or Decimal or even Octal values, which it then converts to the bit values required and sends to the device.
    Last edited by Doogle; Jan 22nd, 2011 at 02:25 AM. Reason: Corrected poorly converted Hex value !

  25. #65

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Ahhh bytes, so you send ASCII symbol.
    Tat makes sence now, I knew I just was not understanding something simple.

    I will check this out and give it a try.
    Thanx so much!

    Edit ad: Though you code did not have the funask, the relays also did not turn on( there are leds for then and you can here them)
    Most likely something I had done, I will recheck it as well.

    Edit 2: x&P// turned on 3,6,7,10,12 and the rest off! Thank you!
    Last edited by planethax; Jan 22nd, 2011 at 01:28 AM.

  26. #66

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    I am starting to get it, see what you neam
    I need to convert byts(0) & byts(1) to Ascii
    ried to convert to hex first but get invalid operation, I will keep trying though, I am almost there!

  27. #67

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    ok, this seems to be working, still need to test a bit more.

    A few Items I just Dim'd did not "Dim as" because I am not sure what to dim them as?
    Dim B1 ?
    Dim B2 ?
    Dim num as ?
    Dim Value as ?

    Sub
    Code:
    Private Sub cmdSequential_Click()
    Dim Command As String
    Dim byts(1) As Byte
    Dim intI As Integer
    Dim intJ As Integer
    Dim intK As Integer
    Dim B1
    Dim B2
    
     'Assumes that odd numbered Options = on
    
    intK = 1
    For intI = 0 To 1
        For intJ = 7 To 0 Step -1
    
         'Set the appropriate bit if the relay is to be turned on
    
            If OpRelay(intK).Value = True Then
                byts(intI) = byts(intI) Or 2 ^ intJ
            End If
            intK = intK + 2
        Next intJ
    Next intI
    
    B1 = HexByte2Char(byts(0))
    B2 = HexByte2Char(byts(1))
    B1 = hex2ascii(B1)
    B2 = hex2ascii(B2)
    MsgBox B1
    MsgBox B2
    Command = "x" & B1 & B2 & "//"
    MsgBox Command
    MSComm1.Output = Command
    End Sub
    Functions


    Code:
    Public Function hex2ascii(ByVal hextext As String) As String
      Dim y As Integer
      Dim num
      Dim Value
    For y = 1 To Len(hextext)
        num = Mid(hextext, y, 2)
        Value = Value & Chr(Val("&h" & num))
        y = y + 1
    Next y
    
    hex2ascii = Value
    End Function
    Public Function HexByte2Char(ByVal Value As Byte) As String
      ' Return a byte value as a two-digit hex string.
      HexByte2Char = IIf(Value < &H10, "0", "") & Hex$(Value)
    End Function
    Last edited by planethax; Jan 22nd, 2011 at 02:31 AM.

  28. #68
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    You shouldn't need to convert byts(0) or byts(1) since they contain the correct bit pattern that the device expects.

  29. #69

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Seems perfect!
    Thanx SO MUCH
    I only have one bug to work out when sending command manually and the funAsk
    FirstByte = Asc(Mid(MyStrg, 1, 1)) ' error 5 invalid procedure or call

    Small bug I imagine, something for tomorrow!!!

    Thanx again!

  30. #70
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    You know, I'm being / am particularly dense / stupid in trying to concatinate a Byte into a String, must be my age or something.

    All you needed to do is just change
    Code:
    Command = "x" & byts(0) & byts(1) & "//"
    to
    Code:
    Command = "x" & Chr(byts(0)) & Chr(byts(1)) & "//"
    Last edited by Doogle; Jan 22nd, 2011 at 02:58 AM.

  31. #71
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Quote Originally Posted by planethax View Post
    I only have one bug to work out when sending command manually and the funAsk
    FirstByte = Asc(Mid(MyStrg, 1, 1)) ' error 5 invalid procedure or call
    The implication of that error is that MyStrg is empty (has a length of zero), which is surprising since it appears that the comEvReceive event has been recognised,there should be at least 1 byte available and you've read it. I suspect it's the dreaded 'DoEvents' in the attente subroutine which you're calling after recognising the comEvReceive event but befoe processing the data. The cause is probably also due to the OnComm event code I suggested you added, being re-entered whilst already active, which can lead to unpredictable results, so perhaps you should remove it.

    This is one of the reasons I prefer an asynchronous approach rather than timing, you don't need DoEvents at all, also, although it's highly unlikely to the point of being impossible in your application, theoretically, you could miss a response if the device takes longer than 300 mS to respond, also you are adding a 300mS processing delay, all of which might not be necessary.

    However the pros and cons can be discussed forever, there's always trade-offs and one man's meat is another man's poison

    Whatever, with the approach you've taken, I think you should wait and then check for the comEvReceive. I'd move the call to Attente and 'tidy up' the code a little:
    Code:
    Private Function funAsk()
    Dim I As Integer
    Dim FirstByte As Byte
    Dim SecondByte As Byte
    Dim intRelay As Integer
    Dim Relays(15) As Boolean
    MSComm1.InBufferCount = 0
    MSComm1.Output = "ask//"
    '
    ' Wait for the device to respond
    '
    Attente (300)
    '
    ' Anything to receive ?
    '
    If (MSComm1.CommEvent = comEvReceive) Then
        '
        ' Yes - Process it
        '
        MyStrg = MSComm1.Input
        FirstByte = Asc(Mid(MyStrg, 1, 1))
        SecondByte = Asc(Mid(MyStrg, 2, 1))
        For I = 7 To 0 Step -1
            Relays(intRelay) = FirstByte And 2 ^ I
            intRelay = intRelay + 1
        Next I
        For I = 7 To 0 Step -1
            Relays(intRelay) = SecondByte And 2 ^ I
            intRelay = intRelay + 1
        Next I
        For I = 0 To 15
            If CStr(Relays(I)) = False Then
                fStatus(I).BackColor = &HFF&
            Else
                fStatus(I).BackColor = &HC000&
            End If
        Next I
    End If
    End Function
    (I've indented it to make it more 'readable')
    Last edited by Doogle; Jan 22nd, 2011 at 04:53 AM.

  32. #72
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    So, this is just a code fragment (what I call, "air code").

    To set a relay state, you have to follow the protocol rules. The first rule is that you must send a bit-mapped command to the relay board that changes only the relays that you want to change and leave all others in the state that they were before the command. This means that you must execute the change in three (or four) steps.

    1. Send an "ASK" command that gets the current relay states. In my code fragment, I call these two bytes n1 and n2, where previously we I called them FirstByte and SecondByte.
    2. Change only the bit for the relay that we are currently changing, while keeping the bits for all other relays unchanged.
    3. Send the relay change command.
    4. If you want to do this -- send another "ASK" command to verify the change, and to update the UI.

    Note, again, I have used control arrays, this time for the option buttons. I added the On/Off option buttons to the status Frame controls (the Index of which is the same as the Index for the On/Off option buttions). Note, also, the UI intialization code, and the bit-mapped On/Off commands, which I encoded in an array.

    Code:
    Dim RelayOnValL(7) As Byte
    Dim RelayOffValL(7) As Byte
    Dim RelayOnValH(7) As Byte
    Dim RelayOffValH(7) As Byte
    
    Private Sub Form_Load()
    
    For I = 7 To 0 Step -1
       RelayOnValL(I) = 2 ^ I
       RelayOffValL(I) = &HFF Xor (2 ^ I)
       RelayOnValH(I) = 2 ^ I
       RelayOffValH(I) = &HFF Xor (2 ^ I)
    Next I
    
    For I = 0 To 15
       'set the initial state of all option buttons to "Off"
       RelayOff(I).Value = True
       'presumably, this is the power on state?
    Next I
    End Sub
    
    Private Sub RelayOff_Click(Index As Integer)
        If MSComm1.PortOpen = True Then
            Dim n1 As Byte
            Dim n2 As Byte
            MSComm1.Output = "ask//"
            Attente (300)
            Dim myStrg As String
            myStrig = MSComm1.Input
            n1 = Asc(Mid(myStrg, 1, 1))
            n2 = Asc(Mid(myStrig, 2, 1))
            'To turn on a relay, send the command:
            '"x" & Chr(n1) & Chr(n2) & "//"
            'where n1 and n2 are first read from the board
            'using the ASK command,
            'Then, create NEW n1 and n2 values:
            If Index < 8 Then
               n1 = n1 And RelayOffValL(Index)
            Else
               n2 = n2 And RelayOffValH(Index)
            End If
            'n1 and n2 to the result
            'the block above resets just a single bit to reflect the change
            MSComm1.Output = "x" & Chr(n1) & Chr(n2) & "//"
        End If
    End Sub
    
    Private Sub RelayOn_Click(Index As Integer)
        If MSComm1.PortOpen = True Then
            Dim n1 As Byte
            Dim n2 As Byte
            MSComm1.Output = "ask//"
            Attente (300)
            Dim myStrg As String
            myStrig = MSComm1.Input
            n1 = Asc(Mid(myStrg, 1, 1))
            n2 = Asc(Mid(myStrig, 2, 1))
            'To turn on a relay, send the command:
            '"x" & Chr(n1) & Chr(n2) & "//"
            'where n1 and n2 are first read from the board
            'using the ASK command,
            'Then, create NEW n1 and n2 values:
            If Index < 8 Then
               n1 = n1 Or RelayOnValL(Index)
            Else
               n2 = n2 Or RelayOnValH(Index)
            End If
            'the block above sets just a single bit to reflect the change
            MSComm1.Output = "x" & Chr(n1) & Chr(n2) & "//"
        End If
    End Sub
    Realize that this code isn't perfect (and I haven't tested it, since I do not have your hardware). There are several places where it could be optimized, and there is no error handling. I've tried to make clear what I've done, but you need to study the subtle point of using the Or function to set an individual bit, and the And function to clear an individual bit.

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  33. #73
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    To implement Dick's suggestion into your existiing code you need to add a couple of lines into funAsk where you are setting the colour of the fStatus control array to reflect the current status.
    Code:
        For I = 0 To 15
            If CStr(Relays(I)) = False Then
                fStatus(I).BackColor = &HFF&
                OpRelay(2 * I).Value = True
            Else
                fStatus(I).BackColor = &HC000&
                OpRelay((2 * I) + 1).Value = True
            End If
        Next I
    Since you're always calling funAsk after any change, this ensures that the Option Buttons always reflect the current state of the relays so those that you haven't changed when you click on 'Send Sequentially' will retain their existing state. This allows you to follow the protocol, described in Dick's post, in one step, without the need to repeatedly interrogate the device.

    EDIT: BTW the above changes removes the need for the set of cmdRelayxx buttons since you can change the state of any single relay, without affecting the others, using the Option Buttons and 'Send Sequentially'

    However, as Dick states, it's important to get a grasp of the Logical Operators: And, Or and Xor, for manipulating bits within bytes, especially if you want to do more complex things, like setting up an automated timed sequence. Take a good look at Dick's code, especially (IMHO) the '&HFF Xor (2 ^ I)' for generating a Mask which is used to turn a single bit off.
    Last edited by Doogle; Jan 23rd, 2011 at 03:20 AM.

  34. #74
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Quote Originally Posted by planethax in Post #6 View Post
    This should be a simple 5 minute app lol
    I just had a little giggle to myself; here we are 3 days and 67 posts later, still developing it.

    Let's hope that the light at the end of the tunnel is not an oncoming Train !

  35. #75

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Sorry I neglected thread, got tied up with other projects.

    I have not tried the last code bits yet, but have saved them and will work with them.

    I must give a HUGE thanx to you both, I have learned a lot!

    At this point I am happy with how this is working.
    Honestly, I do not even need this particular app directly for my app.
    This was just an excercise to learn how to work with relay board and now have reference and C/P available for using it in my main project.

    BTW, I have managed to get the relays to Fire/Check/Report via Voice commands!!!!!
    Needs lots of tweeking and testing but hope to have full Project done by mid March!

    Thank you again!!!

    I am marking this thread Resolved++++++
    If I have further questions I will create a new thread as it shouldn't be related to this threads title.


    Quote Originally Posted by Doogle View Post
    I just had a little giggle to myself; here we are 3 days and 67 posts later, still developing it.

    Let's hope that the light at the end of the tunnel is not an oncoming Train !
    Haha, most a knowledgeable coder should only have taken 5 minutes

    Here is a quick clip of controlling relays via voice.
    http://www.youtube.com/watch?v=K-uG7JmJpvk

  36. #76
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227

    Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Oh i'm bringing this back from the dead

    I have the 4 port relay version and i seem to not be able to get it to work with the current mycontrol4.zip code posted on this topic.

    I've tried emailing/PMing the OP (planethax) but have not had any reply back. Could anyone be willing to help me out in figuring out why its not working for me but works for him? To my understanding all the relays use the same chip - just different relay count.

    David

  37. #77
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Are you sure you're using the right Comm Port ?

    (BTW you should really start your own thread rather than hi-jack someone else's)

  38. #78

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    651

    Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    I have been away.

    Let me dig up old source (new is now integrated into complete app)

  39. #79
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227

    Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Quote Originally Posted by planethax View Post
    I have been away.

    Let me dig up old source (new is now integrated into complete app)
    I appreciate it, planethax

    David

  40. #80
    Addicted Member
    Join Date
    Sep 2003
    Posts
    227

    Re: [RESOLVED] No Mscomm receive USB 16 Channel Relay Module - RS232 Controlled

    Any update on finding that code, planethax?

    David

Page 2 of 3 FirstFirst 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width