refactor: test_snap7.py with CLI args (--station, --product-id, --upper, etc.)
This commit is contained in:
1 parent
9107fb2f4f
commit
81e85944d5
1 file changed
+134
-14
+134
-14
@@ -1,22 +1,142 @@
|
|||||||
|
"""
|
||||||
|
PLC 测试工具 — 读写 S7 DB 块
|
||||||
|
|
||||||
|
用法:
|
||||||
|
# 写入工序 0 完整数据 + 触发 DataReady + 合格
|
||||||
|
python test_snap7.py --station 0 --product-id 10086 --upper 100.0 --lower 80.0 --measured 95.5 --dataready --qualified
|
||||||
|
|
||||||
|
# 只读工序 0 所有字段
|
||||||
|
python test_snap7.py --station 0 --read
|
||||||
|
|
||||||
|
# 仅触发 DataReady (ControlWord=1)
|
||||||
|
python test_snap7.py --station 0 --dataready
|
||||||
|
|
||||||
|
# 触发 DataReady + 合格 (ControlWord=3)
|
||||||
|
python test_snap7.py --station 0 --dataready --qualified
|
||||||
|
|
||||||
|
# 清 ControlWord (写 0)
|
||||||
|
python test_snap7.py --station 0 --value 0
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import struct
|
||||||
import snap7
|
import snap7
|
||||||
|
|
||||||
plc = snap7.client.Client()
|
STATION_SIZE = 24
|
||||||
plc.connect('192.168.2.125', 0, 2)
|
|
||||||
|
|
||||||
print(f"PLC 连接状态: {plc.get_connected()}")
|
def parse_args():
|
||||||
|
p = argparse.ArgumentParser(description='PLC 读写测试工具')
|
||||||
|
p.add_argument('--ip', default='192.168.2.125', help='PLC IP')
|
||||||
|
p.add_argument('--rack', type=int, default=0, help='机架号')
|
||||||
|
p.add_argument('--slot', type=int, default=2, help='槽号')
|
||||||
|
p.add_argument('--db', type=int, default=1, help='DB 块号')
|
||||||
|
p.add_argument('--station', type=int, default=0, help='工序索引 (0-based)')
|
||||||
|
|
||||||
db_number = 1
|
p.add_argument('--read', action='store_true', help='读取本工序所有字段')
|
||||||
start_byte = 0
|
|
||||||
byteoffset = 4
|
p.add_argument('--product-id', type=int, help='写入 ProductID (DINT, 偏移+0)')
|
||||||
|
p.add_argument('--upper', type=float, help='写入 UpperLimit (REAL, 偏移+4)')
|
||||||
|
p.add_argument('--lower', type=float, help='写入 LowerLimit (REAL, 偏移+8)')
|
||||||
|
p.add_argument('--measured', type=float, help='写入 MeasuredValue (REAL, 偏移+12)')
|
||||||
|
p.add_argument('--dataready', action='store_true', help='设置 ControlWord Bit0=1')
|
||||||
|
p.add_argument('--qualified', action='store_true', help='设置 ControlWord Bit1=1')
|
||||||
|
p.add_argument('--value', type=int, help='直接写入 ControlWord WORD 值 (覆盖 --dataready/--qualified)')
|
||||||
|
return p.parse_args()
|
||||||
|
|
||||||
|
|
||||||
data = bytearray(2)
|
def station_base(station: int) -> int:
|
||||||
new_value = 3
|
return station * STATION_SIZE
|
||||||
snap7.util.set_int(data, 0, new_value) # 修改字节数组
|
|
||||||
plc.db_write(db_number, start_byte+byteoffset, data) # 写回PLC
|
|
||||||
|
|
||||||
data = plc.db_read(db_number, start_byte+byteoffset, 2)
|
|
||||||
value = snap7.util.get_int(data, 0)
|
|
||||||
print(value)
|
|
||||||
|
|
||||||
plc.disconnect()
|
def read_fields(plc, db: int, base: int) -> dict:
|
||||||
|
raw = plc.db_read(db, base, STATION_SIZE)
|
||||||
|
return {
|
||||||
|
'ProductID': struct.unpack_from('>i', raw, 0)[0],
|
||||||
|
'UpperLimit': struct.unpack_from('>f', raw, 4)[0],
|
||||||
|
'LowerLimit': struct.unpack_from('>f', raw, 8)[0],
|
||||||
|
'MeasuredValue': struct.unpack_from('>f', raw, 12)[0],
|
||||||
|
'ControlWord': struct.unpack_from('>H', raw, 16)[0],
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def write_dint(plc, db: int, offset: int, value: int):
|
||||||
|
plc.db_write(db, offset, struct.pack('>i', value))
|
||||||
|
|
||||||
|
|
||||||
|
def write_real(plc, db: int, offset: int, value: float):
|
||||||
|
plc.db_write(db, offset, struct.pack('>f', value))
|
||||||
|
|
||||||
|
|
||||||
|
def write_word(plc, db: int, offset: int, value: int):
|
||||||
|
plc.db_write(db, offset, struct.pack('>H', value))
|
||||||
|
|
||||||
|
|
||||||
|
def fmt_fields(fields: dict) -> str:
|
||||||
|
cw = fields['ControlWord']
|
||||||
|
bits = f'(b{cw:016b}) DataReady={cw & 1} 合格={(cw>>1)&1} Req={(cw>>2)&1} 结果={(cw>>3)&1}'
|
||||||
|
return (
|
||||||
|
f" ProductID = {fields['ProductID']}\n"
|
||||||
|
f" UpperLimit = {fields['UpperLimit']}\n"
|
||||||
|
f" LowerLimit = {fields['LowerLimit']}\n"
|
||||||
|
f" MeasuredValue = {fields['MeasuredValue']}\n"
|
||||||
|
f" ControlWord = 0x{cw:04X} {bits}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def main():
|
||||||
|
args = parse_args()
|
||||||
|
base = station_base(args.station)
|
||||||
|
|
||||||
|
plc = snap7.client.Client()
|
||||||
|
plc.connect(args.ip, args.rack, args.slot)
|
||||||
|
print(f"[PLC] 已连接 {args.ip} DB{args.db} 工序{args.station} (偏移{base})")
|
||||||
|
|
||||||
|
any_write = False
|
||||||
|
|
||||||
|
if args.product_id is not None:
|
||||||
|
write_dint(plc, args.db, base + 0, args.product_id)
|
||||||
|
print(f"[写] ProductID@{base+0} = {args.product_id}")
|
||||||
|
any_write = True
|
||||||
|
|
||||||
|
if args.upper is not None:
|
||||||
|
write_real(plc, args.db, base + 4, args.upper)
|
||||||
|
print(f"[写] UpperLimit@{base+4} = {args.upper}")
|
||||||
|
any_write = True
|
||||||
|
|
||||||
|
if args.lower is not None:
|
||||||
|
write_real(plc, args.db, base + 8, args.lower)
|
||||||
|
print(f"[写] LowerLimit@{base+8} = {args.lower}")
|
||||||
|
any_write = True
|
||||||
|
|
||||||
|
if args.measured is not None:
|
||||||
|
write_real(plc, args.db, base + 12, args.measured)
|
||||||
|
print(f"[写] MeasuredValue@{base+12} = {args.measured}")
|
||||||
|
any_write = True
|
||||||
|
|
||||||
|
if args.value is not None:
|
||||||
|
write_word(plc, args.db, base + 16, args.value)
|
||||||
|
print(f"[写] ControlWord@{base+16} = 0x{args.value:04X}")
|
||||||
|
any_write = True
|
||||||
|
elif args.dataready or args.qualified:
|
||||||
|
cw = read_fields(plc, args.db, base)['ControlWord']
|
||||||
|
if args.dataready:
|
||||||
|
cw |= 0x0001
|
||||||
|
if args.qualified:
|
||||||
|
cw |= 0x0002
|
||||||
|
write_word(plc, args.db, base + 16, cw)
|
||||||
|
print(f"[写] ControlWord@{base+16} = 0x{cw:04X} (DataReady={args.dataready} 合格={args.qualified})")
|
||||||
|
any_write = True
|
||||||
|
|
||||||
|
if any_write:
|
||||||
|
print()
|
||||||
|
|
||||||
|
fields = read_fields(plc, args.db, base)
|
||||||
|
print(f"[读] 工序{args.station} (偏移{base}~{base+STATION_SIZE-1}):")
|
||||||
|
print(fmt_fields(fields))
|
||||||
|
|
||||||
|
plc.disconnect()
|
||||||
|
print("[PLC] 已断开")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main()
|
||||||
Reference in new issue
Block a user