davenardella
I am working on the integration of the S7 protocol in OpenPLC using my Snap7 libraries.
S7Protocol is a structurally complex protocol but it is very performant and secure (from the point of view of data integrity).
The Server part completely emulates a Siemens communication processor, once activated, OpenPLC is seen from the outside (including Step7) as a Siemens S7-315 PN/DP CPU.
All scadas that implement this protocol can read and write internal resources.
 
Current status

I have grafted the library into OpenPLC maintaining the existing conventions: there is an option file (s7protocol) in /script that if it contains the string s7ptrotocol, at the first compilation of a new PLC program, the wrapper files are automatically included in the compilation; otherwise they are deleted and no reference to them remains.

In Linux/Raspberry the library is compiled during the OpenPLC installation, so they will always be aligned with the libcc and the architecture (32/64).
In Windows I use the compiled (VisualStudio) dll wich are smaller and more performant.(However, if you want you can recompile them with mingw32/64, there is a makefile into the official Snap7 distribution).
 
Ultimately the changes are very non-intrusive, I added the snap7_src folder to /utils and modified 3 files ( background_installer.sh , compile_program.sh and main.cpp).
 
Install (Raspberry)
install.png
 
Compile user program
compile.png
 
Run program (Raspberry)
start.png
 
Client connected to Windows OpenPLC
snap7client.png
 
 
I currently have it implemented on
  • Windows (msys2)
  • Linux
  • Raspberry
Now all the work will be done only in the oplc_snap7.cpp file that in addition to containing the wrapper of the communication functions, manages the interface with OpenPLC.
 
and here we come to the second part...
Reply 1 0
davenardella
To Do
Snap7 Server can share internal resources in two ways:
in the simple one, it is enough to register the memory areas by passing address and size.

s7server.png
 
In this example, from the outside, all I/O/M read/write requests will act on these "fake" arrays.
 
registerarea.png
 
the second method, a bit longer, is to use callbacks made available by Snap7Server.
It is a bit more complex, but allows greater control of what happens.
 
callbacks.png
 
Currently Snap7 guarantees consistency between access of multiple clients to the same area (the server is multithreaded and allows up to 1024 simultaneous connections), but not between Client and PLC for data that exceed the standard size (1 byte, 1 word, 1 dword etc..) as in real Siemens PLCs.

At this point, do you have any suggestions/proposals on how to manage internal resources?
Reply 1 0
thiagoralves
Wow!!! This is incredibly amazing!! Thanks for that awesome contribution @davenardella
 
I do have some ideas that might improve this a little bit. Perhaps the easiest one would be to map the S7 DBs into the Modbus buffers and then call it the day. It will essentially make the S7 implementation have the same mapping as the Modbus one. I'm not too familiar with the S7 protocol, but I think this might work. If S7 has some form of tag exposure like OPC-UA, where you can simply browse by tag names to read/write to the correct locations, then maybe a better implementation would be to parse the debug.c file (generated dynamically for every program submission) to actually get variable names, locations, etc, and then populate a buffer accordingly.
 
Another approach, that may seem more work upfront but that is definitely rewarding in the long run, would be to create additional Function Blocks on the editor's library containing S7 specific items. These blocks could create the DBs and populate them with data from the user program.
Reply 0 0
fg71
Looking forward to test this with Siemens LOGO, Python and Node-Red and make some examples to share in this forum.
It would be interesting if I could use WinCC and setup KTP400 to be used with OpenPLC and Raspberry Pi.
Reply 0 0
davenardella
Looking forward to test this with Siemens LOGO, Python and Node-Red and make some examples to share in this forum.
It would be interesting if I could use WinCC and setup KTP400 to be used with OpenPLC and Raspberry Pi.
Yes, Snap7Server works with KTP400 and other panels (via WinCC), and yes, it uses GET/PUT mechanism wich encapsulates S7Protocol.
 
Actually it would also be possible to make OpenPLC an active partner, i.e. connect to a Siemens PLC and read/write a DB; since, the S7Client object is included in the library.
The operation is feasible, however it is not immediate (zero-conf), it would be necessary to create an IEC FB that encapsulates S7Client.
Reply 0 0
davenardella
Wow!!! This is incredibly amazing!! Thanks for that awesome contribution @davenardella
 
I do have some ideas that might improve this a little bit. Perhaps the easiest one would be to map the S7 DBs into the Modbus buffers and then call it the day. It will essentially make the S7 implementation have the same mapping as the Modbus one. I'm not too familiar with the S7 protocol, but I think this might work. If S7 has some form of tag exposure like OPC-UA, where you can simply browse by tag names to read/write to the correct locations, then maybe a better implementation would be to parse the debug.c file (generated dynamically for every program submission) to actually get variable names, locations, etc, and then populate a buffer accordingly.
 
