# Modbus TCP/IP

**URL:** <https://forum.xojo.com/t/modbus-tcp-ip/70219>\
**Category:** General\
**Created:** [May 14, 2022, 4:08pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219 "2022-05-14T16:08:32Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gaurav\_Joshi](https://forum.xojo.com/user_avatar/forum.xojo.com/gaurav_joshi/32/717_2.png) [@Gaurav\_Joshi](https://forum.xojo.com/u/Gaurav_Joshi)\
**Post date:** [May 14, 2022, 4:08pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/1 "2022-05-14T16:08:32Z")

</div>

Hello All,

Probably this would be 5th or 6th post asking about implementing Modbus TCP/IP with Xojo.

Pardon my understanding of Xojo as I am new to it, but I haven’t been able to find a clear explanation of implementing Modbus TCP/IP with Xojo.

I want to communicate with Digital IO using a windows app.

Can someone please help me with a plugin which I can add to the xojo and use API to communicate with the devices.

I basically want to read & write to coils & registers.

Thanks,

Gaurav Joshi

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [May 14, 2022, 4:21pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/2 "2022-05-14T16:21:53Z")

</div>

Well I don’t know what Modbus TCP/IP is, or what Digital IO is, although a quick search tells me the former is a protolcol for supervision and control of automation equipment.

What IP address will the target devices have and how do you set it? What port do they listen on? What research have you done? What documents have you studied to understand the protocol and how it applies to the devices you have? I see also there’s a Wikipedia article on Modbus.

---

<div class="post-metadata">

**Author:** ![Gaurav\_Joshi](https://forum.xojo.com/user_avatar/forum.xojo.com/gaurav_joshi/32/717_2.png) [@Gaurav\_Joshi](https://forum.xojo.com/u/Gaurav_Joshi)\
**Post date:** [May 14, 2022, 4:47pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/3 "2022-05-14T16:47:10Z")

</div>

Hello Tim,  
Thanks for your response.  
I shall try & answer some of your questions based upon my current understanding.  
Modbus TCP/IP is basically MODBUS over IP. The Digital IO is nothing but remote IO device which communicates using Ethernet port. The standard port for Modbus TCP/IP is 502. The communication needs to take place something similar to as explained in [pyModbusTCP · PyPI](https://pypi.org/project/pyModbusTCP/).  
I am not aware as to how the communication protocol works, since till date, I have used different softwares such as DaqFactory or Matlab which have inbuilt modbus TCP/IP libraries. However, I find there certain limitations within these softwares with regards to ease of use, because of which I want to use Xojo for this new project related to Factory Automation.  
If someone can develop standard plugins for Modbus TCP/IP & Modbus RTU, I am even willing to buy them, since I believe this shall help increase the capability of Xojo and thus its reach in Industrial Automation.  
Regards,  
Gaurav

---

<div class="post-metadata">

**Author:** ![Sunil\_Abraham](https://forum.xojo.com/user_avatar/forum.xojo.com/sunil_abraham/32/437_2.png) [@Sunil\_Abraham](https://forum.xojo.com/u/Sunil_Abraham)\
**Post date:** [May 14, 2022, 4:50pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/4 "2022-05-14T16:50:13Z")

</div>

Hi Gaurav, I am done programming in Xojo using modbus tcp/ip. If interested PM me for more details.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [May 15, 2022, 3:07pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/5 "2022-05-15T15:07:27Z")

</div>

> [@Sunil\_Abraham](#):
>
> If interested PM me for more details.

That’s not very helpful to other forum members who might also be interested in more details ☹

---

<div class="post-metadata">

**Author:** ![Frank\_Bicknell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@Frank\_Bicknell](https://forum.xojo.com/u/Frank_Bicknell)\
**Post date:** [May 15, 2022, 4:09pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/6 "2022-05-15T16:09:45Z")

</div>

Perhaps there is a different way to approach it.  
I have tried to understand modbus but can’t quite get my head around it. It also seems like it is an outdated standard. With the speed of tcp/ip communications there are many methods to transfer data. I wrote my own building automation software using xojo and automation direct arduino compatible plc’s. I use xml on both client and server and it works seamlessly not to mention a lot easier to understand and maintain.  
Here is the write up on my project.

> **[Open PLCs Enhance Building Automation](https://www.isa.org/intech-home/2022/april-2022/features/open-plcs-enhance-building-automation)**
>
> ISA

---

<div class="post-metadata">

**Author:** ![Sunil\_Abraham](https://forum.xojo.com/user_avatar/forum.xojo.com/sunil_abraham/32/437_2.png) [@Sunil\_Abraham](https://forum.xojo.com/u/Sunil_Abraham)\
**Post date:** [May 15, 2022, 4:10pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/7 "2022-05-15T16:10:19Z")

</div>

Its because its a paid module.

---

<div class="post-metadata">

**Author:** ![Dennis\_Hoskins](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/bb73d2/32.png) [@Dennis\_Hoskins](https://forum.xojo.com/u/Dennis_Hoskins)\
**Post date:** [May 15, 2022, 7:13pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/8 "2022-05-15T19:13:12Z")

</div>

I have written a wrapper for an open source library for industrial control protocols including Modbus TCP/IP. See this post for more information:

> [@Modbus TCP](https://forum.xojo.com/t/modbus-tcp/66610/2):
>
> Noel, I have used a C library on Github that provides support for Modbus TCP and EtherNet/IP. I have not used the Modbus TCP portion but the EtherNet/IP works very well with Rockwell ControlLogix PLCs. The Github URL is: [https://github.com/libplctag/libplctag](https://github.com/libplctag/libplctag) I’ve written a Xojo wrapper module for the library. You can get it at: [libplctag](https://drive.google.com/file/d/1Y6rr__AWo-14YXHDPWGDefx71pcUGsVK/view?usp=sharing) The wrapper is written for version 2.3 but it should work for the current 2.4 version. I usually put the dll file in the same folder as the executable. D…

Dennis

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [May 16, 2022, 12:55pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/9 "2022-05-16T12:55:18Z")

</div>

> [@Frank\_Bicknell](#):
>
> seems like it is an outdated standard

Maybe, but very firmly entrenched in many fields and not going away anytime soon I think.

---

<div class="post-metadata">

**Author:** ![Nicholas\_Loveday](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/f1d935/32.png) [@Nicholas\_Loveday](https://forum.xojo.com/u/Nicholas_Loveday)\
**Post date:** [May 16, 2022, 1:29pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/10 "2022-05-16T13:29:38Z")

</div>

There is another more secure standard to replace MODBUS but the sheer volume of legacy hardware out there means MODBUS will be around for a very very long time yet.

Probably longer than Xojo, frankly.

NB if you don’t know what an industrial PLC is for, think coal or nuclear power stations, steelworks, oil and gas rigs, mines. Trains, process plants, manufacturing facilities for food, fertiliser plants, automatic bagging and pallet machines… Things that tend to go “bang” in a big way in milliseconds.

Particularly safety systems where human life is at risk if there are hazardous undetected failure modes.

NB if what you are playing with is safety-related, ie someone could be harmed if your solution screws up, do not use Xojo, not even for supervisory monitoring and especially not for control. Please obtain and read the relevant standards in your country concerning industrial safety and programmable devices. In UK or Europe EN50126, EN50128 and EN50129.

Aduino, Pi, PIC and similar are tinker toys in comparison.

---

<div class="post-metadata">

**Author:** ![Frank\_Bicknell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/f/e19adc/32.png) [@Frank\_Bicknell](https://forum.xojo.com/u/Frank_Bicknell)\
**Post date:** [May 16, 2022, 2:40pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/11 "2022-05-16T14:40:23Z")

</div>

> [@Nicholas\_Loveday](#):
>
> Aduino, Pi, PIC and similar are tinker toys in comparison

That is your opinion. Maybe you don’t know what makes a PLC go. They all have Micro-Processors and are ‘programmed’. The devices from Automation Direct are Industrial. Because they are programmed with the Arduino implementation of C++ does not make them a ‘tinker toy’.They are just as industrial and robust as any other brand available and they meet all necessary standards. I have had a lot of experience with many brands of PLC’s they all do the same thing. The P1AM line is just more versatile. If you know what you are doing with Xojo it is as good as any other computer language.

And another thing Modbus is not really a standard because almost all of the manufacturers have their own implementation and it is very obscure.

---

<div class="post-metadata">

**Author:** ![vondes\_vondes](https://forum.xojo.com/letter_avatar_proxy/v4/letter/v/e9bcb4/32.png) [@vondes\_vondes](https://forum.xojo.com/u/vondes_vondes)\
**Post date:** [December 6, 2023, 8:50pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/12 "2023-12-06T20:50:18Z")

</div>

Xojo supports socket programming, and you can use it to establish a TCP/IP connection to your Modbus device. Use the TCPSocket class in Xojo to create a socket connection. Modbus has a well-defined protocol. You’ll need to implement the Modbus protocol to read and write coils and registers. Refer to the Modbus specification (Modbus Application Protocol Specification V1.1b) for details on how to structure Modbus messages. Using the socket connection, send Modbus messages to the device and receive responses. Parse the responses according to the Modbus protocol.

---

<div class="post-metadata">

**Author:** ![Gaurav\_Joshi](https://forum.xojo.com/user_avatar/forum.xojo.com/gaurav_joshi/32/717_2.png) [@Gaurav\_Joshi](https://forum.xojo.com/u/Gaurav_Joshi)\
**Post date:** [December 7, 2023, 2:35am UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/13 "2023-12-07T02:35:11Z")

</div>

Hello,  
Thanks for the inputs. I shall definitely try creating a TCP Socket Class and then write instructions to initially read and then write to a connection.  
I shall try if possible to write a short code as well for everyone to refer, once i succeed with a test connection.  
Regards,  
Gaurav

---

<div class="post-metadata">

**Author:** ![Joseph\_E](https://forum.xojo.com/user_avatar/forum.xojo.com/joseph_e/32/613_2.png) [@Joseph\_E](https://forum.xojo.com/u/Joseph_E)\
**Post date:** [December 7, 2023, 5:25pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/14 "2023-12-07T17:25:15Z")

</div>

Arduino now has an industrial PLC/Controller available in their PRO offerings, one of which supports RS485:

[Arduino Opta](https://www.arduino.cc/pro/hardware-arduino-opta/)

They partnered with Finder which is a very well-entrenched industrial controller company.

I did a significant project using Arduino as the controller and a XOJO app as the interface/display.

---

<div class="post-metadata">

**Author:** ![Perry\_Paolantonio](https://forum.xojo.com/user_avatar/forum.xojo.com/perry_paolantonio/32/4353_2.png) [@Perry\_Paolantonio](https://forum.xojo.com/u/Perry_Paolantonio)\
**Post date:** [December 7, 2023, 5:54pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/15 "2023-12-07T17:54:25Z")

</div>

We are using Modbus TCP in Xojo to control a motion picture film scanner. Just use [LibModbus](https://libmodbus.org/) - works great, no need to deal with sockets or formatting.

In our case, we basically created a Xojo class that exposes all of the fuctions of libmodbus that we need, to our application. On launch, we create an instance of that class and the app automatically connects to the MCU. Communicating with it is as simple as:

`TransportController.Write_Register(12, 1)`

In this case, it writes a “1” to register 12

We don’t have a need for all of the functions of the library, which is why I haven’t made my Xojo code public, since it’s incomplete. But getting it set up was pretty easy. Since we have two controllers, one for the transport (motors, etc), and one for the imaging tools - the LEDs, and the camera/lens linear stages, we create two instances on startup: TransportController and ImagingController. Each has its own set of registers and it’s lightning fast.

If somebody wants to finish this off I can toss the Xojo code up on our github and give them access to it.

---

<div class="post-metadata">

**Author:** ![Perry\_Paolantonio](https://forum.xojo.com/user_avatar/forum.xojo.com/perry_paolantonio/32/4353_2.png) [@Perry\_Paolantonio](https://forum.xojo.com/u/Perry_Paolantonio)\
**Post date:** [December 7, 2023, 7:07pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/16 "2023-12-07T19:07:27Z")

</div>

I just took a look at my code and it’s a bit of a mess, since it’s so specific to our implementation. but with a little time I could clean it up. It also has some methods to support reading from and writing to 32 bit registers (which are split across two 16 bit registers), thanks to help a few months back from folks here. My implementation only includes the code for Modbus TCP, but serial communications are also supported by the library, and implementing that shouldn’t be hard, I just don’t have time or the equipment to test that, so I’d leave it to someone else to do that part.

Modbus has turned out to be a great way for us to communicate with the scanner, after a few attempts at developing communication protocols that all had issues. It’s been completely flawless for us so far.

---

<div class="post-metadata">

**Author:** ![Gaurav\_Joshi](https://forum.xojo.com/user_avatar/forum.xojo.com/gaurav_joshi/32/717_2.png) [@Gaurav\_Joshi](https://forum.xojo.com/u/Gaurav_Joshi)\
**Post date:** [March 9, 2024, 4:49am UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/17 "2024-03-09T04:49:57Z")

</div>

Hi Perry,  
Since I am not so used to all of the functionality of Xojo, I don’t understand when you say

> Blockquote  
> we basically created a Xojo class that exposes all of the fuctions of libmodbus that we need, to our application. On launch, we create an instance of that class and the app automatically connects to the MCU

Can you please help me understand the exact process to implement Modbus in Xojo. A bit of hand-holding shall be really helpful. Shall be really grateful for the same.

Regards,  
Gaurav Joshi

---

<div class="post-metadata">

**Author:** ![Perry\_Paolantonio](https://forum.xojo.com/user_avatar/forum.xojo.com/perry_paolantonio/32/4353_2.png) [@Perry\_Paolantonio](https://forum.xojo.com/u/Perry_Paolantonio)\
**Post date:** [March 9, 2024, 2:14pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/18 "2024-03-09T14:14:18Z")

</div>

So Libmodbus is a C library. You can find it here: [https://libmodbus.org/](https://libmodbus.org/) – this needs to be compiled into a DLL (assuming you’re on Windows) or a dylib (mac). Once you have that, you can use Declare statements in Xojo to access any of the functions in that DLL, which are documented on the libmodbus site. This is the most direct way to do it, but doing it with individual declare statements is a little clunky.

Instead, I created a Module in Xojo for libmodbus. This module has a constant called LibModBus that holds the path to the library. Inside the module is a class that has all of the libmodbus functions we needed as External Methods, which are private in scope.

 ![image](https://forum.xojo.com/uploads/default/original/3X/3/d/3db05953af12a0fbbd86c0fb311029b8e97c9473.png)

The external methods each reference the LibModBus constant in their `lib` attribute in the inspector. For each of these External Methods is a method in xojo that calls it. Your code only calls the public method, not the external method.

 ![image](https://forum.xojo.com/uploads/default/original/3X/b/f/bf3c3c5ca3bbfd338fb06b2bdfb155784d678974.png)

The constructor for this class has code that sets up the modbus connection so when you create an instance it automatically handles that and from then on you reference that instance of the class when sending or receiving via modbus.

Doing it this way means you can create an instance of this class when you launch your app, or open the main window, and sending/receiving data via modbus is just a matter of accessing the right method in that class, which is a lot cleaner than constantly declaring into the library every time you need it.

Using the example from my post above: We have two MCUs we control. One for film transport and one for the imaging stuff like LEDs. When the app is started it creates a new instance of the libmodbus class called `TransportController`. This class creates a connection to the transport MCU.

When we want to write to a register on the MCU we call `TransportController.Write_Register(x, y)` where x is the register number and y is the value we want to set. `Write_Register` is a public method in my app’s libmodbus class that calls the private external method called `modbus_write_register`.

`Write_Register` passes the x and y values to the external `modbus_write_register` method, which in turn passes it on to the function of the same name in the DLL.

Right now the implementation of the class we have is very specific to our setup and is incomplete (I only dealt with the functions in libmodbus that we needed and there’s a little code in there that’s not generic) and I just don’t really have time to modify it and make it public right now, but this is the basic outline of how I set it up.

---

<div class="post-metadata">

**Author:** ![Gaurav\_Joshi](https://forum.xojo.com/user_avatar/forum.xojo.com/gaurav_joshi/32/717_2.png) [@Gaurav\_Joshi](https://forum.xojo.com/u/Gaurav_Joshi)\
**Post date:** [March 11, 2024, 4:33am UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/19 "2024-03-11T04:33:04Z")

</div>

Hi Perry,  
Thanks for a detailed reply. If I am ever going to be able to make use of Xojo for Modbus communication, a full credit for the success would go to you.  
I would try to make use of this as you have explained above.  
However, can you please guide me as to how to create a .dll file from libmodbus source, for windows platform. I have infact even tried contacting the maker of the library stephane to get more information on the same. However, haven’t yet got any inputs from him, so thought of asking you once.

Regards,  
Gaurav

---

<div class="post-metadata">

**Author:** ![Perry\_Paolantonio](https://forum.xojo.com/user_avatar/forum.xojo.com/perry_paolantonio/32/4353_2.png) [@Perry\_Paolantonio](https://forum.xojo.com/u/Perry_Paolantonio)\
**Post date:** [March 11, 2024, 12:33pm UTC](https://forum.xojo.com/t/modbus-tcp-ip/70219/20 "2024-03-11T12:33:00Z")

</div>

I am by no means an expert on this stuff but I found it to be pretty straightforward. I believe I used these instructions: [Walkthrough: Create and use your own Dynamic Link Library (C++) | Microsoft Learn](https://learn.microsoft.com/en-us/cpp/build/walkthrough-creating-and-using-a-dynamic-link-library-cpp?view=msvc-170) – basically, you open it in Visual Studio as a DLL project and compile.

One thing that did trip me up was that my Xojo app was 64bit and I think I had to specify that on the DLL settings as well so that it compiled as 64bit. Can’t remember exactly as it was quite a while ago.

[Next page](https://forum.xojo.com/t/modbus-tcp-ip/70219.md?page=2)
