mbunds
Hello again!

I have begun experimenting with ways to cause an instance of OpenPLC Runtime to communicate with other instances.

I realize that psm.py is an internal routine, folded-up into the main application upon compilation, and is not the place to enter PSM driver code, and should not be modified unless the user is willing to risk the consequences.

Inside the psm.py codebase I found:

 

global mtcp_server

def run_server():

    global mtcp_server

    store = ModbusSlaveContext(di=d_inputs, co=d_outputs, ir=a_inputs, hr=a_outputs, zero_mode=True)

    context = ModbusServerContext(slaves=store, single=True) 

    #run server

    mtcp_server = ModbusTcpServer(context, address=("localhost", 2605))

    mtcp_server.serve_forever()

 

Is it possible to modify the server context string so this routine might contact "remote" instances outside of the localhost context?

(I'm certain the answer is "NO", but one can hope...)

thiagoralves
Hi @mbunds 
There is nothing you can’t do if you’re willing to go to the extent of writing the right code. That being said, why don’t you just configure one instance as the slave device of the other? This is the official and easiest way to have two instances communicating.
Reply 0 0
mbunds

Thank you, Zen master!

I truly and deeply appreciate your methods for patiently guiding users toward enlightenment, providing just enough to let those of us truly wishing to learn to have their own discovery.

This way of teaching is so much more impactful than providing the entire solution, and I'll bet you know this, given users/students who will ask a few questions, and then return with ephanies, versus those who basically want you to do all the work.

From an earlier post, you had suggested that assigning one of the PLC's to multiple others as a "slave" was permissible, which adds an option to my tool set, but may not provide the entire solution.

Long story short, I am testing OpenPLC as one of the solutions for simulating PLCs within a "digital-twin", basically a process plant or factory either partially or entirely simulated in software. I have a Linux hypervisor running multiple (16) LXI containers, each of which is loaded with a lightweight, "headless" Ubuntu server instance, upon which runs an instance of OpenPLC Runtime. 

This mirrors the 16 PLCs making up the DCS in our physical plant, but other LXI "PLC Modules" can be spawned instantly from an LXI template holding a pre-configured instance of the "base" PLC runtime. These can be used to create "responders" simulating more complex sensors on the network.

The physical PLCs and PACs in my plant never commit writes.

Each one has a similar "front-end" logic which queries one or more remote PLCs, then sets-up registers to be read by others.

I didn't set this up, because where I come from (energy/oil/gas), MY PLC processes I/O data, reads remotes only if necessary, processes the RLO, and then SENDS it; whatever is on the receiving end had better be ready to accept it, and send me a receipt. I'm not used to "asking politely" for all of my data, then waiting around for someone to consume my registers.

This is getting long, so I'll wrap-up.

If nothing else pans out, the finished "digital twin" would let me attach at least one "real" PLC by disabling its simulated twin running in the "OpenPLC mirror node", and allow the physical PLC to respond to the data read from the simulated nodes from the virtual network, in exactly the same way as it would on the real network. While its true that I will have other challenges making the physical I/O respond, I have a plan to use OpenPLC on Atmel or RPI to provide physical feedback, generated from the simulated plant.

Given the topology I describe, and among the "PLC nodes" running within the simulated DCS, it is obvious that the internal communications among soft PLCs within the ideal configuration is irrelevant.

The thing is that our "real" network commits a lot of reads (and no writes); essentially, everyone is simultaneously both "client" and "server", so you can imagine how this doesn't exactly dovetail with cross-assigning slaves to slaves, and will definitely not line-up with the "real-world" Modbus queries expecting to find all of this data available within the "non-OpenPLC-slave" address ranges.

For example, in our plant:

PLC"X" reads data from 12 others

PLC"A" reads data from PLC"X" and PLC"F"

PLC"B" reads data from PLC"A", PLC"X", and PLC"F"

And so on.

Assigning slaves to OpenPLC Runtime instances to support this behavior may work out, as long as I don't introduce collisions between internally addressed "remote" I/O, and real queries from hardware under test.

Thanks again!

Reply 0 0
thiagoralves
Yeah, this is a really complicated setup. Perhaps the best way to move forward would be to create Modbus functionblocks that can be called on the PLC program itself instead of a configuration on the runtime. This is also possible, but involves writing the code on the editor to support a block like that. I have this on my TO-DO list as it will add spurious modbus communication for Arduino boards. This post here:  https://openplc.discussion.community/post/how-to-add-my-fbd-to-default-pouslibrary-10408620?pid=1310581219 will tell you what you have to do to create a new block on the editor. Following this design you can write code for your own Modbus block in C. I know it is not simple, but I'm pretty sure you can reuse a lot of OpenPLC code as it has Modbus implemented on the runtime.
Reply 1 0
mbunds

Thanks again!

The ability to patch-in user-designed function blocks is awesome, but do you think this is a better path than enhancing the code within the psm.py wrapper?
(When I say "better", I usually mean "faster" or "easier" - PSM provided a path-of-least-resistance, just as I was gearing-up to play in your source code and discover what would be necessary to extend its core functionality, which I probably would have abandoned for faster solutions anyway.)

After noticing the presence of psm.py within the runtime source, I downloaded several common packages for testing, including the pymodbus libraries, intending to expand psm.py functionality to allow "direct" reads from remotes.

Unless I misunderstand the way you integrated PSM with underlying C++ code, "psm.py" appears to be a wrapper/abstraction layer/translator between "psm user-code" and the OpenPLC Runtime instance. It appears to leverage a pymodbus server, but for communicating strictly between a user's python device drivers, and the Modbus protocol stack running within the local instance, bound exclusively to localhost (127.0.0.1 ONLY) of OpenPLC Runtime.

That's when I fired-off the question to you about the possibility of modifying psm.py to talk to remote instances, as well as its local one. If this works, then great! I'll send you the solution, but if not, an independent instance of pymodbus would probably be just as happy running on a seperate thread in my LXCs, performing the same tasks, but not disturbing any of the PSM functionality within the OpenPLC Runtime itself.

If I'm right, it looks like an OpenPLC Runtime instance, updated to provide PSM functionality, is essentially running "twin" Modbus stacks; one inside of the OpenPLC runtime (slave), and one patched-in (pymodbus) to allow user devices having python drivers/libraries to communicate "back" to the slave running within the main process - the OpenPLC Runtime instance.

If this (modifying psm.py) works from within an OpenPLC Runtime instance, I imagine you would consider it more of a "patch" than a "solution", since steering me toward the C++ source code tells me that you would rather have remote comms be intrinsic, written in native code, rather than patched-on to the PSM system which you created for a different purpose entirely (user device python driver creation). After proving whether the digital twin concept will work as planned, I'll probably swing-around and take up the main OpenPLC Runtime source. Fortunately, our "real" plant does not use any addresses outside of the more limited range provided by OpenPLC, which would be nice to have expanded, but the ability to cause OpenPLC to act more like a proper DCS having remote comms, even with limitations, would make a lot of users very happy.

Then again, maybe it's simpler than I am giving it credit for, since I'm coming from a commercial software environment that offers huge lists of features we kludge together using canned editors, which usually offer no modification of source code except for basic scripting with VBA, LUA, or RUBY.

(NOTE: In earlier messages I erroneously referred to "LXC" containers as "LXI" due to an esoteric package naming Linux Execution Instances (LXI) which I must have typed ten million times.)

BELOW: WORK-UP DRAFT - Typical LXC instances with instrumentation responders.
DRAFT - IDEAL STACK - 2022-09-22_11-19-33.png


 

 

Reply 0 0
mbunds
Yeah, this is a really complicated setup. Perhaps the best way to move forward would be to create Modbus functionblocks that can be called on the PLC program itself instead of a configuration on the runtime. This is also possible, but involves writing the code on the editor to support a block like that. I have this on my TO-DO list as it will add spurious modbus communication for Arduino boards. This post here:  https://openplc.discussion.community/post/how-to-add-my-fbd-to-default-pouslibrary-10408620?pid=1310581219 will tell you what you have to do to create a new block on the editor. Following this design you can write code for your own Modbus block in C. I know it is not simple, but I'm pretty sure you can reuse a lot of OpenPLC code as it has Modbus implemented on the runtime.


I'm beginning to discover many different options that may support my use-case., and likely many others

Today I found "bit-linking" in ScadaBR, and I have to tell you that PSM has opened-up a whole world of possibilities, as you said; "with enough imagination"...

Anyway, once I arrive at some more-or-less "solid" solutions for OpenPLC communications among soft PLC's, I'll share them, but at the end of the day it looks like it will be fairly easy.
Reply 0 0
Reply