You cannot write a single bit directly to a PLC. To change a bit (BOOL), you must follow a read-modify-write pattern:
- Read the entire byte containing the bit.
- Use
util.set_bool to modify the bit in the local bytearray. - Write the entire byte back to the PLC.
Warning: Never write a freshly created bytearray for booleans, as this will overwrite all other bits in that byte with zeros.
from s7 import util
# Read DB1.DBX0.3 (bit 3 of byte 0)
data = client.db_read(1, 0, 1)
value = util.get_bool(data, 0, 3)
print(f"DB1.DBX0.3 = {value}")
# Write DB1.DBX0.3 -- read first, then modify, then write
data = client.db_read(1, 0, 1)
util.set_bool(data, 0, 3, True)
client.db_write(1, 0, data)