Visual Basic 6 (VB6 atau VB Classic) memang sudah tua. Microsoft sudah mengeluarkan software programming yang jauh lebih canggih. Terakhir adalah Microsoft Visual Studio 2013. VB6 sudah tidak di-support lagi oleh mereka. Tapi ternyata masih banyak yang menggunakan VB6, baik untuk sekedar belajar pemrograman, atau untuk bekerja membuat aplikasi.
Visual Basic 6 (VB6 or VB classic) is old, Microsoft has release more advance programming software, the last is Microsoft Visual Studio 2013. Microsoft do not support VB6 now. But, there are a lot of people whom still using VB6 for programming, for learn or work.
Berikut ini adalah tutorial bagaimana membuat aplikasi Modbus TCP client dengan VB6.
The following tutorial describe hoe to make VB6 application communicate with Modbus TCP.
1. Buka VB6.
open VB6.
2. Buat project baru, Standard EXE.
Create new project, Standard EXE
3. Buatlah beberapa objek berikut pada Form:
Add objects on form like this:
- txtIP : textbox untuk input IP address dari device Modbus TCP. "127.0.0.1" adalah IP komputer lokal, kita akan pakai ini karena kita akan pakai Modbus Simulator.
txtIP: textbox for input IP address of device Modbus TCP. "127.0.0.1" is local computer, we will use this because we will use Modbus Simulator.
- tombol: cmdConnect, cmdDisconnect, cmdRead, cmdWrite
buttons: cmdConnect, cmdDisconnect, cmdRead, and cmdWrite
- txtStartReg : textbox untuk input alamat pertama dari register Modbus yang akan dibaca/ditulis.
txtStartReg: textbox for input the first register address of Modbus whic will be read/write.
- txtLengthReg : textbox untuk input jumlah register yang akan dibaca/ditulis. Pada tutorial ini kami batasi 20, sesuai dengan jumlah textbox untuk datanya, silahkan diedit sendiri apabila kurang.
txtLengthReg: textbox for input the length of registers which will be read/write. In this tutorial the length is limited until 20 as much as textbox for register data. You can change this if need more.
- txtReg() : textbox yang berindeks, dari 0 sampai 19, berfungsi untuk menampilkan nilai dari register.
txtReg(): indexed textbox, from 0 to 19, for displaying value of registers.
- komponen Winsock1 : komponen untuk koneksi ke device Modbus TCP. Isi properties RemotePort dengan nilai 502. Jika komponen Winsock pada toolbox tidak ada, klik kanan pada area toolbox lalu klik Components. Carilah komponen Microsoft Winsock Control.
Winsock component: for connect to Modbus TCP device. Set RemotePort property with 502. If the component is not on toolbox, right-click on toolbox area then click Components. Find Microsoft Winsock Control.
- TimerTO : timer untuk Time Out pembacaan atau penulisan ke Modbus, pada contoh ini kita set properties Interval -nya dengan 1000 (1 detik).
TimerTO: timer for reading or writing Time Out, in this sample we set Interval property to 1000 (1 second).
- lblStatus : label untuk status dari proses.
lblStatus: label for displaying state of process.
4. Tulis script berikut pada jendela Code:
Write the following script to Code window:
'/// begining of script ///
Dim MbusQuery(11) As Byte
Dim MbusByteArray(255) As Byte
Dim MbusResponse As String
Dim ModbusTimeOut As Integer
Dim MbusRead As Boolean
Dim MbusWrite As Boolean
Dim ModbusWait As Boolean
Private Sub cmdConnect_Click()
Me.MousePointer = vbHourglass
Winsock1.RemoteHost = txtIP.Text
Winsock1.Connect
Dim StartTime
StartTime = Timer
Do While ((Timer < StartTime + 2) And (Winsock1.State <> 7))
DoEvents
Loop
If (Winsock1.State = 7) Then
lblStatus.Caption = "Connected"
lblStatus.BackColor = vbGreen
cmdConnect.Enabled = False
cmdDisconnect.Enabled = True
Else
lblStatus.Caption = "Can't connect to " + txtIP.Text
lblStatus.BackColor = vbYellow
End If
Me.MousePointer = vbDefault
End Sub
Private Sub cmdDisconnect_Click()
Me.MousePointer = vbHourglass
If (Winsock1.State <> sckClosed) Then
Winsock1.Close
End If
Dim StartTime
StartTime = Timer
Do While ((Timer < StartTime + 2) And (Winsock1.State <> sckClosed))
DoEvents
Loop
If (Winsock1.State = sckClosed) Then
lblStatus.Caption = "Disconnected"
lblStatus.BackColor = vbRed
cmdConnect.Enabled = True
cmdDisconnect.Enabled = False
Else
lblStatus.Caption = "Error disconnect!"
lblStatus.BackColor = vbYellow
End If
Me.MousePointer = vbDefault
End Sub
Private Sub cmdRead_Click()
'cek if length is more than 20
If Val(txtLengthReg.Text) > 20 Then
MsgBox "Can not read more than 20 registers!"
Exit Sub
End If
Dim StartLow As Byte
Dim StartHigh As Byte
Dim LengthLow As Byte
Dim LengthHigh As Byte
If (Winsock1.State = 7) Then
StartLow = Val(txtStartReg.Text) Mod 256
StartHigh = Val(txtStartReg.Text) \ 256
LengthLow = Val(txtLengthReg.Text) Mod 256
LengthHigh = Val(txtLengthReg.Text) \ 256
MbusQuery(0) = 0
MbusQuery(1) = 0
MbusQuery(2) = 0
MbusQuery(3) = 0
MbusQuery(4) = 0
MbusQuery(5) = 6
MbusQuery(6) = 1
MbusQuery(7) = 3
MbusQuery(8) = StartHigh
MbusQuery(9) = StartLow
MbusQuery(10) = LengthHigh
MbusQuery(11) = LengthLow
MbusRead = True
MbusWrite = False
Winsock1.SendData MbusQuery
ModbusWait = True
ModbusTimeOut = 0
TimerTO.Enabled = True
Else
MsgBox ("Device not connected via TCP/IP!")
End If
End Sub
Private Sub cmdWrite_Click()
Dim MbusWriteCommand As String
Dim StartLow As Byte
Dim StartHigh As Byte
Dim ByteLow As Byte
Dim ByteHigh As Byte
Dim i As Integer
If (Winsock1.State = 7) Then
StartLow = Val(txtStartReg.Text) Mod 256
StartHigh = Val(txtStartReg.Text) \ 256
LengthLow = Val(txtLengthReg.Text) Mod 256
LengthHigh = Val(txtLengthReg.Text) \ 256
MbusWriteQuery = Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(0) + Chr(7 + 2 * Val(txtLengthReg.Text)) + Chr(1) + Chr(16) + Chr(StartHigh) + Chr(StartLow) + Chr(0) + Chr(Val(txtLengthReg.Text)) + Chr(2 * Val(txtLengthReg.Text))
For i = 0 To Val(txtLengthReg.Text) - 1
ByteLow = Val(txtReg(i).Text) Mod 256
ByteHigh = Val(txtReg(i).Text) \ 256
MbusWriteQuery = MbusWriteQuery + Chr(ByteHigh) + Chr(ByteLow)
Next i
MbusRead = False
MbusWrite = True
Winsock1.SendData MbusWriteQuery
ModbusWait = True
ModbusTimeOut = 0
TimerTO.Enabled = True
Else
MsgBox ("Device not connected via TCP/IP!")
End If
End Sub
Private Sub TimerTO_Timer()
ModbusTimeOut = ModbusTimeOut + 1
If ModbusTimeOut > 2 Then
ModbusWait = False
ModbusTimeOut = 0
lblStatus.Caption = "Modbus Time Out!"
lblStatus.BackColor = vbYellow
TimerTO.Enabled = False
End If
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim bData As Byte
Dim j As Byte
For i = 1 To bytesTotal
Winsock1.GetData bData
MbusByteArray(i) = bData
Next
j = 0
If MbusRead Then
For i = 10 To MbusByteArray(9) + 9 Step 2
txtReg(j).Text = (MbusByteArray(i) * 256) + MbusByteArray(i + 1)
j = j + 1
Next i
lblStatus.Caption = "Registers read"
lblStatus.BackColor = vbGreen
For k = j To 19
txtReg(k).Text = ""
Next k
ModbusWait = False
ModbusTimeOut = 0
TimerTO.Enabled = False
ElseIf MbusWrite Then
If (MbusByteArray(8) = 16) And (MbusByteArray(12) = Val(txtLengthReg.Text)) Then
lblStatus.Caption = "Registers written"
lblStatus.BackColor = vbGreen
ModbusWait = False
ModbusTimeOut = 0
TimerTO.Enabled = False
Else
lblStatus.Caption = "Error writting registers"
lblStatus.BackColor = vbYellow
End If
End If
End Sub
'/// end of script ///
5. Jalankan program.
Run the program.
6. Buka Modbus Simulator (Mod RSSim, baca artikelnya
disini)
Open Modbus Simulator (Mod RSSim, read the article here)
7. Tekan tombol Connect.
click on Connect button.
8. Setelah berhasil konek (tertulis di lable status: "Connected"), tentukan Start Address yang akan dibaca, kita coba dengan 0, dan Length -nya 20.
After successfully connected (showed in label status: "Connected"), set Start Address which will be read, we try with 0, and the Length is 20.
9. Tekan tombol Read. Jika pembacaan berhasil maka nilai register akan ditampilkan pada textbox txtReg() dan lable status tertulis "Registers read".
Click on Read button. If this works, the values of Holding registers will displayed on textbox txtReg() and the label status shows "Registers read".
10. Cobalah mengganti nilai pada txtReg(), misal txtReg(0) kita isi nilai 1 dan txtReg(1) kita isi dengan 2.
Try to change the value in txtReg(), in example txtReg(0) we set to 1 and txtReg(1) set to 2.
12. Tekan tombol Write. Jika penulisan berhasil maka tertulis di label status "Registers written", dan nilai di Modbus Simulator akan berubah.
Click on Write button. If this works, the label status shows "Registers written", and the valus on Modbus Simulator will change.
Catatan: setelah sekitar 2 menit, jika tidak terjadi Read atau Write ke Modbus, maka secara otomatis device Modbus akan mematikan koneksi. Jika ini terjadi, lakukan Disconnect lalu Connect lagi.
Note: after 2 minutes, if no Read or Write request to Modbus, the connection will automatically down. if this happen, do Disconnect and the Connect again.
Silahkan download source projek ini
disini.
The source code of this project can be downloaded here.
Sekian tutorial dari kami. Semoga bermanfaat dan selamat mencoba..
======================
Update 24 Februari 2014
Modbus TCP VB6 Winsock Version 2
Berikut adalah pengembangan dari projek Modbus TCP VB6 Winsock. Pada veri ini ada beberapa penambahan dan perubahan:
1. Fitur Monitoring Realtime. dengan fitur ini, data yang ditampilkan akan update otomatis setiap 1 detik.
2. Pembacaan Coil Outputs dan Digital Inputs.
3. Maksimal data yang ditampilkan hanya 16 data, agar pas untuk pembacaan Coils dan Inputs.
4. Untuk Digital Inputs tidak bisa dilakukan Write.
Tampilannya seperti ini:
Silahkan download source projek Modbus TCP VB6 Winsock versi 2 ini
disini.
Semoga bermanfaat.