Changeset 159


Ignore:
Timestamp:
Jul 16, 2010, 12:38:38 PM (14 years ago)
Author:
Rick van der Zwet
Message:

Features :-)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • powerbar/serial-npm4000.py

    r158 r159  
    2424opt_address_code = 0xFFFF
    2525opt_baudrate = 19200
     26opt_delay = 0.0
    2627
    2728# Serial connection port status is cached globally to avoid overhead
     
    2930port_status_synced = False
    3031ports_status = None
     32ports_ampere = None
     33# Segment A, B, C
     34grid_status = None
    3135
    3236def make_checksum(command):
     
    195199def action_port_on(port):
    196200  """ Enable port on device """
    197   global ports_status
    198   ports_status = None
     201  global port_status_synced
     202  port_status_synced = False
    199203
    200204  action_login()
     
    203207def action_port_off(port):
    204208  """ Disable port on device """
    205   global ports_status
    206   ports_status = None
     209  global port_status_synced
     210  port_status_synced = False
    207211
    208212  action_login()
     
    210214
    211215def get_ports_status():
    212    global ports_status
     216   global ports_status, port_status_synced, ports_ampere, grid_status
     217   ports_status_synced = False
    213218   # Example port 1 is off
    214219   # d1 28 ff ff fe ff ff
     
    221226   #[01:38:14] Comm: Refresh
    222227   port_array = [False] * 25
     228   ports_ampere = [0] * 25
     229   grid_status = [0] * 3
    223230
    224231   action_login()
     
    228235      if (status_array & (1 << port)) > 0:
    229236        port_array[port+1] = True
     237   # retval[7] is the sensor state?
     238   ports_ampere = [0] + retval[8:16] + retval[17:25] + retval[26:34]
     239   grid_status = [retval[16], retval[25], retval[34]]
    230240
    231241   # Update global state
    232242   ports_status = port_array
     243   port_status_synced = True
     244
    233245   return port_array
     246
    234247
    235248
    236249def get_port_status(port):
    237250   """ Get specific port status """
    238    global ports_status
    239 
    240    if ports_status:
    241      return ports_status[port]
    242    else:
    243      return get_ports_status()[port]
    244 
    245 
     251   global ports_status, port_status_synced
     252
     253   if not port_status_synced:
     254     get_ports_status()
     255   return ports_status[port]
     256
     257
     258
     259def get_port_ampere(port):
     260   """ Get specific port ampere usage """
     261   global ports_ampere, port_status_synced
     262
     263   if not port_status_synced:
     264     get_ports_status()
     265   return ports_ampere[port]
     266
     267
     268
     269def get_grid_status():
     270   """ Get grid ampere usage """
     271   global grid_status, ports_status_synced
     272   
     273   if not port_status_synced:
     274     get_ports_status()
     275   return grid_status
    246276 
    247277
    248 
    249 def runTest():
    250     print "TEST: Start will all port Offline"
    251     doCommand('allPortsOff')
    252 
    253     print "TEST: All On and Off again, in mass execution"
    254     doCommand('allPortsOn',0x01)
    255     doCommand('allPortsOff')
    256 
    257     print "TEST: Enable and disable ports one by one"
    258     for i in range(1,25):
    259         print getPortName(i)
    260         doCommand('portOn', getPortHex(i))
    261         doCommand('portOff', getPortHex(i))
    262 
    263     print "TEST: Send known error"
    264     doCommand('knownError')
    265278
    266279def bool_to_str(boolean, raw=False):
     
    280293Arguments:
    281294  [-h|--help]                   Reading right know
     295  [-a|--ampere]                 Display ampere readings
    282296  [-d|--debug]                  Print extra communication output
    283297  [-r|--raw]                    Status(es) is bits like output
    284   --serialport=                 Serial Port to connect to [%s]
    285   --password=                   Password to use in hex notation [%s]
    286   --addresscode=                Internal device number in hex notation [%s]
     298  --serialport=<path>           Serial Port to connect to [%s]
     299  --password=<hex>              Password to use in hex notation [%s]
     300  --addresscode=<hex>           Internal device number in hex notation [%s]
     301  --delay=<int>                 Delay used between port operations [%s]
     302  --baudrate=<int>              Bautrate used for communication (19200,9600) [%s]
    287303  [-s <port>|--status=<port>]   Current port(s) configuration
    288304  [-t <port>|--toggle=<port>]   Toggle port(s)
     
    294310  --changeaddresscode=          Change addresscode [TODO: Implement]
    295311  --changetimerdelay=           Change internal timer delay [TODO: Implement]
    296   --delay=                      Delay used between port operations [TODO: Implement]
    297   --baudrate=                   Bautrate used for communication (19200,9600) [%s]
     312
     313Note: [TODO: Implement] bit codes are in the source code, feel free to drop me
     314an email <info@rickvanderzwet.nl> if you really to need to be in there.
    298315
    299316Note: <port> has different notations:
     
    301318  Actual value of port          A1,..,A8,B1,..,B8,C1,..,C8
    302319  All ports                     all
    303     """ % (msg, sys.argv[0], opt_serial_port, hex(opt_password), hex(opt_address_code), opt_baudrate)
     320    """ % (msg, sys.argv[0], opt_serial_port, hex(opt_password), hex(opt_address_code), opt_delay, opt_baudrate)
    304321    if exitcode:
    305322        sys.exit(exitcode)   
    306323
    307324def main():
    308     global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate
     325    global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate, opt_delay
    309326    try:
    310327        opts, args = getopt.getopt(sys.argv[1:],
    311             "df:s:t:ro:v",
    312              ["debug", "verbose", "serialport=", "password=",
     328            "adf:s:t:ro:v",
     329             ["ampere", "debug", "delay=", "verbose", "serialport=", "password=",
    313330             "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw="])
    314331    except getopt.GetoptError, err:
     
    318335    opt_action = None
    319336    opt_raw = False
     337    opt_ampere = False
    320338    for o, a in opts:
    321339        debug("%s : %s" % (o, a))
    322         if o in ["-d", "--debug"]:
     340        if o in ["-a", "--ampere"]:
     341            opt_ampere = True
     342        elif o in ["-d", "--debug"]:
    323343            DEBUG = True
     344        elif o in ["--delay"]:
     345            opt_delay = float(a)
    324346        elif o in ["-h", "--help"]:
    325347            usage("",0)
     
    368390    debug("Operating on ports " + str(ports))
    369391
     392    # Open serial port
    370393    ser = serial.Serial(opt_serial_port, opt_baudrate, timeout=5)
    371394    debug(serial)
    372      
    373395
    374396    # Status needs real integers, hack
    375397    for port in ports:
    376398      if opt_action == "status":
    377         print bool_to_str(get_port_status(port),opt_raw),
     399        if opt_raw:
     400          print bool_to_str(get_port_status(port),opt_raw),
     401        else:
     402          ampere_str = ""
     403          if opt_ampere:
     404            ampere_str = "[%s]" % get_port_ampere(port)
     405          print "Port %02i : %s %s" % (port, get_port_status(port), ampere_str)
    378406      elif opt_action == "toggle":
    379407        if get_port_status(port):
     
    387415      else:
    388416          assert False, "Option '%s' invalid" % opt_action
    389 
     417      # Backoff if we need to be patient
     418      time.sleep(opt_delay)
     419      sys.stdout.flush()
     420
     421    # Be nice and close correctly
    390422    ser.close()
     423
     424    if opt_ampere:
     425      if opt_raw:
     426        print " ".join(map(str, get_grid_status()))
     427      else:
     428        print "Grid A: %s" % grid_status[0]
     429        print "Grid B: %s" % grid_status[1]
     430        print "Grid C: %s" % grid_status[2]
    391431
    392432if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.