Teknik Elektro LinksBarrier-Gate & Turnstile Interface, Thermostat, Relay Driver, Microcontroler Board

Contoh Program TCP Socket (VB.NET dan VB6) Untuk Mengontrol Wi-Fi Relay

WIFI-4REL adalah driver relay 4-channel yang dapat dikontrol secara wireless melalui koneksi Wi-Fi. Komunikasi yang digunakan adalah TCP. Jadi untuk mengontrol status relay (on/off) diperlukan pemrograman TCP Socket.

Lebih detil mengenai WIFI-4REL bisa Anda baca pada tulisan terkait berikut ini.

Pada tulisan ini saya berikan contoh sederhana mengontrol WIFI-4REL menggunakan Visual Basic .NET dan Visual Basic 6.

 

Contoh Program VB.NET

Kode Program WIFI-4REL VB.NET

Imports system.net.sockets
Imports System.Text

Public Class Form1
 Dim wifi4rel As New System.Net.Sockets.TcpClient


 Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
 Dim s As String = TextBox2.Text
 wifi4rel = New System.Net.Sockets.TcpClient()
 wifi4rel.Connect(s, 23)
 If wifi4rel.Connected Then
 TextBox1.Text = "Connected."
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("" & vbCr & vbLf & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 Else
 TextBox1.Text = "Not Connected."
 End If
 End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("on1" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
 wifi4rel.Close()
 TextBox1.Text = "Not Connected."
 End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("off1" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("on2" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("off2" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("on3" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("off3" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("on4" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("off4" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("aon" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub

Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
 Dim wf4r As NetworkStream = wifi4rel.GetStream
 Dim cmd As Byte() = Encoding.ASCII.GetBytes("aof" & vbCr & vbLf)
 wf4r.Write(cmd, 0, cmd.Length)
 End Sub
End Class

Mohon maaf jika programnya pakai metode “asal jalan”. Silakan dikembangkan dengan teknik yang lebih baik.

 

Contoh Program VB6

Kode Program WIFI-4REL VB6

Private Sub Command1_Click()
 Winsock1.RemoteHost = Text1.Text
 Winsock1.RemotePort = Text2.Text
 Winsock1.Connect
End Sub

Private Sub Command2_Click(Index As Integer)
 Dim cmd(10) As String
 cmd(0) = "ON1"
 cmd(1) = "OFF1"
 cmd(2) = "ON2"
 cmd(3) = "OFF2"
 cmd(4) = "ON3"
 cmd(5) = "OFF3"
 cmd(6) = "ON4"
 cmd(7) = "OFF4"
 cmd(8) = "AON"
 cmd(9) = "AOF"
 If Winsock1.SocketHandle > 0 Then
 Winsock1.SendData (cmd(Index) & vbCr & vbLf)
 End If
End Sub

Private Sub Command3_Click()
 Winsock1.Close
End Sub

 

Untuk komunikasi TCP pada VB.NET kita menggunakan class Socket dengan namespace System.Net.Sockets. Sedangkan pada VB6 kita menggunakan komponen WinSock.

Semoga bermanfaat dan selamat berkarya.

 

Add a Comment

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>