ral6639
Newbie level 3
I recently purchased a 8 ch sainsmart relay module and they supplied a 4 ch relay code. I have not been able to get all 8 ch to operate . Can any look over my code and help me out.
Code:
Imports FTD2XX_NET.FTDI
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox3 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox5 As System.Windows.Forms.CheckBox
Friend WithEvents CheckBox7 As System.Windows.Forms.CheckBox
Friend WithEvents TextBox4 As System.Windows.Forms.TextBox
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents Label5 As System.Windows.Forms.Label
Friend WithEvents TextBox5 As System.Windows.Forms.TextBox
Friend WithEvents Label6 As System.Windows.Forms.Label
Friend WithEvents Button3 As System.Windows.Forms.Button
Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.Button2 = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.TextBox2 = New System.Windows.Forms.TextBox()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.TextBox3 = New System.Windows.Forms.TextBox()
Me.CheckBox1 = New System.Windows.Forms.CheckBox()
Me.CheckBox3 = New System.Windows.Forms.CheckBox()
Me.CheckBox5 = New System.Windows.Forms.CheckBox()
Me.CheckBox7 = New System.Windows.Forms.CheckBox()
Me.TextBox4 = New System.Windows.Forms.TextBox()
Me.Label4 = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.TextBox5 = New System.Windows.Forms.TextBox()
Me.Label6 = New System.Windows.Forms.Label()
Me.Button3 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(328, 16)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(128, 32)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Click here to run"
'
'TextBox1
'
Me.TextBox1.BackColor = System.Drawing.SystemColors.GradientActiveCaption
Me.TextBox1.Enabled = False
Me.TextBox1.Location = New System.Drawing.Point(152, 40)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(144, 20)
Me.TextBox1.TabIndex = 1
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(320, 352)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(128, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Exit"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(16, 40)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(112, 16)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Number of Devices"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'TextBox2
'
Me.TextBox2.BackColor = System.Drawing.SystemColors.GradientActiveCaption
Me.TextBox2.Enabled = False
Me.TextBox2.Location = New System.Drawing.Point(152, 80)
Me.TextBox2.Name = "TextBox2"
Me.TextBox2.Size = New System.Drawing.Size(144, 20)
Me.TextBox2.TabIndex = 4
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(136, 23)
Me.Label2.TabIndex = 5
Me.Label2.Text = "Serial Number of Device 0"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 120)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(128, 23)
Me.Label3.TabIndex = 6
Me.Label3.Text = "Description of Device 0"
'
'TextBox3
'
Me.TextBox3.BackColor = System.Drawing.SystemColors.GradientActiveCaption
Me.TextBox3.Enabled = False
Me.TextBox3.Location = New System.Drawing.Point(152, 112)
Me.TextBox3.Name = "TextBox3"
Me.TextBox3.Size = New System.Drawing.Size(144, 20)
Me.TextBox3.TabIndex = 7
'
'CheckBox1
'
Me.CheckBox1.AutoSize = True
Me.CheckBox1.Location = New System.Drawing.Point(152, 209)
Me.CheckBox1.Name = "CheckBox1"
Me.CheckBox1.Size = New System.Drawing.Size(15, 14)
Me.CheckBox1.TabIndex = 8
Me.CheckBox1.UseVisualStyleBackColor = True
'
'CheckBox3
'
Me.CheckBox3.AutoSize = True
Me.CheckBox3.Location = New System.Drawing.Point(194, 209)
Me.CheckBox3.Name = "CheckBox3"
Me.CheckBox3.Size = New System.Drawing.Size(15, 14)
Me.CheckBox3.TabIndex = 10
Me.CheckBox3.UseVisualStyleBackColor = True
'
'CheckBox5
'
Me.CheckBox5.AutoSize = True
Me.CheckBox5.Location = New System.Drawing.Point(236, 209)
Me.CheckBox5.Name = "CheckBox5"
Me.CheckBox5.Size = New System.Drawing.Size(15, 14)
Me.CheckBox5.TabIndex = 12
Me.CheckBox5.UseVisualStyleBackColor = True
'
'CheckBox7
'
Me.CheckBox7.AutoSize = True
Me.CheckBox7.Location = New System.Drawing.Point(278, 209)
Me.CheckBox7.Name = "CheckBox7"
Me.CheckBox7.Size = New System.Drawing.Size(15, 14)
Me.CheckBox7.TabIndex = 14
Me.CheckBox7.UseVisualStyleBackColor = True
'
'TextBox4
'
Me.TextBox4.BackColor = System.Drawing.SystemColors.GradientActiveCaption
Me.TextBox4.Enabled = False
Me.TextBox4.Location = New System.Drawing.Point(152, 151)
Me.TextBox4.Name = "TextBox4"
Me.TextBox4.Size = New System.Drawing.Size(144, 20)
Me.TextBox4.TabIndex = 17
'
'Label4
'
Me.Label4.Location = New System.Drawing.Point(16, 159)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(128, 23)
Me.Label4.TabIndex = 16
Me.Label4.Text = "Byte written"
'
'Label5
'
Me.Label5.Location = New System.Drawing.Point(16, 209)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(128, 23)
Me.Label5.TabIndex = 18
Me.Label5.Text = "Relays"
'
'TextBox5
'
Me.TextBox5.Location = New System.Drawing.Point(150, 247)
Me.TextBox5.Name = "TextBox5"
Me.TextBox5.Size = New System.Drawing.Size(142, 20)
Me.TextBox5.TabIndex = 19
'
'Label6
'
Me.Label6.AutoSize = True
Me.Label6.Location = New System.Drawing.Point(16, 250)
Me.Label6.Name = "Label6"
Me.Label6.Size = New System.Drawing.Size(36, 13)
Me.Label6.TabIndex = 20
Me.Label6.Text = "Inputs"
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(328, 247)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(127, 20)
Me.Button3.TabIndex = 21
Me.Button3.Text = "Read"
Me.Button3.UseVisualStyleBackColor = True
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(472, 405)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Label6)
Me.Controls.Add(Me.TextBox5)
Me.Controls.Add(Me.Label5)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.CheckBox7)
Me.Controls.Add(Me.CheckBox5)
Me.Controls.Add(Me.CheckBox3)
Me.Controls.Add(Me.CheckBox1)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
#End Region
'Based on D2XX_Unit_NET example code found on FTDI website
'Modified to reference the FTD2XX_NET.dll in a .NET way
'Brian Hughes 01 Dec 2011
Dim DeviceCount As Integer
Dim DeviceIndex As Integer
Dim TempDevString As String
Dim BytesWritten As UInteger
Dim TempStringData As String
Dim BytesRead As Integer
Dim Board As FTD2XX_NET.FTDI
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim arrBytes(1) As Byte
Dim BitPattern As Byte = 0
If CheckBox7.Checked Then
BitPattern = BitPattern Or 2
End If
If CheckBox5.Checked Then
BitPattern = BitPattern Or 8
End If
If CheckBox3.Checked Then
BitPattern = BitPattern Or 32
End If
If CheckBox1.Checked Then
BitPattern = BitPattern Or 128
End If
TextBox4.Text = BitPattern.ToString
arrBytes(0) = BitPattern
BytesWritten = 0
' Set Relays on
FT_Status = Board.Write(arrBytes, 1, BytesWritten)
If FT_Status <> FT_OK Then
Exit Sub
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
' Close device
FT_Status = Board.Close()
If FT_Status <> FT_OK Then
Exit Sub
End If
Me.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Board = New FTD2XX_NET.FTDI()
' Get the number of devices attached
FT_Status = Board.GetNumberOfDevices(DeviceCount)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Display device count on form
TextBox1.Text = DeviceCount.ToString
FT_Status = Board.OpenByIndex(0)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Get serial number of device with index 0
FT_Serial_Number = ""
FT_Status = Board.GetSerialNumber(FT_Serial_Number)
If FT_Status <> FT_OK Then
Exit Sub
End If
' Display serial number on form
TextBox2.Text = FT_Serial_Number
' Get description of device with index 0
FT_Description = ""
Board.GetDescription(FT_Description)
If FT_Status <> FT_OK Then
Exit Sub
End If
TextBox3.Text = FT_Description
' Reset device
FT_Status = Board.ResetDevice()
If FT_Status <> FT_OK Then
Exit Sub
End If
' Set Baud Rate
FT_Status = Board.SetBaudRate(FT_BAUD_921600)
If FT_Status <> FT_OK Then
Exit Sub
End If
Dim mask As Byte
mask = 170
' Set Bit Bang
FT_Status = Board.SetBitMode(mask, FT_BIT_MODES.FT_BIT_MODE_ASYNC_BITBANG)
If FT_Status <> FT_OK Then
Exit Sub
End If
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim arrByte As Byte
Dim BitPattern As Byte = 0
FT_Status = Board.GetPinStates(arrByte)
If FT_Status <> FT_OK Then
Exit Sub
End If
TextBox5.Text = arrByte
End Sub
End Class