Wednesday, 24 July 2013

Modbus TCP Sample VB (English version)

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..

31 komentar:

  1. there is a problem when trying to Read and Write address more than Integer limit (32767).

    On 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..

    ReplyDelete
  2. i found a bug,
    everytime 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.

    ReplyDelete
  3. Hi

    Is this code also available in VB6? I know it's old, but it still works for most of my projects.

    Chris

    ReplyDelete
    Replies
    1. Hi Chris,

      No, 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.

      Delete
    2. Please see here for Modbus TCP with VB6 project :
      http://hadiscada.blogspot.com/2013/12/modbus-tcp-dengan-vb6.html

      Delete
  4. mantap pak... buat belajar vb.......

    ReplyDelete
  5. Top job - great code!!!

    ReplyDelete
  6. hello , Can i read rtu over ip gateway? Moscad protocol

    ReplyDelete
    Replies
    1. Hello,
      sorry, i dont know about Moscad protocol.

      Delete
  7. Hi
    Great Job, I am using your code for a Vb.net project, so far so good will revert if any clarification or bugs found

    ReplyDelete
  8. Thank you very much , its very use full for me

    ReplyDelete
  9. hi
    Very 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 !

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Sorry , now that was solve

      Thank 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

      Delete
  10. Hi,
    I 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?

    ReplyDelete
    Replies
    1. Dim value As UInt16
      value = 1000
      Dim values(1) As Byte
      values = BitConverter.GetBytes(value)
      If (BitConverter.IsLittleEndian) Then Array.Reverse(values)
      MBmaster.WriteSingleRegister(ID, unit, 54, values)

      Delete
  11. first all thanks for code ,
    sir , 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)

    ReplyDelete
    Replies
    1. it happens there data is not result of command ,
      refer 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

      Delete
    2. please refer to the newer project.
      Thanks.

      Delete
  12. nice code and thank you for sharing. i notice that Modbus_Sample_VB crashes when switching to char. so i modified it slightly as follows...

    [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

    ReplyDelete
  13. Sorry, but have you ever tried a real modbus connection?
    In my case it doesn't work. I can connect to the address via port 502, but I can't read anything.

    ReplyDelete
    Replies
    1. i just forgot the unit-ID...

      is there a way to show the values as floating?!

      Delete
    2. please see this :
      http://hadiscada.blogspot.com/2021/08/sharing-sourcecode-modbus-float.html

      its in C#, but you can learn and try to convert to VB.

      Delete
  14. Untuk mengambil angka tertentu dari sebuah PLC konsep dasarnya spt apa Pak? Di manual yang saya miliki structurnya spt ini :
    Address(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.

    ReplyDelete
  15. I want use this applcation to reed data from gqpr modem. I can connect with modbus tcp but ı cant read anything. what ı should do?

    ReplyDelete
    Replies
    1. hello, i think you read at the wrong address, please check from the datasheet of your device.

      Delete

 
Copyright © . HadiSCADA - Posts · Comments
Theme Template by BTDesigner · Powered by Blogger