VB.net MSComm port USB 4 port relay

Thread Starter

StealthRT

Joined Mar 20, 2009
253
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:
Rich (BB 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:
Rich (BB 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
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,006
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:

Rich (BB code):
 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.
 

Thread Starter

StealthRT

Joined Mar 20, 2009
253
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:

Rich (BB code):
 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.
could you give me an example of this, Ernie?

David
 
Top