forked from gijzelaerr/python-snap7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboolean.py
More file actions
44 lines (31 loc) · 1.5 KB
/
boolean.py
File metadata and controls
44 lines (31 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
"""
So how to change a bool value?
1) Read out the _bytearray your boolean values are in.
2) In this byte_array you need to find the byte (byteindex)
3) Find the bit in this byte (bitindex) so you can
4) set the correct value with utility function set_bool which does all the hard work.
set_bool(_bytearray = data which you read out before.., byte_index, bool_index, value),
5) write back the changed bytearray back to the PLC.
A round trip wil take 5ms.
The example code
https://github.com/gijzelaerr/python-snap7/blob/master/example/
the minimun amount of data being read or written to a plc is 1 byte.
"""
import snap7
plc = snap7.client.Client()
plc.connect('192.168.200.24', 0, 3)
# In this example boolean in DB 31 at byte 120 and bit 5 is changed. = 120.5
reading = plc.db_read(31, 120, 1) # read 1 byte from db 31 staring from byte 120
snap7.util.set_bool(reading, 0, 5) # set a value of fifth bit
plc.db_write(reading, 31, 120, 1) # write back the bytearray and now the boolean value is changed in the PLC.
# NOTE you could also use the read_area and write_area functions.
# then you can specify an area to read from:
# https://github.com/gijzelaerr/python-snap7/blob/master/snap7/types.py
from snap7.types import areas # noqa: E402
# play with these functions.
plc.read_area(area=areas['MK'], dbnumber=0, start=20, size=2)
data = bytearray()
snap7.util.set_int(data, 0, 127)
plc.write_area(area=areas['MK'], dbnumber=0, start=20, data=data)
# read the client source code!
# and official snap7 documentation