Page 1 of 1

Modbus

Posted: 19 Apr 2018, 12:43
by gambrinus
Hello,

I was directed here by Volker. I need to know if the RevPiModIO python module supports reading and writing to virtual modbus registers?

Thanks!

Re: Modbus

Posted: 28 Apr 2018, 23:58
by volker
You will never read and write "Modbus Registers" with the virtual devices Modbus RTU/TCP master or slave. These virtual modules are performing cyclically "actions" by sending or receiving Modbus function calls to the partner reading and writing Modbus registers of the partner. The results of these actions are read or written into named process variables of the central process image of the RevPi Core.
Any of the programs running on a RevPi is using read and write access to this central process image (CPI) to exchange data - so does also RevPiModIO. Sven may explain you more in detail how easy this can be done with his library. But there is no difference in where the data in the CPI has come from. this should be absolutely transparent to the library.

Re: Modbus

Posted: 01 May 2018, 18:13
by gambrinus
Thanks, I figured it out. To any user who may be interested, you just use revpimodio2.RevPiModIO(n) where n is the position of the Modbus TCP virtual device.

In fact, you are not limited to words as the PiCtory app shows. Below is a functional example of how to write a float to an output that can be read as an Input Register from another device.

Code: Select all

import revpimodio2

class MyDriver():
    def __init__(self):
        self.rpi = revpimodio2.RevPiModIO(autorefresh=True)
        self.rpi.handlesignalend() 
        self.modbus = revpimodio2.RevPiModIO(64)
        
        self.modbus.io.Output_3.replace_io("myfloat", "f") 
        

    def cyclefunction(self, cycletools):       
        self.modbus.io.myfloat.value = 3.14
        
    def start(self):
        self.rpi.cycleloop(self.cyclefunction, cycletime=1000) 

if __name__ == "__main__": 
    driver = MyDriver()
    driver.start()
    

Re: Modbus

Posted: 01 May 2018, 22:56
by volker
Hi,
please be aware that IEEE floats are not defined by Modbus. Therefor you will find several differing ways to implement floats in multiple Modbus Word registers. Our process image does convert the Modbus big endian words in liitle endian so that you can use any C word to read and write to Mobdu registers. But many Modbus devices use also big endian for multiple register formats (long integers or floating). Thus you will endup with a mixture of bigendian word order and little endian byte order in the words. So please take care and inform yourself about the device's format of multiple word process values. Especially energy measurement devices very often use 32 or 64 bit numbers stored in 2 or 4 Modbus registers and most of the time they are stored in big endian order.

If you use your own software for the Modbus slave and the Modbus master this will of course not be a problem as you mirror the encoding and decoding of IEEE float.

Re: Modbus

Posted: 02 May 2018, 14:11
by gambrinus
Thanks Volker! The OPC server we're using allows us to configure byte order, so no problems =). I was able to get the float decoded into a tag on the OPC server easily enough. I'm working on a custom RAP file now.