Modbus TCP Master Communication Reading Register from Modbus slave

riken wrote:

I am trying a comunication Modbus with my PLC arduino based on arduino mega, and I want to read a holding registers of Victron Energy inverter

Which implies you will be wanting to use a library for your Arduino that offers Modbus master mode. Beware the library you pointed to above at reply #19 appears to only offer slave mode.


Paul - VK7KPA

Hello pylon,

I´m very busy at the moment. Sorry.
I use Arduino IDE version 1.0.5.
I think, I have to update because ARDUINO 1.8.1 is the latest version. :-o
I will do it. So i hope it is possible to do the Serial.print(F("lalalalala")); change. In 1.0.5 it is not possible.

Hello riken,

yes I use qModMaster.exe. Did you get it?
It´s important that your Arduino mega act as a master. The master can get the holding register data from your slave (Victron Energy) with the function 03 (reading holding register).
Thanks in advance for your help.
We will fix it :smiley:

rockwallaby:
riken wrote:Which implies you will be wanting to use a library for your Arduino that offers Modbus master mode. Beware the library you pointed to above at reply #19 appears to only offer slave mode.


Paul - VK7KPA

konradieee:
Hello pylon,

I´m very busy at the moment. Sorry.
I use Arduino IDE version 1.0.5.
I think, I have to update because ARDUINO 1.8.1 is the latest version. :-o
I will do it. So i hope it is possible to do the Serial.print(F("lalalalala")); change. In 1.0.5 it is not possible.

Hello riken,

yes I use qModMaster.exe. Did you get it?
It´s important that your Arduino mega act as a master. The master can get the holding register data from your slave (Victron Energy) with the function 03 (reading holding register).
Thanks in advance for your help.
We will fix it :smiley:

I don't know that I need to implement my arduino as a master. I think that I can choose the mode.
Thanks a lot. If I don't find a library with Modbus TCP master, I will do my project with modbus RTU master.

riken wrote:

I don't know that I need to implement my arduino as a master. I think that I can choose the mode.

I do know you will need to use Arduino as Modbus master if you wish to succeed in communicating to any slave device such as a Victron inverter. I don't know of any inverter or solar charge controller that will act as Modbus master, they all act as Modbus slaves.


Paul VK7KPA

If I don't find a library with Modbus TCP master, I will do my project with modbus RTU master.

As far as I found information on the Internet Victron doesn't support ModBus RTU (physical interface is an RS-485) but only ModBus TCP.

I have no idea what model inverter is being talked about, just that it is a Victron brand, so we have no idea what communication interfaces are available, either inbuilt or via any purchased accessories.

I believe Victron inverters use their own VE.Bus and VE.Direct propriety protocol communications system.
Depending on the model of inverter, it may have a CAN bus interface that you could use.
Some inverters even use a simple HTTP protocol.

You could look to use the VE.Direct protocol as I believe it could be a fairly simple protocol to implement and connect to it using standard async communications, say from an Arduino, though I would suggest using a galvanic isolator.


Paul VK7KPA

rockwallaby:
riken wrote: I do know you will need to use Arduino as Modbus master if you wish to succeed in communicating to any slave device such as a Victron inverter. I don't know of any inverter or solar charge controller that will act as Modbus master, they all act as Modbus slaves.


Paul VK7KPA

pylon:
As far as I found information on the Internet Victron doesn't support ModBus RTU (physical interface is an RS-485) but only ModBus TCP.

rockwallaby:
I have no idea what model inverter is being talked about, just that it is a Victron brand, so we have no idea what communication interfaces are available, either inbuilt or via any purchased accessories.

I believe Victron inverters use their own VE.Bus and VE.Direct propriety protocol communications system.
Depending on the model of inverter, it may have a CAN bus interface that you could use.
Some inverters even use a simple HTTP protocol.

You could look to use the VE.Direct protocol as I believe it could be a fairly simple protocol to implement and connect to it using standard async communications, say from an Arduino, though I would suggest using a galvanic isolator.


Paul VK7KPA

