0

Demonstrates iTach IP2CC Ethernet (IP) to contact closure (relay) Visual Basic 2010 Express

 

Public Class Form1

'Demonstrates iTach IP2CC Ethernet (IP) to contact closure (relay)

'This Visual Basic 2010 Express (free from Microsoft) example needs a

'Form1 with a Button1 and ListBox1 Object on it

'Copy this text to the Public Class of Form1

'Replace the Host IP address in the IP identifed in iHelp

 

Public tcpClient As New System.Net.Sockets.TcpClient()

Public theCount

Public Host As String = "126.0.0.91" 'Default 192.168.1.70

Public Hostport As Long= 4998

Public strResponse

 

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

' One time opening of Host/Port upon loading Form1

Try

tcpClient.Connect(Host, Hostport)' Connects to the host/port

Catch

MsgBox("Can't find device at IP " & Host & " Port "& Hostport)

End Try

End Sub

 

Function SendTextToIP(ByVal strCommand As String) As String

' Sends and receives the text to the host/port via tcp, adds a carriage return

Try

Dim networkStream As Net.Sockets.NetworkStream = tcpClient.GetStream()

Dim strResponse As String = vbCr

strResponse ="No response"

Dim sendBytes As [Byte]() = System.Text.Encoding.ASCII.GetBytes(strCommand & Chr(13))

networkStream.Write(sendBytes, 0, sendBytes.Length)

Dim bytes(tcpClient.ReceiveBufferSize) As Byte

networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))

Dim returndata As String = System.Text.Encoding.ASCII.GetString(bytes)

strResponse = "Device responded: " & CStr(returndata)

Return strResponse

Catch

Return "Error: Cannot write data to the device"

End Try

End Function

 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Button 1 action cycles thru the 3 ports and puts the response in ListBox1

Dim thePort = 0

Dim TheState = 0

Dim ResponseFromITach

Select Case theCount

Case 0

thePort = 1

TheState = 1

Case 1

thePort = 2

TheState = 1

Case 2

thePort = 3

TheState = 1

Case 3

thePort = 1

TheState = 0

Case 4

thePort = 2

TheState = 0

Case 5

thePort = 3

TheState = 0

End Select

 

theCount += 1

If theCount > 5 Then theCount = 0

 

ResponseFromITach = SendTextToIP("setstate,1:" & thePort & "," & TheState)

ListBox1.Items.Add(ResponseFromITach)

End Sub

End Class

 




iTach.doc

0 comments

Please sign in to leave a comment.