Wednesday, 24 July 2013

Modbus TCP Sample VB (English version)

Modbus Sample VB
This is a project for Reading and Writing Modbus TCP’s registers.
The beginning is where I am looking for Sample program of Modbus TCP’s communication. And then I found a sample project in, here:
This is a 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 can do my wish, here is:

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

11 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


  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.


  3. Hi

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


    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.


    2. Please see here for Modbus TCP with VB6 project :

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

  5. Top job - great code!!!

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

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

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


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