Thanks a lot, I read datasheet three months ago and I forgot that I can use modbus TCP only.

Well, I think that I have to explain my project. I am doing my final project degree. This project consists in management photovoltaic energy using open source.
I use:
-Raspberry pi 3 + QtCreator
-M-Duino, It's a PLC based on Arduino.
PLC

I want to do control of charges in PLC arduino and having GUI in Raspberry Pi.

I connect PLC and Raspberry using USB connection. I will send data string althouhg, I don't know as I send diferents strings because I need to send four strings.
The only way of getting SOC(state of charge battery) variable, it's reading a inverter photovoltaic register. The model is inverter/charging multiplus 800 VA-5KVA and it's necessary use a color control GTX.
I think that If I read SOC, I will need to use Modbus TCP.

I have two options:

-I connect PLC with inverter and use MgsModbus library.(I need to change much code).
-I connect Raspberry pi with inverter and using QModbus master.
I think the second option is more difficult than first option because I will use QThread surely and I don't know using this library.

I will spend a lot of time on both options.

There are many options on how to do this, using the M-Duino and RPi or just M-Duino or dare I say it, just RPi.

It comes down to how you will implement the GUI.
Will it be via some form of web based application or will it be by a directly connected LCD ?

If you do not have any requirement for any direct control of I/O, then is there any real need for using an M-Duino ?
You can do the whole thing quite well with just using a single small board, such as a GNU/Linux board.
If you use a small ARM based GNU/Linux board, say like RPi, then you can have it communicate Modbus TCP using the very good libmodbus library.

The problem will be if you will implement a GUI that will need an Ethernet connection, say using HTTP or Web Sockets for example. Then you need two Ethernet ports on the board, as it is not wise to have one Ethernet port communicating as Modbus TCP master as well as another TCP protocol at the same time.

You could add an extra Ethernet port by using one of those USB adapters that has Ethernet.
The problem is that it is a RPi, which is not highly rated in my books, not at all.
Look at Olimex boards, say like a Olimex LIME 2.

So, you need to design your system and understand all aspects of what is needed, not just the communications part.

Other factors you need to think about are, your level of programming skill and your available time to complete the project.
I will be honest and say, it would appear your level of skill to design and implement the project will likely be very challenging for you, especially if you have not had a lot of previous experience to use to help pull you through.

So, you have a few options, and here are a few thoughts to consider
Use M-Duino as Modbus TCP master using MsgModbus library. This will allow the M-Duino to get data from Victron inverter and do some other I/O control if necessary. Then, as M-Duino is Modbus TCP master, it can also write data to RPi (phutt, yuck) which is operating also as Modbus TCP slave. The RPi is coded up with a bit of code using libmodbus library and some other code and libraries for GUI.

Throw the M-Duino in the rubbish and just use a small GNU/Linux board as mentioned above, if your choice is RPi, then use that.
All you need is a single board, using libmodbus library. If you have no need for any direct I/O. I would suggest other boards other than RPi, even microPython board would be worth considering, using PyModbus.

Throw the RPi in the rubbish and just use the M-Duino. It can act as Modbus TCP master and also do GUI, either with a LCD connected to the I/O. Even better GUI can be made using the USB port connected to a computer acting as a terminal and code the M-Duino output as VT220. Using VT100/220 allows you to do quite nice GUI using colour (not color) with cursor position control. It is how we all did stuff years ago and I use it with many of the Arduino projects. Look here as an example of a solar project I did to help someone using a MidNite solar charge controller. You will see in this post the png image of the output the Arduino was outputting directly to the computer using a simple terminal application. VT100/220 example image. You might find some useful information and ideas in that thread as well, including code.

Anyhow, it probably would have been better to start a new thread rather that latch on to this thread by another user.


Paul VK7KPA

Thanks rockwallaby,

I need to use RPI and M-Duino because I use M-Duino to sense physics variables as current, soc, irradiance and I also control outputs for manegement energy. RPI is used for GUI and user can programe control.
I want to connect with Internet in the future therefore I want to use Modbus TCP on M-Duino.

