Page 1 of 1
Modbus
Posted: 18 Apr 2018, 19:31
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!
Modbus Slave
Posted: 18 Apr 2018, 19:52
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!
Re: Modbus
Posted: 18 Apr 2018, 22:05
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.
Re: Modbus
Posted: 18 Apr 2018, 22:13
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.
Re: Modbus
Posted: 19 Apr 2018, 09:58
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?
Re: Modbus
Posted: 19 Apr 2018, 11:24
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.
Re: Modbus
Posted: 19 Apr 2018, 12:38
by gambrinus
Thanks Volker! Certainly seems simple enough... I'll stop by Sven's forum.