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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
| '''
pip install snap7 ''' import snap7 from snap7 import util
plc = snap7.client.Client()
plc.connect('192.168.0.1', 0, 1) print("开始发送!")
bool1 = 0 bool2 = 1 bool3 = 0 bool4 = 1 bool5 = 1 bool6 = 0 bool7 = 0 bool8 = 0 boolData = bytearray(1) util.set_bool(boolData, 0, 0, bool(bool1)) util.set_bool(boolData, 0, 1, bool(bool2)) util.set_bool(boolData, 0, 2, bool(bool3)) util.set_bool(boolData, 0, 3, bool(bool4)) util.set_bool(boolData, 0, 4, bool(bool5)) util.set_bool(boolData, 0, 5, bool(bool6)) util.set_bool(boolData, 0, 6, bool(bool7)) util.set_bool(boolData, 0, 7, bool(bool8)) plc.db_write(211, 0, boolData)
intData = bytearray(2) util.set_int(intData, 0, 5) plc.db_write(211, 2, intData)
realData = bytearray(4) util.set_real(realData, 0, 99.9) plc.db_write(211, 4, realData)
string = 'db211' data = int.to_bytes(254, 1, 'big') + int.to_bytes(len(string), 1, 'big') + string.encode(encoding='ascii')
plc.write_area(snap7.client.Areas.DB, 211, 8, data) plc.disconnect() print("发送成功!")
|