VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
Go Back  Xtreme Visual Basic Talk > > > VB.net MSComm port USB 4 port relay


Reply
 
Thread Tools Display Modes
  #1  
Old 10-20-2011, 11:49 AM
StealthRT's Avatar
StealthRT StealthRT is offline
Contributor
 
Join Date: Aug 2002
Posts: 785
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:
Quote:
"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
Reply With Quote
  #2  
Old 11-02-2011, 11:40 AM
passel's Avatar
passelVB.net MSComm port USB 4 port relay passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 8,026
Default

The Bit Bang approach looks like it is sending a single byte, where each bit represents the state of a relay.
If there is no way to read the current state of the relays, then you should maintain a variable where you track that.

This is all educated guess work, since I haven't looked at any of the links you provided, but I'm presuming for four relays, sending different combinations of the bottom four bits (values 0 to 15) will set and clear the various relays.
1 = Set relay 0 (will clear relays 1,2 and 3)
2 = Set relay 1 (will clear relays 0, 2 and 3)
4 = Set relay 2 (will clear relays 0, 1 and 3)
8 = Set relay 3 (will clear relays 0, 1 and 2)

If you have a variable (lets call it state) and you have constants set up for each relay (set to 1, 2, 4 and 8), you would OR values to the the state variable to close the relay, and AND the inverse of the value to clear one of the relays.
You could set up "Off" constants or enumerations, if you wanted.
Refer to OnErr0r's Bits, Bytes and Bases Tutorial if you are unfamiliar with bit manipulation.

Assuming state is the variable, using literal values to set or clear the third relay could be done like this, but using an enumeration or constants would be better than using literal numbers.

Make sure the variable is a byte, since the code converts it to a single character to send out the comm port.

This is VB6 code, but .Net code would be pretty much the same, but I'm in a hurry so don't have time to bring up .Net to verify my syntax. This only shows the bit manipulation, you would of course be calling your routine to pass the state out the comm port to actually effect the relays.

Code:
Private Sub Command1_Click()
  Dim state As Byte
  
  state = state Or 4           'Turn on 3rd relay
  Debug.Print state            '  note value is now 4
  state = state Or 2           'Turn on 2nd relay too
  Debug.Print state            '  note value is now 6
  state = state And (Not 4)    'Turn off 3rd relay
  Debug.Print state            '  note value is now 2
End Sub
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
 
VB.net MSComm port USB 4 port relay
VB.net MSComm port USB 4 port relay
 
-->