Newer
Older
SCADA / modbus / set_coil.py
root on 8 May 2022 1 KB playing with modbus day #1
  1. #!/usr/bin/env python
  2.  
  3. """
  4. File: set_coil.py
  5. Desc: set a specific coil to a specific value
  6. """
  7. __author__ = '0xRoM'
  8. from argparse import ArgumentParser, ArgumentTypeError
  9. from pyModbusTCP.client import ModbusClient
  10. import time
  11. import sys
  12.  
  13. parser = ArgumentParser()
  14. parser.add_argument('-i','--ipaddress', action='store', type=str, required=True, help='Input IP Address')
  15. parser.add_argument('-p','--port', action='store', type=int, required=False, help='Port Number', default=502)
  16. parser.add_argument('-c','--coil', action='store', type=int, required=True, help='Coil Number', default=0)
  17. parser.add_argument('-tr','--true', action='store_true', required=False, help='True if set')
  18. parser.add_argument('-l','--loop', action='store_true', required=False, help='loop on')
  19. parser.add_argument('-t','--timeout', action='store', type=float, required=False, help='request every X seconds', default=2)
  20. args = parser.parse_args()
  21.  
  22. client=ModbusClient(host=args.ipaddress,port=args.port,auto_open=True,auto_close=True,timeout=10)
  23. if args.true:
  24. client.write_single_coil(args.coil,True)
  25. else:
  26. client.write_single_coil(args.coil,False)
  27.  
  28. if args.loop:
  29. i=1
  30. while True:
  31. client=ModbusClient(host=args.ipaddress,port=args.port,auto_open=True,auto_close=True,timeout=10)
  32. if args.true:
  33. client.write_single_coil(args.coil,True)
  34. else:
  35. client.write_single_coil(args.coil,False)
  36. client.close()
  37. print '\r>>packet: %d' % i,
  38. sys.stdout.flush()
  39. i+=1
  40. time.sleep(args.timeout)
  41.  
Buy Me A Coffee