Modbus Sample VB
This is a VB.net project for Reading and Writing Modbus TCP’s registers.The beginning is where I am looking for Sample program VB.net of Modbus TCP’s communication. And then I found a sample project in CodeProject.com, here:
http://www.codeproject.com/Tips/16260/Modbus-TCP-class
This is a VB.net project for Modbus TCP’s communication built with C# language, by Stephan Stricker.
The problem is I feel comfortable with VB language. So, when I open the script, I feel a little bit confuse. It is very important to convert the script to my “mother language” because from this project I will develop a more complex application.
Then I searching for translator from C# to VB script with google, and I get a good result, a nice site DeveloperFusion.com can do my wish, here is:
http://www.developerfusion.com/tools/convert/csharp-to-vb/
The original project contain of 2 projects. I open all of that and convert with site above.
Then I make a new project with VB language. I choose .Net 2.0 framework. This new project named Modbus Sample VB.
In my new project, I just create one project, which is consist of one form and one module. I move Modbus TCP class to module ModbusTCP, and I copy all objek in frmStart and paste it on frmStart in my new project.
It amazing, the translated script is almost perfect, only a few lines error.
Picture below shows the error lines in ModuleTCP, event OnReceive, on the code ElseIf… I don’t know yet how to fix this line, so I change the line to only Else.
In form frmStart more line should be fixed. I remarks unused Import, and Imports ModbusTCP changed to Imports Modbus_Sample_VB.ModuleTCP.
I remark Namespace and End Namespace, and all line between declaration of data and event frmStart_Load.
In button event btnConnect also there some modification.
There is one Warning, but I think it can be ignored.
And my new project is done, Modbus TCP client built in VB language.
I try to connect to Modbus Simulator with local IP 127.0.0.1, and successfully, it can read and write registers.
Here is a preview of Modbus Simulator.
And here is a preview of Modbus Sample VB.
The executable file of this project can be downloaded here..
if you need source of this project click here..
there is a problem when trying to Read and Write address more than Integer limit (32767).
ReplyDeleteOn function CreateReadHeader and CreateWriteHeader There is a problem when convert Start Address to byte, in this line of code :
Dim _adr As Byte() = BitConverter.GetBytes(CShort(IPAddress.HostToNetworkOrder(CShort(startAddress))))
data(8) = _adr(0)
' Start address
data(9) = _adr(1)
' Start address
I solved my problem with this edited code :
'Dim _adr As Byte() = BitConverter.GetBytes(CShort(IPAddress.HostToNetworkOrder(CShort(startAddress))))
data(8) = startAddress \ 256 ' _adr(0)
' Start address
data(9) = startAddress Mod 256 ' _adr(1)
' Start address
Thanks..
i found a bug,
ReplyDeleteeverytime i connect to Modbus Server (in my case is simulator Mod RS Sim), its build double connection.
after i check the code, there is a double connect request. So I remove this one:
Connect synchronous client
other bug is when change data type. in example from Word to Byte. It is not directly change the displayed data in register.
So edit som code and this problem solved.
I convert this project to VS Express.
and the download link has been updated to latest project.
Regards.
Hi
ReplyDeleteIs this code also available in VB6? I know it's old, but it still works for most of my projects.
Chris
Hi Chris,
DeleteNo, it can't. There are many modifications needed to implement this code to VB6. I prefer build new VB6 project from scratch. Yes, VB6 is old and I agree with you, it still works for most of my jobs too. I also have experience in VB6 and Modbus. Maybe next time i will post it here.
Hadi.
Please see here for Modbus TCP with VB6 project :
Deletehttp://hadiscada.blogspot.com/2013/12/modbus-tcp-dengan-vb6.html
mantap pak... buat belajar vb.......
ReplyDeleteTop job - great code!!!
ReplyDeletehello , Can i read rtu over ip gateway? Moscad protocol
ReplyDeleteHello,
Deletesorry, i dont know about Moscad protocol.
Hi
ReplyDeleteGreat Job, I am using your code for a Vb.net project, so far so good will revert if any clarification or bugs found
thanks..
DeleteThank you very much , its very use full for me
ReplyDeletehi
ReplyDeleteVery good code, now today i face a bug in negative value there was register data value = -2 but in software word = 65534 show !
Please help me , if all of you solve that !
This comment has been removed by the author.
DeleteSorry , now that was solve
DeleteThank you
code is below :
word(x \ 2) = data(x) * 256 + data(x + 1)
Dim unsigned As UInt16 = word(x \ 2)
word(x \ 2) = CShort(Val("&H" & Hex(unsigned)))
x = x + 2
great. thanks..
DeleteHi,
ReplyDeleteI have little experience with VB.NET.
I need to do an "MBmaster.WriteSingleRegister (ID, unit, StartAddress, data)"
data from a textbox without using GetData. Example: write 1000 at address 4055. Can you help me?
Dim value As UInt16
Deletevalue = 1000
Dim values(1) As Byte
values = BitConverter.GetBytes(value)
If (BitConverter.IsLittleEndian) Then Array.Reverse(values)
MBmaster.WriteSingleRegister(ID, unit, 54, values)
first all thanks for code ,
ReplyDeletesir , i am tring to read the some values from holding reg
and tries to display it in some text box ,
i am ref your code under button read holding reg,
but to display the data on text box , i am not finding the
variable from which i can read it , like data
please help in this regard
output values i needed to display , in your code it is in data
MBmaster.ReadHoldingRegister(IDr, unitr, StartAddressr, Lengthr)
it happens there data is not result of command ,
Deleterefer the showas function
you can get it byte by byte , and also in delayed ways
i mean it is little bit dificult but not immposiable
ref below code for reading holding reg only
in showas function
If radBytes.Checked Then
If x <= data.GetUpperBound(0) Then
ctrl.Text = data(x).ToString()
tmRichTextBox5.AppendText(Convert.ToChar(data(x)))
ctrl.Visible = True
Else
ctrl.Text = ""
End If
please refer to the newer project.
DeleteThanks.
yourstm12@gmail.com
ReplyDeletenice code and thank you for sharing. i notice that Modbus_Sample_VB crashes when switching to char. so i modified it slightly as follows...
ReplyDelete[modification of ShowAs()]:
If radChar.Checked Then
'If x <= _char.GetUpperBound(0) Then
' ctrl.Text = _char(x).ToString()
' ctrl.Visible = True
'Else
' ctrl.Text = ""
'End If
If x <= data.GetUpperBound(0) Then
If (data(x) >= 32) And (data(x) <= 122) Then
ctrl.Text = "'" + Chr(data(x)).ToString() + "'"
Else
ctrl.Text = "<" + data(x).ToString + ">"
End If
ctrl.Text = ctrl.Text + " " + hex_byte(data(x))
ctrl.Visible = True
Else
ctrl.Text = ""
End If
End If
[new function added for hex display]:
Private Function hex_byte(ByVal n As Integer) As String
Dim h As String = "0x"
Dim ones As Integer = n And 15
Dim tens As Integer = (n - ones) / 16
h = h + Hex(tens) + Hex(ones)
Return h
End Function
thank you very much
DeleteSorry, but have you ever tried a real modbus connection?
ReplyDeleteIn my case it doesn't work. I can connect to the address via port 502, but I can't read anything.
i just forgot the unit-ID...
Deleteis there a way to show the values as floating?!
please see this :
Deletehttp://hadiscada.blogspot.com/2021/08/sharing-sourcecode-modbus-float.html
its in C#, but you can learn and try to convert to VB.
Untuk mengambil angka tertentu dari sebuah PLC konsep dasarnya spt apa Pak? Di manual yang saya miliki structurnya spt ini :
ReplyDeleteAddress(byte) Function Code (Byte) data (n byte) dan CRC (2 bytes). Model komunikasinya modbus TCP/IP. Ketika saya menggunakan aplikasi yang Bapak buat, terus koneksi ke IP addressnya sudah berhasil. Namun di aplikasi tsb kan kita diminta untuk memasukkan unit,start address dan size. Lalu pertanyaannya adalah bagaimana cara saya menentukan unit,start address dan size tsb Pak? Karena saya masih bingung ketika membaca manual dari PLC tsb.
sudah sy reply di artikel yg lain ya mas.
DeleteI want use this applcation to reed data from gqpr modem. I can connect with modbus tcp but ı cant read anything. what ı should do?
ReplyDeletehello, i think you read at the wrong address, please check from the datasheet of your device.
Delete