I'm not sure I understand how snap7 is working or how I could use Snap7 Client to set GPIO on RPi.
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 ??
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
Little confusing when this, change from 02 to 03 when all other go from 00 to 01
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()