Modbus

Topics about the Software of Revolution Pi
Post Reply
gambrinus
Posts: 11
Joined: 18 Apr 2018, 19:26
Answers: 0

Modbus

Post by gambrinus »

Hello,
I am considering purchasing the RevPi Core 3 for an automation project. For my purpose, I need the RevPi to be a Modbus slave to an OPC-UA server. I am interested in whether or not there is a Python module for reading and writing to Modbus registers? I'd like to be able to read and write to each to register based on name, not register numbers. Looks like it is possible via bash shell, but I am curious if there is built-in support in the Python module?
Thanks!
gambrinus
Posts: 11
Joined: 18 Apr 2018, 19:26
Answers: 0

Modbus Slave

Post by gambrinus »

I am considering purchasing a RevPi Core 3 for an automation project. For my project, I need the Pi to perform some basic IO, but also store floats in a tag for an OPC-UA server. After doing some research, it appears this is possible with Pictory Modbus Slave, but the examples do not make it clear if you can access the registers with Python. I see that it can be done through the pitest application, so I'm guessing there is support in the Python module. Is there any API support in Python for manipulating Modbus registers?

Thanks!
User avatar
volker
Posts: 1046
Joined: 09 Nov 2016, 15:41
Answers: 1

Re: Modbus

Post by volker »

Hi,
there is a "virtual" module "Modbus Master" as well as "Modbus Slave" both are available as TCP or RTU version. Please read the Tutorials how to use them. They work by configuration, no coding! The values are exchanged with the central process image just like any other RevPi IO module does. Any software can access the values from the central process image by using file based linux IO functions (read, write, seek and ioctl). There are ready to buy 3rd party software components which are OPC UA servers exchanging their data pool with the RevPi process image: Products from TANI and IBH softec. You configure them via GUI.
Unser RevPi Motto: Don't just claim it - make it!
User avatar
volker
Posts: 1046
Joined: 09 Nov 2016, 15:41
Answers: 1

Re: Modbus

Post by volker »

okay, that was a cross posting. So you have already read about virtual module Modbus. As I already have written the Modbus values are stored in the central process image. There is Python example of how to exchange values with this memory area using piControl kernel driver. Idf you are using IEE floating you need to understand the byte order of your Modbus Master because this is not Modbus standard. If you have understood the byte order you can then construct a Python byte array with the bytes to be stored in the Modbus registers and load the into the central process image by piControl.write function (length and byte array are the function parameters. Please also note that Modbus TCP master module does rearange the bytes of the process image words from little endian into big endian.
Unser RevPi Motto: Don't just claim it - make it!
gambrinus
Posts: 11
Joined: 18 Apr 2018, 19:26
Answers: 0

Re: Modbus

Post by gambrinus »

I see. Bit order in the processing image is little endian, but protocol will communicate it big endian. So to store a 32 bit float, I will need to write some code for handling it as 2 16 bit registers. That's not too difficult. Thank you for answering my questions. Sorry about the initial double-post. I see tutorials about how to access IOs as virtual device in Python, but I cannot find one for Modbus. Is it safe to assume that the Modbus virtual device works the same way? For example, if my virtual modbus slave at position 64, would I use revpimodio2 like this?

import revpimodio2
modbus = revpimodio2.RevPiModIODriver(64, autorefresh=True)
modbus.io.Input_1.replace_io("int1", "H")

Can I then access that int as modbus.io.int1.value?

Sorry for the questions... I don't have the product in front of me to experiment with. I just want to verify that I can do everything I need to be able to do before we commit to purchasing one for development. Documentation on accessing modbus registers via python is either not clear or difficult to find, but I'm guessing that's because it functions just like any of the other virtual devices?
User avatar
volker
Posts: 1046
Joined: 09 Nov 2016, 15:41
Answers: 1

Re: Modbus

Post by volker »

I can't answer your question about usage of RevPiModIO as this is supported by Sven of Miprotec who created it. Please refer to this in his forum or blog. You may post in English into his forum although it looks like German only ;-)

Yes, there is no difference between any module (virtual or not virtual): Process values get stored in the central process image (CPI) and you exchange them programatically by using the file based driver piControl:

driver = piControl()
driver.open()
driver.seek(offset)
driver.read(number of bytes)
driver.close

It is easy like that...

If you need to read using Process variable names you simply build your offset table at start time by reading the _config.rsc file (it's a JSON formatted file) into a JSON structure and extracting the process variables including their offset.
Unser RevPi Motto: Don't just claim it - make it!
gambrinus
Posts: 11
Joined: 18 Apr 2018, 19:26
Answers: 0

Re: Modbus

Post by gambrinus »

Thanks Volker! Certainly seems simple enough... I'll stop by Sven's forum.
Post Reply