VB.net MSComm port USB 4 port relay

Discussion in 'Programmer's Corner' started by StealthRT, Oct 20, 2011.

  1. StealthRT

    Thread Starter Active Member

    Mar 20, 2009
    223
    0
    I am trying to get my 4 port USB relay board to work in VB.net using the COM PORT. I found some code from a poster who has the 16 port USB relay version. When trying his code i can only get it to turn on relays 1-3 all at the same time. The product can be found here: USB 4 port relay

    I was following what the guy in the post was doing to turn his on:
    That forum post can be found here: Post

    The USB relay board is using a FTDI chip FT245RL. The datasheet can be found here

    The code i am trying to use is this:
    Code ( (Unknown Language)):
    1.  
    2. If MSComm.PortOpen = True Then
    3.     MSComm.PortOpen = False
    4. End If
    5.  
    6. MSComm.CommPort = 6
    7. MSComm.Settings = "9600,N,8,1"
    8. MSComm.InputLen = 0
    9. MSComm.RThreshold = 1
    10. MSComm.SThreshold = 0
    11. MSComm.PortOpen = True
    12.  
    13. 'Turn relay #1 ON
    14. MSComm.Output = "01+//"
    15.  
    And like i said above, it turns on relays 1-3 when using that code above.. but that code only works in VB6 anyways... :(

    Then i finally got some source code from the seller for the FTDI commands:
    Code ( (Unknown Language)):
    1.  
    2. Friend Class USB_FTDI_TEST
    3.     Inherits System.Windows.Forms.Form
    4.     Private Declare Function FT_Open Lib "FTD2XX.dll" (ByVal intDeviceNumber As Short, ByRef lnghandle As Integer) As Integer
    5.     Private Declare Function FT_OpenEx Lib "FTD2XX.dll" (ByVal arg1 As String, ByVal arg2 As Integer, ByRef lnghandle As Integer) As Integer
    6.     Private Declare Function FT_Close Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    7.     Private Declare Function FT_Read Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lpszBuffer As String, ByVal lngBufferSize As Integer, ByRef lngBytesReturned As Integer) As Integer
    8.     Private Declare Function FT_Write Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lpszBuffer As String, ByVal lngBufferSize As Integer, ByRef lngBytesWritten As Integer) As Integer
    9.     Private Declare Function FT_SetBaudRate Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lngBaudRate As Integer) As Integer
    10.     Private Declare Function FT_SetDataCharacteristics Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal byWordLength As Byte, ByVal byStopBits As Byte, ByVal byParity As Byte) As Integer
    11.     Private Declare Function FT_SetFlowControl Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal intFlowControl As Short, ByVal byXonChar As Byte, ByVal byXoffChar As Byte) As Integer
    12.     Private Declare Function FT_ResetDevice Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    13.     Private Declare Function FT_SetDtr Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    14.     Private Declare Function FT_ClrDtr Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    15.     Private Declare Function FT_SetRts Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    16.     Private Declare Function FT_ClrRts Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    17.     Private Declare Function FT_GetModemStatus Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByRef lngModemStatus As Integer) As Integer
    18.     Private Declare Function FT_Purge Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lngMask As Integer) As Integer
    19.     Private Declare Function FT_GetStatus Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByRef lngRxBytes As Integer, ByRef lngTxBytes As Integer, ByRef lngEventsDWord As Integer) As Integer
    20.     Private Declare Function FT_GetQueueStatus Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByRef lngRxBytes As Integer) As Integer
    21.     Private Declare Function FT_GetEventStatus Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByRef lngEventsDWord As Integer) As Integer
    22.     Private Declare Function FT_SetChars Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal byEventChar As Byte, ByVal byEventCharEnabled As Byte, ByVal byErrorChar As Byte, ByVal byErrorCharEnabled As Byte) As Integer
    23.     Private Declare Function FT_SetTimeouts Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lngReadTimeout As Integer, ByVal lngWriteTimeout As Integer) As Integer
    24.     Private Declare Function FT_SetBreakOn Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    25.     Private Declare Function FT_SetBreakOff Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    26.     Private Declare Function FT_ListDevices Lib "FTD2XX.dll" (ByVal arg1 As Integer, ByVal arg2 As String, ByVal dwFlags As Integer) As Integer
    27.     Private Declare Function FT_GetNumDevices Lib "FTD2XX.dll"  Alias "FT_ListDevices"(ByRef arg1 As Integer, ByVal arg2 As String, ByVal dwFlags As Integer) As Integer
    28.     Private Declare Function FT_SetBitMode Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal mask As Byte, ByVal enable As Byte) As Integer
    29.    
    30.     ' Return codes
    31.     Const FT_OK As Short = 0
    32.     Const FT_INVALID_HANDLE As Short = 1
    33.     Const FT_DEVICE_NOT_FOUND As Short = 2
    34.     Const FT_DEVICE_NOT_OPENED As Short = 3
    35.     Const FT_IO_ERROR As Short = 4
    36.     Const FT_INSUFFICIENT_RESOURCES As Short = 5
    37.     Const FT_INVALID_PARAMETER As Short = 6
    38.     Const FT_INVALID_BAUD_RATE As Short = 7
    39.    
    40.     ' Word Lengths
    41.     Const FT_BITS_8 As Short = 8
    42.     Const FT_BITS_7 As Short = 7
    43.    
    44.     ' Stop Bits
    45.     Const FT_STOP_BITS_1 As Short = 0
    46.     Const FT_STOP_BITS_1_5 As Short = 1
    47.     Const FT_STOP_BITS_2 As Short = 2
    48.    
    49.     ' Parity
    50.     Const FT_PARITY_NONE As Short = 0
    51.     Const FT_PARITY_ODD As Short = 1
    52.     Const FT_PARITY_EVEN As Short = 2
    53.     Const FT_PARITY_MARK As Short = 3
    54.     Const FT_PARITY_SPACE As Short = 4
    55.    
    56.     ' Flow Control
    57.     Const FT_FLOW_NONE As Integer = &H0
    58.     Const FT_FLOW_RTS_CTS As Integer = &H100
    59.     Const FT_FLOW_DTR_DSR As Integer = &H200
    60.     Const FT_FLOW_XON_XOFF As Integer = &H400
    61.    
    62.     ' Purge rx and tx buffers
    63.     Const FT_PURGE_RX As Short = 1
    64.     Const FT_PURGE_TX As Short = 2
    65.    
    66.     ' Flags for FT_OpenEx
    67.     Const FT_OPEN_BY_SERIAL_NUMBER As Short = 1
    68.     Const FT_OPEN_BY_DESCRIPTION As Short = 2
    69.    
    70.     ' Flags for FT_ListDevices
    71.     Const FT_LIST_BY_NUMBER_ONLY As Integer = &H80000000
    72.     Const FT_LIST_BY_INDEX As Integer = &H40000000
    73.     Const FT_LIST_ALL As Integer = &H20000000
    74.  
    75.     Sub SetRelays(ByVal State As Integer)
    76.         Dim lngBytesWritten As Object
    77.         Dim strWriteBuffer As Object
    78.         Dim lnghandle As Object
    79.  
    80.         If FT_Open(0, lnghandle) <> FT_OK Then
    81.             Status.Text = "Error while opening"
    82.  
    83.             Exit Sub
    84.         Else
    85.             Status.Text = "Open OK"
    86.         End If
    87.  
    88.         If FT_SetBitMode(lnghandle, 255, 1) <> FT_OK Then
    89.             Status.Text = "Bit Bang Mode Error"
    90.             Exit Sub
    91.         Else
    92.             Status.Text = "Bit Bang Mode OK"
    93.         End If
    94.  
    95.         strWriteBuffer = " "
    96.         Mid(strWriteBuffer, 1, 1) = Chr(State)
    97.         lngBytesWritten = 0
    98.  
    99.         If FT_Write(lnghandle, strWriteBuffer, Len(strWriteBuffer), lngBytesWritten) <> FT_OK Then
    100.             Status.Text = "Write Failed"
    101.             Exit Sub
    102.         Else
    103.             Status.Text = "Write OK"
    104.         End If
    105.  
    106.         FT_Close(lnghandle)
    107.     End Sub
    108.    
    109.     Private Sub cmdRelay1ON_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay1ON.Click
    110.         Call SetRelays(1)
    111.     End Sub
    112.  
    113.     Private Sub cmdRelay1OFF_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay1OFF.Click
    114.         Call SetRelays(0)
    115.     End Sub
    116.    
    117.     Private Sub cmdRelay2ON_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay2ON.Click
    118.         Call SetRelays(2)
    119.     End Sub
    120.    
    121.     Private Sub cmdRelay2OFF_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay2OFF.Click
    122.         Call SetRelays(0)
    123.     End Sub
    124.    
    125.     Private Sub cmdAllRelaysON_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAllRelaysON.Click
    126.         Call SetRelays(255)
    127.     End Sub
    128.    
    129.     Private Sub cmdAllRelaysOFF_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAllRelaysOFF.Click
    130.         Call SetRelays(0)
    131.     End Sub
    132. End Class
    133.  
    But there are a few problems here:

    (1) The relay #1 doesn't even come on when pushing the button
    (2) Relay #2 works for both on and off.
    (3) Both the all on and all off work.
    (4) When i want to turn just relay 2 off without any others off then there is no code for that in there.

    So i am confused as to how to separate each relay out with that code above and just be able to turn on any number 1-4 relay on or off independently from each other! It shouldn't be this hard! I'm pretty sure its using the Bit-Bang approach here.

    Any help would be great!

    David
     
    Last edited: Oct 20, 2011
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    VB.net still offers COMM support. Been a while since I used it but I see a reference to it. Sorry, you'll have to worry out all the dotnet references.

    Post #72 seems to define an output command that works:

    Code ( (Unknown Language)):
    1.  MSComm1.Output = "x" & Chr(n1) & Chr(n2) & "//"
    where n1 and n2 are bitfields for all the relays, and that string will change all the relays to the setting you give it. Obviously the FTDI chip is set for 16 relays, all you need do is find which 4 bits are yours.
     
  3. StealthRT

    Thread Starter Active Member

    Mar 20, 2009
    223
    0
    could you give me an example of this, Ernie?

    David
     
Loading...