CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2008
    Posts
    114

    Exclamation VB.net MSComm port USB 4 port relay

    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:
    "01+//" - Relay 1 is switched ON
    "01-//" - Relay 1 is switched OFF
    "02+//" - Relay 2 is switched ON
    "02-//" - Relay 2 is switched OFF
    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:
    If MSComm.PortOpen = True Then
        MSComm.PortOpen = False
    End If
    
    MSComm.CommPort = 6
    MSComm.Settings = "9600,N,8,1"
    MSComm.InputLen = 0
    MSComm.RThreshold = 1
    MSComm.SThreshold = 0
    MSComm.PortOpen = True
    
    'Turn relay #1 ON
    MSComm.Output = "01+//"
    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:
    Friend Class USB_FTDI_TEST
    	Inherits System.Windows.Forms.Form
    	Private Declare Function FT_Open Lib "FTD2XX.dll" (ByVal intDeviceNumber As Short, ByRef lnghandle As Integer) As Integer
    	Private Declare Function FT_OpenEx Lib "FTD2XX.dll" (ByVal arg1 As String, ByVal arg2 As Integer, ByRef lnghandle As Integer) As Integer
    	Private Declare Function FT_Close Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	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
    	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
    	Private Declare Function FT_SetBaudRate Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lngBaudRate As Integer) As Integer
    	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
    	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
    	Private Declare Function FT_ResetDevice Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	Private Declare Function FT_SetDtr Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	Private Declare Function FT_ClrDtr Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	Private Declare Function FT_SetRts Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	Private Declare Function FT_ClrRts Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	Private Declare Function FT_GetModemStatus Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByRef lngModemStatus As Integer) As Integer
    	Private Declare Function FT_Purge Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lngMask As Integer) As Integer
    	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
    	Private Declare Function FT_GetQueueStatus Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByRef lngRxBytes As Integer) As Integer
    	Private Declare Function FT_GetEventStatus Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByRef lngEventsDWord As Integer) As Integer
    	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
    	Private Declare Function FT_SetTimeouts Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal lngReadTimeout As Integer, ByVal lngWriteTimeout As Integer) As Integer
    	Private Declare Function FT_SetBreakOn Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	Private Declare Function FT_SetBreakOff Lib "FTD2XX.dll" (ByVal lnghandle As Integer) As Integer
    	Private Declare Function FT_ListDevices Lib "FTD2XX.dll" (ByVal arg1 As Integer, ByVal arg2 As String, ByVal dwFlags As Integer) As Integer
    	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
    	Private Declare Function FT_SetBitMode Lib "FTD2XX.dll" (ByVal lnghandle As Integer, ByVal mask As Byte, ByVal enable As Byte) As Integer
    	
    	' Return codes
    	Const FT_OK As Short = 0
    	Const FT_INVALID_HANDLE As Short = 1
    	Const FT_DEVICE_NOT_FOUND As Short = 2
    	Const FT_DEVICE_NOT_OPENED As Short = 3
    	Const FT_IO_ERROR As Short = 4
    	Const FT_INSUFFICIENT_RESOURCES As Short = 5
    	Const FT_INVALID_PARAMETER As Short = 6
        Const FT_INVALID_BAUD_RATE As Short = 7
    	
    	' Word Lengths
    	Const FT_BITS_8 As Short = 8
    	Const FT_BITS_7 As Short = 7
    	
    	' Stop Bits
    	Const FT_STOP_BITS_1 As Short = 0
    	Const FT_STOP_BITS_1_5 As Short = 1
    	Const FT_STOP_BITS_2 As Short = 2
    	
    	' Parity
    	Const FT_PARITY_NONE As Short = 0
    	Const FT_PARITY_ODD As Short = 1
    	Const FT_PARITY_EVEN As Short = 2
    	Const FT_PARITY_MARK As Short = 3
    	Const FT_PARITY_SPACE As Short = 4
    	
    	' Flow Control
    	Const FT_FLOW_NONE As Integer = &H0
    	Const FT_FLOW_RTS_CTS As Integer = &H100
    	Const FT_FLOW_DTR_DSR As Integer = &H200
    	Const FT_FLOW_XON_XOFF As Integer = &H400
    	
    	' Purge rx and tx buffers
    	Const FT_PURGE_RX As Short = 1
    	Const FT_PURGE_TX As Short = 2
    	
    	' Flags for FT_OpenEx
    	Const FT_OPEN_BY_SERIAL_NUMBER As Short = 1
    	Const FT_OPEN_BY_DESCRIPTION As Short = 2
    	
    	' Flags for FT_ListDevices
    	Const FT_LIST_BY_NUMBER_ONLY As Integer = &H80000000
    	Const FT_LIST_BY_INDEX As Integer = &H40000000
    	Const FT_LIST_ALL As Integer = &H20000000
    
        Sub SetRelays(ByVal State As Integer)
            Dim lngBytesWritten As Object
            Dim strWriteBuffer As Object
            Dim lnghandle As Object
    
            If FT_Open(0, lnghandle) <> FT_OK Then
                Status.Text = "Error while opening"
    
                Exit Sub
            Else
                Status.Text = "Open OK"
            End If
    
            If FT_SetBitMode(lnghandle, 255, 1) <> FT_OK Then
                Status.Text = "Bit Bang Mode Error"
                Exit Sub
            Else
                Status.Text = "Bit Bang Mode OK"
            End If
    
            strWriteBuffer = " "
            Mid(strWriteBuffer, 1, 1) = Chr(State)
            lngBytesWritten = 0
    
            If FT_Write(lnghandle, strWriteBuffer, Len(strWriteBuffer), lngBytesWritten) <> FT_OK Then
                Status.Text = "Write Failed"
                Exit Sub
            Else
                Status.Text = "Write OK"
            End If
    
            FT_Close(lnghandle)
        End Sub
    	
        Private Sub cmdRelay1ON_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay1ON.Click
            Call SetRelays(1)
        End Sub
    
        Private Sub cmdRelay1OFF_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay1OFF.Click
            Call SetRelays(0)
        End Sub
    	
        Private Sub cmdRelay2ON_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay2ON.Click
            Call SetRelays(2)
        End Sub
    	
        Private Sub cmdRelay2OFF_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRelay2OFF.Click
            Call SetRelays(0)
        End Sub
    	
        Private Sub cmdAllRelaysON_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAllRelaysON.Click
            Call SetRelays(255)
        End Sub
    	
        Private Sub cmdAllRelaysOFF_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAllRelaysOFF.Click
            Call SetRelays(0)
        End Sub
    End Class
    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

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB.net MSComm port USB 4 port relay

    VB.Net 2005 and later has a serial port class that allows you to work with it much the same as you were able to do with MSComm.
    Always use [code][/code] tags when posting code.

  3. #3
    Join Date
    Aug 2008
    Posts
    114

    Re: VB.net MSComm port USB 4 port relay

    That's true but like I said the code works but doesn't turn on the correct ports.

    David

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: VB.net MSComm port USB 4 port relay

    Well I am not sure I understand, if you try to turn on Port 5 and port 6 turns on instead then you know how to turn on port 6. Or is it not turning on the ports at all?

    Edit: Have you tried to send commands via hyperterminal to see what happens? Do you have documentation as to what commands the chip uses?
    Last edited by DataMiser; October 20th, 2011 at 02:29 PM.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Oct 2011
    Posts
    25

    Re: VB.net MSComm port USB 4 port relay

    I agree with DataMiser. You should try the commands in Hyper Terminal and see what it's doing. If everything works as expected in Hyper Terminal then it's time to debug your code. Otherwise, you could be spending time debugging forever...

  6. #6
    Join Date
    Aug 2008
    Posts
    114

    Re: VB.net MSComm port USB 4 port relay

    How would you do that in hyper terminal since windows 7 does not come with it?

    David

  7. #7
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: VB.net MSComm port USB 4 port relay

    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

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

Featured