Advertisement
ayush3504

modbus usb powerstrip script

Sep 16th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import os, sys, minimalmodbus, argparse
  4.  
  5. # choose no. of switch in the given power strip model
  6. switchNum=6
  7.  
  8. # parse command line paramenters
  9. parser = argparse.ArgumentParser(description=\
  10.                                  'Modbus master for Modbus USB Power Strip. \
  11.                                 Reads and writes to specified switch relay coils')
  12. parser.add_argument('devicePath', help='Device path e.g. /dev/ttyUSB0')
  13. parser.add_argument('switchId', type=int, choices=range(switchNum+1), help='Select switch number 1-' + str(switchNum) + ' on the power strip. Switch number 0 is used to select all switches for turn_on or turn_off operations')
  14. parser.add_argument('operation', choices=['read','turn_on', 'turn_off'], help='Performs given operation on the chosen switch')                
  15. args = parser.parse_args()
  16.  
  17. def main(devicePath, switchId, operation):
  18.     # to ensure that the port closes after each call
  19.     minimalmodbus.CLOSE_PORT_AFTER_EACH_CALL=True
  20.    
  21.     # establish connection
  22.     try:
  23.         # resolve symbolic links, if any, in the given device path
  24.         powerStripCanonicalPath = os.path.realpath(devicePath)
  25.         # create an instance of modbus slave instrument called powerStrip
  26.         powerStrip = minimalmodbus.Instrument(powerStripCanonicalPath, 1) # arguments: port name, slave address (in decimal)
  27.         # set baud rate
  28.         powerStrip.serial.baudrate = 9600
  29.         #powerStrip.debug = True  # to see communication with the instrument
  30.     except:
  31.         print 'Connection failed'
  32.         sys.exit(1)
  33.  
  34.     # set switch address since addressing starts from 0
  35.     switchAddr = switchId - 1
  36.  
  37.     # execute given operation
  38.     try:
  39.         if operation=='read':
  40.             # check for valid switchId to perform read operation
  41.             if switchId > 0:
  42.                 print 'Switch', switchId,
  43.                 if powerStrip.read_bit(switchAddr, 1) == 1: # read_bit(coil address, modbus function code)
  44.                     print 'is on'
  45.                 elif powerStrip.read_bit(switchAddr, 1) == 0:
  46.                     print 'is off'
  47.             else:
  48.                 print 'Read not supported on all switches'
  49.                 raise
  50.         else:
  51.             if switchId > 0:
  52.                 # to perform turn_on, turn_off operations on given switch
  53.                 if operation == 'turn_on':
  54.                     powerStrip.write_bit(switchAddr, 1, 5) # write_bit(coil address, value, modbus function code)
  55.                 if operation == 'turn_off':
  56.                     powerStrip.write_bit(switchAddr, 0, 5) # write_bit(coil address, value, modbus function code)
  57.                 # to perform operation on all switch
  58.             elif switchId == 0:
  59.                 for currSwitchAddr in range(switchNum):
  60.                     powerStrip.write_bit(currSwitchAddr, operation == 'turn_on', 5) # write_bit(coil address, value, modbus function code)
  61.     except:
  62.         print 'Operation failed'
  63.         sys.exit(2)
  64. if __name__ == "__main__":
  65.     main(args.devicePath, args.switchId, args.operation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement