Changeset 158 for powerbar


Ignore:
Timestamp:
Jul 16, 2010, 11:26:58 AM (14 years ago)
Author:
Rick van der Zwet
Message:

We got a lift off :-)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • powerbar/serial-npm4000.py

    r157 r158  
    1818# Set to true to enable verbose communication aka debugging
    1919DEBUG = False
    20 DEBUG = True
    2120
    2221# Default for options
    2322opt_serial_port = '/dev/ttyUSB0'
    24 opt_password = 0x1245678
     23opt_password = 0x12345678
    2524opt_address_code = 0xFFFF
    2625opt_baudrate = 19200
     
    9897    """ Send raw command to serial device and wait for response """
    9998
    100     print "Going to send: " + hex_to_str(raw_command)
     99    debug("Going to send: " + hex_to_str(raw_command))
    101100    send_line = "".join([chr(item) for item in raw_command])
    102101    ser.write(send_line)
    103102    recv_line = ser.read(response_size)
    104103    recv_command = str_to_hex(recv_line)
    105     print "Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line))
     104    debug("Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line)))
    106105    return(recv_command)
    107106
     
    169168  return [int(hex(number)[2:].zfill(length)[x:x+2],16) for x in range(0,length,2)]
    170169
    171 
    172 
    173 def send_command(action, argument=None):
     170#print hex_to_str(num_to_hex(opt_password))
     171#exit(0)
     172
     173
     174
     175def send_command(action, argument=[]):
    174176  """ Send CRC computed command to serial device and wait for response """
    175177  (command, response_size, timeout) = line[action]
    176   command = command[0:2] + num_to_hex(opt_address_code) + num_to_hex(argument)
     178  if not isinstance(argument, list):
     179    argument = num_to_hex(argument)
     180  command = command[0:2] + num_to_hex(opt_address_code) + argument
    177181  serial.timeout = timeout
    178182  raw_command = command + [make_checksum(command)]
     
    191195def action_port_on(port):
    192196  """ Enable port on device """
     197  global ports_status
     198  ports_status = None
     199
    193200  action_login()
    194   port_status_synced = False
    195   return send_command('port_on', port)
     201  return send_command('port_on', port_to_hex(port))
    196202
    197203def action_port_off(port):
    198204  """ Disable port on device """
     205  global ports_status
     206  ports_status = None
     207
    199208  action_login()
    200   port_status_synced = False
    201   return send_command('port_off', port)
     209  return send_command('port_off', port_to_hex(port))
    202210
    203211def get_ports_status():
     212   global ports_status
    204213   # Example port 1 is off
    205214   # d1 28 ff ff fe ff ff
     
    222231   # Update global state
    223232   ports_status = port_array
    224    port_status_synced = True
    225233   return port_array
    226234
     
    228236def get_port_status(port):
    229237   """ Get specific port status """
    230    if port_status_synced:
     238   global ports_status
     239
     240   if ports_status:
    231241     return ports_status[port]
    232242   else:
     
    254264    doCommand('knownError')
    255265
    256 
     266def bool_to_str(boolean, raw=False):
     267   if not raw:
     268      return str(boolean)
     269   elif boolean:
     270      return "1"
     271   else:
     272      return "0"
     273     
    257274
    258275def usage(msg="",exitcode=None):
     
    264281  [-h|--help]                   Reading right know
    265282  [-d|--debug]                  Print extra communication output
     283  [-r|--raw]                    Status(es) is bits like output
    266284  --serialport=                 Serial Port to connect to [%s]
    267285  --password=                   Password to use in hex notation [%s]
     
    288306
    289307def main():
    290     global ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate
     308    global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate
    291309    try:
    292310        opts, args = getopt.getopt(sys.argv[1:],
    293             "df:s:t:o:v", ["debug","verbose", "serialport=", "password=", "addresscode=","toggle=","off=", "on=", "status=", "buadrate="])
     311            "df:s:t:ro:v",
     312             ["debug", "verbose", "serialport=", "password=",
     313             "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw="])
    294314    except getopt.GetoptError, err:
    295315        usage(str(err),2)
     
    297317    opt_port = None
    298318    opt_action = None
     319    opt_raw = False
    299320    for o, a in opts:
    300321        debug("%s : %s" % (o, a))
     
    320341            opt_action = "off"
    321342            opt_port = a
     343        elif o in ["-r","--raw"]:
     344            opt_raw = True
    322345        elif o in ["-o","--on"]:
    323346            opt_action = "on"
     
    352375    for port in ports:
    353376      if opt_action == "status":
    354         get_port_status(port)
     377        print bool_to_str(get_port_status(port),opt_raw),
    355378      elif opt_action == "toggle":
    356379        if get_port_status(port):
Note: See TracChangeset for help on using the changeset viewer.