my code:
import snap7.client as s7
ip = '192.168.0.7'
rack = 0
slot = 1
data_blok = 100
start_adress = 0
size = 260
try:
plc = s7.Client()
plc.connect(ip, rack, slot)
con = plc.get_connected()
print(f"Bağlantı Durumu: {con}")
db = plc.db_read(data_blok, start_adress, size) //read
name = db[0:256].decode('UTF-8').strip('\x00')
print(f'Data AA: {name}')
value = int.from_bytes(db[256:258], byteorder='big')
print(f'Data BB: {value}')
boll = bool(db[258])
print(f'Data CC: {boll}')
except:
print("hata")
output:
Bağlantı Durumu: True
Data AA: HELLO WORD //string
Data BB: 55 //int
Data CC:True //bool
Process finished with exit code 0 ...
///////////////////////////////////////////
How can I change the integer value here ??
plc.db_write(?????????)
I know that its late but this discussion helped me maybe() it will be helpful for someone else.
You should first use set_int() function from snap7.util to change the integer value in your bytearray as:
This takes your bytearray "db", and writes the integer value "your_int_value" starting from the byte with index 256.
Secondly you should send the modified bytearray "db" back to plc as:
What this does is to write the bytearray "db" to the data block with number "data_blok" starting from the byte with index "start_addres"
Also here are my functions for writing a spesific data type to plc:
Just give the Data Block number to "db_num", byte index for the starting byte for writing to "start_byte", and real, byte, int or boolean value for the corresponding function.