fg71
I'm not sure I understand how snap7 is working or how I could use Snap7 Client to set GPIO on RPi.
 
When I use python-snap7 and use this function
def write_bit(db_number, start_address, byte_index, bit_index, value):
    byte_size = 16
    data = s7Client.db_read(db_number, start_address, byte_size)
    set_bool(data, byte_index, bit_index, value)
    s7Client.db_write(db_number, start_address, data)
    print(f"Successfully wrote bit {bit_index} in byte {byte_index} to {value}")
 
And use: this: write_bit(DB2, 0, 0, 0, True)
 
In LOGO world that is V0.0 to become True.
 
How will that look in OpenPLC Editor world ??
 
 
Here I could see from @davenardella snap7 server that
[203128%2F9255952%2Fmceclip0]
 
I understand that the start_address python function is the position (index) 0 - 15, start from left to right in the gif above.
There byte_index and bit_index is 0, if we change the start_address from 0 - 15 we get 01 from left to right index position.
On other hand we could get the same effect by setting the start_address to 0 and use byte_index. 0 - 15
In the gif animation below we have used start_address of 0 and byte_index 15 and bit_index 0
[203128%2F9255958%2Fmceclip0]
 
Little confusing when this, change from 02 to 03 when all other go from 00 to 01
[203128%2F9255988%2Fmceclip0]
 
I use start_address of 2 and byte_index and bit_index to 0
 
@davenardella, do you think it maybe are a bug in 0010 the first index from he left ??
I used your Snap7 Server Demo
 
To demo you could use my Python script
from snap7 import client
from snap7.util import set_bool, get_bool
from time import sleep

s7Client = client.Client()

def connect_to_plc(PLC):
    s7Client.connect(PLC, 0, 2)
    if (s7Client):
        print("Connected to PLC")

def get_info():
    plc_info = s7Client.get_cpu_info()
    print(plc_info)

def get_cpu_state():
    plc_state = s7Client.get_cpu_state()
    print(plc_state)

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

while True:
    connect_to_plc(YOUR_IP)
    if not (s7Client.get_connected()):
        s7Client.connect()
        s7Client.plc_cold_start()
        sleep(2)
    
    start_address = 2
    byte_index = 0
    bit_index = 0
    write_bit(2, start_address*8+bit_index, byte_index, bit_index, True)
    sleep(2)
    write_bit(2, start_address*8+bit_index, byte_index, bit_index, False)
    sleep(2)
    s7Client.disconnect()
 
Reply 0 0
davenardella
This method is really dangerous, reading a byte, modifying a bit and writing the byte back:
1 - it doesn't ensure consistency, in the meantime someone could have modified the byte
2 - it requires two transactions.
 
you have to use WriteArea passing S7WLBit as wordlenght, Start = byte_index*8 + bit_index and size = 1
 
unluckily, seems that wo wrote python snap7 wrapper forgot to use wordlength
 
writearea.png
 
How will that look in OpenPLC Editor world ??
Writing DB2, as explained in the user manual, affects %IW
Reply 0 0
davenardella
Let's start from beginning.
S7Protocol has a function inside the PLC to read and write data, this is made using the codes 1 and 2 inside the PDU.
 
The official exported (C) function, to do this, is
int S7API Cli_WriteArea(S7Object Client, int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData);
which in the object oriented (C++) wrapper bacame:
 
Client.WriteArea(int Area, int DBNumber, int Start, int Amount, int WordLen, void *pUsrData);
 
When WordLen != S7WLBit, Start is the Byte Start and Amount is the Byte Count.
 
When WordLen == S7WLBit, Start is the Bit Index and Amount must be 1. IF the byte pointed by pUsrData is == 0 the bit is reset otherwise is set.
 
if I write the 128th bit it modifies the first bit of byte 16
if I write the 129th bit it modifies the second bit of byte 16 and so on...
according to the rule byte_idx = Start /8 and bit_idx = Start % 8.
 
This works for all addresses, read and write.
 
dbwrite.png
  • I don't know why, whoever wrote the python wrapper didn't handle WordLen
  • I don't understand why in your function you need two variables to indicate the same thing (start_address and byte _index) and then you use start_address*16+0 as if it were a bit_index.
  • I don't understand why you read and write 16 bytes to modify a bit.
  • I don't know how your set_bool function works.