Yes, I haven't any experience with Raspberry and Modbus but I will achieve experience with this project.

I have 5 moths to complete this project. Maybe It's possible.

My main problems are Modbus TCP and USB comunications because I need to send a lot of datas for seria USB.
I think that I can do timer interrupts in Qt and sending data strings or bytearray.

I will start new thread.

Then you need two Ethernet ports on the board, as it is not wise to have one Ethernet port communicating as Modbus TCP master as well as another TCP protocol at the same time.

Excuse me but this is rubbish. ModBus TCP is communicating on a specified port and will not be disturbed in any way by other traffic on the same segment. It makes sense to use two separate Ethernet segments (aka two Ethernet ports on the RPi) if you want to connect the RPi to the Internet, so that the ModBus segment is clearly separated. But even in that case it's better to put the firewall before the RPi.

I strongly suggest to use the libmodbus on the RPi for the ModBus TCP protocol. That software is tested to work and not a piece of unfinished work that seems to never got out of a pre-alpha state like ModbusMsg.

Do whatever you already do with the M-Duino and implement the ModBus on the RPi.

riken, with 5 months I am sure you will have success.
I thought you would get battery state of charge (SOC) either from data from the inverter or directly form the inverter ?
If not, then how will you get this by analogue I wonder ?

Remember Qt is an application framework as such, where you can add in your actual application functionality with C++ code.
So, doing this, you could have the RPi acting as Modbus TCP master, and communicating to both the Victron inverter as well as the M-Duino, which will both be Modbus TCP slaves.

To do this, you use the library I mentioned, the very good libmodbus library on the RPi.
The M-Duino can then just do the direct digital and analogue I/O and do what ever calculations.
But the idea with this is that you use the RPi (or other better GNU/Linux board) to do all the number crunching.

Then, you have a single Modbus TCP master which will get the all data from all devices.
There is no need to do other communication with 'strings' or other tricky stuff.

Use Qt for what it was designed for, for nice GUI's, and wrap it around the code to do the Modbus and other calculations you need.

Pylon, you are excused indeed, but it appears to me you did not read my comment correctly, where I say, and I quote, 'as it is not wise to have one Ethernet port communicating as Modbus TCP master as well as another TCP protocol at the same time', with the key word in there being 'wise'.

I did not say it was not possible, and simply mentioned this as many people do not understand the inner details of a protocol such as Modbus, where timing of responses might come into play.

Without the caution, a user might have a single Ethernet port on a board such as a RPi and have the Modbus polling very fast and then expect to be able to have another connection say by HTTP or streaming data via Web Sockets, and wonder why Modbus errors are suddenly appearing.

So, my caution is simply to highlight that it is not 'wise' to use a single Ethernet port while using Modbus TCP with another TCP/UDP protocol.

It has nothing to do with firewalls, and more to do with hardware that is not so good in this particular aspect, i.e. RPi mixed with multiple protocols used by an application that might not be coded in a way with full understanding to avoid tricky issues.

Pylon, I hope I have been able to clarify my point more for you.

Anyhow, riken, go for it and good luck, I think you shall succeed with determination.


Paul - VK7KPA

pylon:
The bigger problem is that the library is so bad, it doesn't check for a correct length but just accesses bytes outside the received message.
The library you used has some more drawbacks, it looks quite unfinished to me.

@pylon - do you have any desire to help finish the MgsModbus library and get it operational? I only ask because I have spent about 10 hours (no exaggeration) looking for an operational Arduino-Modbus TCP/IP MASTER library and could not find one.

Since reading the slave device modbus data is one of the final steps of my project, and it appears that a modbus tcp/ip master library could benefit the Arduino community, I would rather not start over with a Rasp Pi.

I am willing to put in any leg work required and I have the time, but I am a very novice programmer at best. Thanks in advance for any help that you can offer. It would be much appreciated.

rojo20, what is needed is a single unified Modbus library, one that can be configured as client or server for both RTU and TCP modes of operation. Much like the libmodbus library which has proved itself in many embedded applications.