Another approach, that may seem more work upfront but that is definitely rewarding in the long run, would be to create additional Function Blocks on the editor's library containing S7 specific items. These blocks could create the DBs and populate them with data from the user program.
Thanks for the reply Thiago!
 
As for I/O/M, I think I will work on internal buffers (obviously synchronizing accesses with the PLC cycle and other actors to keep data consistent). This is to remain faithful to the S7Protocol philosophy which is zero-conf: all resources are accessible from the outside, even if they are not used and even if the PLC is completely empty.

I will have to understand how to manage/interpret the concept of DB.
 
I will do some tests/considerations and we will talk about it again to understand if it is a useful path. 😉
 
Reply 0 0
fg71
it would be necessary to create an IEC FB that encapsulates S7Client.
Do you mean Network block (FB) that you find in LSC ??
Reply 0 0
fg71
I made a fast example in Python to use Network block in Siemens LOGO to use V0.0 as input to activate output.
I have setup Siemens LOGO as S7 communication (Server), Python S7 communication (Client)
[203128%2F9222964%2Fmceclip0]
 
Here is the Python code
 
import snap7
from snap7.util import set_bool, get_bool

# Configuration
PLC_IP = "192.168.0.2"
DB_NUMBER = 1
START_ADDRESS = 0

# Initialize client
client = snap7.client.Client()
client.connect(PLC_IP, 0, 1)

def read_bit(db_number, start_address, byte_index, bit_index):
    data = client.db_read(db_number, start_address, 1)
    bit_value = get_bool(data, byte_index, bit_index)
    return bit_value

def write_bit(db_number, start_address, byte_index, bit_index, value):
    data = client.db_read(db_number, start_address, 1)
    set_bool(data, byte_index, bit_index, value)
    client.db_write(db_number, start_address, data)
    print(f"Successfully wrote bit {bit_index} in byte {byte_index} to {value}")

# Example usage
if __name__ == "__main__":
    byte_index = 0  # Index of the byte within the data block
    bit_index = 0   # Index of the bit within the byte (0-7)

    # Read bit value
    bit_value = read_bit(DB_NUMBER, START_ADDRESS, byte_index, bit_index)
    print(f"Initial bit value: {bit_value}")

    # Write bit value
    new_value = True  # Set the bit
    write_bit(DB_NUMBER, START_ADDRESS, byte_index, bit_index, new_value)

    # Read bit value again
    bit_value = read_bit(DB_NUMBER, START_ADDRESS, byte_index, bit_index)
    print(f"Updated bit value: {bit_value}")

    client.disconnect()
 
Reply 0 0
davenardella
Do you mean Network block (FB) that you find in LSC ??
rather I meant an IEC 61131-3 FB callable inside OpenPLC (ladder/st..), LSC is specific to LOGO
Reply 0 0
fg71
@davenardella, Thanks, that is wonderful news for OpenPLC I think
Reply 0 0
davenardella
It seems to work... 😉
WinCC-OpenPLC.png
Reply 0 0
thiagoralves
😱😱😱😱😱😱😱😱
Reply 0 0
davenardella

I'm going through the tests but I noticed a "glitch" about %MW0.
Although I can read and write it well with WinCC, its value is not monitored.
So I excluded the Snap7 code, to avoid any interference, and I did some tests with OpenPLC alone.
This is the test program, a simple var inc.
 
st_program.png
 
And this is the monitoring page with %MW0 dead.
monitoring.png
 
So I've been digging around in the sources a bit and found, maybe, a couple of typos in modbus.cpp
code.png
 
using <= instead of < causes reading/writing of int_output[1024] (which is also out of bounds) instead of int_memory[0].
 
After changing <= to < seems working fine,.I also tried with SnapModbus client tool and the variable seems to be accessible correctly.
 
working.png
Reply 0 0
richw
This has been an issue since the runtime went from red to blue, it has been reported many times as a bug.
Reply 0 0
thiagoralves
This is great! Thanks for finding the bug Dave! Indeed this bug has been reported a couple times, but I never had a chance to stop and look at it throughly. I'm glad you were able to find it and fix it. So how is the Snap7 implementation on OpenPLC? Does it depend on Modbus to work? Are you planing to push a PR with the changes soon?
 
By the way, I see you are using the new editor on your tests. How has been your experience with it so far? Have you tried the new 4.0.1-beta released this week?
Reply 1 0
Reply