Since you only work with bytes (since you're unlucky to have to use the python wrapper 😂) focus on that: can you read and write a byte in the right place? Then you fix all the messes with the bits 😉
Reply 0 0
fg71
I could get the bit by using this Python code
from snap7.type import WordLen

S7WLBit = WordLen.Bit
print(S7WLBit)
 
Reply 0 0
davenardella
No, you cannot.
As I sayd you cannot use WordLen because WordLen is not managed by the Python wrapper.
 
Either modify the wrapper adding a Read/Writebit function that manages WordLen.
Or Read ONE byte (not 16) modify the bit then write back the byte.
If you chose the second way I suggest you to check the result of the read before attempting the write.
 
For fun both the ways are ok, for a serious software avoid read/modify/write cycles because they are not consistent.
 
Finally
I'm not sure I understand how snap7 is working or how I could use Snap7 Client to set GPIO on RPi.
 
If you are using a SoftPLC into the Raspberry, you should never write directly an output, because:
1 - It's not safe because it's not synchronized with the main program.
2 - The output could be overwritten by the IPU of the PLC cycle.
 
if you choose to mess around just for fun, ok, however, if you want to follow the logic of PLCs, even what you do must be in line with the dictates of industrial controls.
 
So, write a memory bit, then in your PLC logic set/reset the output in accord to the memory and the logic conditions.
Reply 0 0
fg71
Do not Siemens use %M0.0 (Memory flag or Merker) in Tia Portal to set bool to True or False ??
In LOGO its called memory flag and its Read/Write so I have used it when I like to do user input for HMI.
 
I checked the WriteArea() function in Go, it looks like correct to me.
 
Maybe I could download Go and play with that 🙂
 
There will always be some latency between real hardware and software. That said its a crazy world we are living there emulation of old hardware couldn't only give higher fps but also the resolution for games. SoftPLC could be anything I believe if its done correctly.
Is not Beckhoff based on FreeBSD and EtherCat is not a slow protocol.
 
How fast is OpenPLC_v3 runtime compare to other SoftPLC solutions ??
Reply 0 0
fg71
I did some test in OpenPLC_v3 and Snap7.
I have always used variable memory with s7 protocol but realized that LOGO do support DB as Client.
If I use variable memory default db is 1 and that is what I have used before. After some test I understand that OpenPLC_v3 Snap7 use DB2.
So I configure LOGO to use DB2 and I could see the result on your Snap7 Server Demo. I changed to IP of OpenPLC_v3.
And use your Snap7 Client Demo to access the DB2 and byte and bits.
When I read the DB2 from your Snap7 Client Demo app it looks like the byte is started at 1 and I used the WordLen S7WLByte.
In other words the read from your Snap7 Server Demo was not same as your implementation of Snap7 OpenPLC_v3.
I have only change the IP and that why I'm little confused.
 
Could you check if your Snap7 Server Demo match OpenPLC snap7 ??
 
This is setup in LOGO, you could see I use DB2, byte 0 and bit 0, that will show in your Snap7 Server Demo row 1 and column 1
But if connect to OpenPLC_v3 and use Snap7 and use your Snap7 Client Demo and read it will show row 1 and column 2
In other words the byte start at 1 and not 0
[203128%2F9260405%2Fmceclip0]
Reply 0 0
davenardella
I already explained it to you about SnapTRAINER and the concept does not change.
 
Siemens is BIG-ENDIAN and the DB2->IW% pair, to be consistent as a 16-bit value, must be swapped in incoming.
 
If you want it to be consistent at the bit or byte level you must swap your address.
Look at this table.
 
big endian.png
 
DB2.DBB0 is the HI-order byte 04
DB2.DBB1 is the LO-order byte 28
 
Reply 0 0
fg71
So if I understand your correctly your Snap7 Server Demo store the memory as Big-endian and OpenPLC_v3 store its as little-endian ?
Why I think so is LOGO wrote to the correct byte/bit to your Snap7 Server Demo.
Now I asume Siemens LOGO use big-endian
 
Is this what your are talking about
# Original data (16-bit integer)
data = 0x1234

# Swap bytes manually
def swap_data(data):
    return ((data & 0xFF) << 8) | ((data >> 8) & 0xFF)

print(f"{swap_data(data):#06x}")
 
Reply 0 0
davenardella
Correct.
The big-endian low-endian concept is a bit annoying. However Snap7 has a 300 page PDF manual (which you should read before going bug hunting), where these concepts are explained in detail. 😉
Reply 0 0
fg71
Yes you are correct, but I also read from Siemens documentation and recommendation to use optimized data block for s7-1200 and s7-1500
And this data block is not Big-Endian they are Little-Endian. So the only reason Siemens still have Big-Endian is for backward compatibility.
So if the hardware is not legacy, the recommendation is to use Little-Endian. (Optimized Data Block)
 
That said I could read in your Snap7 manual that the optimized block access must be turned off.
 
You could read this from chapter 2.6.3
Reply 0 0
davenardella
Yes you are correct, but I also read from Siemens documentation and recommendation to use optimized data block for s7-1200 and s7-1500
And this data block is not Big-Endian they are Little-Endian.
 
Variables of optimized datablock have not an address known from outside because it depends on how the compiler fit them in the memory, so they are unusable with S7Protocol V1. They can be accessed only "byname" using OPC or S7Protocol V2.
 
So if the hardware is not legacy, the recommendation is to use Little-Endian. (Optimized Data Block)
It's not a hardware issue:
S7300/400/WinAC/LOGO -> S7Protocol V1 (Big endian)
S1200/1500 and DB unoptimized -> S7Protocol V1 (Big endian)
S1200/1500 and DB optimized    -> S7Protocol V2 (Little endian)
 
That said I could read in your Snap7 manual that the optimized block access must be turned off.
Because Snap7 implements S7Protocol V1 and needs of variables physical address.
Trying to access a variable into an optimized DB iusses a "resource not found" error with Protocol V1
 
but I also read from Siemens documentation and recommendation to use optimized data block for s7-1200 and s7-1500
You use LOGO if I'm not mistaken, which, as server, has a virtual table mapped onto an unoptimized DB1; as client uses GET/PUT functions wich work with Protocol V1 and allow only unoptimized access, so you have no choice, these recommendations make little sense for the communication.
 
 
Reply 1 0
fg71
I was speaking in general if you ask a person who has always used s7-1200 or 1500 they may say Siemens PLC is Little-Endian.
Its only when a person read the documentation we could be sure (so you are correct). I didn't do research enough to claim or though
there was a bug in your software. Siemens LOGO version 8 or (OBA8) support Network Block there data block other and 1 could be access.
Its the variable memory (V) and byte (B), word (W) double word (D) that use data block 1.
 
To be able to use DB2 in LOGO you need to do something like this, the green color means its S7 Client that read/write to S7 Server
So you could do something like this DB2.DBX0.0 or DB2.DBW0 in LOGO
 
[203128%2F9262820%2Fmceclip0]
Reply 0 0
Reply