From my research, existing Arduino Modbus libraries may be improved, I am hopeful.

I have been thinking about it over the past while, but it will need time to develop and prove.
Maybe at some time pylon or others and I could work on making some progress, if there is willingness.
Thoughts ?


Paul - VK7KPA

rockwallaby:
rojo20, what is needed is a single unified Modbus library, one that can be configured as client or server for both RTU and TCP modes of operation. Much like the libmodbus library which has proved itself in many embedded applications.

From my research, existing Arduino Modbus libraries may be improved, I am hopeful.

I have been thinking about it over the past while, but it will need time to develop and prove.
Maybe at some time pylon or others and I could work on making some progress, if there is willingness.
Thoughts ?


Paul - VK7KPA

rockwallaby, I am in total agreement with you.

I wish I could help more, but I suck at programming. I am one of those guys that kinda understands, muddles through, edits any code he can find, bangs his head against the wall until something works. I certainly offer my service in a testing capacity or anything else where I can be of use, but correcting libraries is outside of my proficiency unfortunately. Any and all help is super appreciated though. Seems like an Arduino would be perfect for this type of task.

@rockwallaby,

I was thinking for a week and I think that Your opinion it's correct.
I will use a libmodbus library or QModbus library to implementing my program.

I have understanded these examples:

Libmodbus QT

Modbus Master Example

I want to read SOC data to inverter, I have an excell document whose it has ModBus TCP Addresses.

I will try to update with new news.

@pylon - do you have any desire to help finish the MgsModbus library and get it operational? I only ask because I have spent about 10 hours (no exaggeration) looking for an operational Arduino-Modbus TCP/IP MASTER library and could not find one.

To be honest: no. I had some looks at it and almost every time I inspected the code I found another unfinished work, in most cases without even mentioning the unfinished state. As I personally only have ModBus RTU in use I have very little interest in investing time into a ModBus TCP only library.

rojo20, what is needed is a single unified Modbus library, one that can be configured as client or server for both RTU and TCP modes of operation. Much like the libmodbus library which has proved itself in many embedded applications.

I agree that a unified library is probably the best for the Arduino community but I also knew that this will be a lot of work. It's much more work than on the Linux platform because we have to integrate with a lot of #ifdef parts to keep the code small to keep it running on a standard UNO.

From my research, existing Arduino Modbus libraries may be improved, I am hopeful.

Which library did you have in mind when writing the "hopeful"?

pylon:
To be honest: no. I had some looks at it and almost every time I inspected the code I found another unfinished work, in most cases without even mentioning the unfinished state. As I personally only have ModBus RTU in use I have very little interest in investing time into a ModBus TCP only library.

Pylon, I understand. Thank you very much for taking a look at it again. I appreciate it. It is what it is, unfortunately.

pylon, back in mid 2015 I had contact with another user who did a re-write on the SimpleModbusMaster code to improve a number of things, including providing a callback feature for response.

I found the posts made at the time, which might be of interest to you over on the long Modbus thread started by Juan. The discussion starts with Frank aka DDTechG at this post

But do agree with you in terms of the amount of work to create a single unified library, especially for the various boards with limited program memory.

The code for ModbusMaster by Doc Walker appears neat and very readable. Though I notice it appears that after a master to slave transaction, the code will sit in a while loop waiting for a reply from the slave, which is not the best method to handle this type of situation, hence why I like the callback feature implemented in the code by DDTechG.


Paul _VK7KPA

I tried your(kondradieee) program as modbus master in tcp i but I am not getting output. Please send me the correct program.

riken:
@rockwallaby,

I was thinking for a week and I think that Your opinion it's correct.
I will use a libmodbus library or QModbus library to implementing my program.

I have understanded these examples:

Libmodbus QT

Modbus Master Example

I want to read SOC data to inverter, I have an excell document whose it has ModBus TCP Addresses.

I will try to update with new news.

I need to develop a similar project, could you please tell me which library did you use? How did you manage the communication between inverter and Raspberry?