Modbus TCP/IP

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

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.

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

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

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

2 Likes

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.

1 Like

Its because its a paid module.

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

Dennis

1 Like

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

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.

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.

1 Like

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.

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

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

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.

We are using Modbus TCP in Xojo to control a motion picture film scanner. Just use LibModbus - 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.

1 Like

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.

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

So Libmodbus is a C library. You can find it here: 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.

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.

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.

1 Like

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

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

1 Like