Changeset 157 for powerbar


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

More hacks to test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • powerbar/serial-npm4000.py

    r156 r157  
    1818# Set to true to enable verbose communication aka debugging
    1919DEBUG = False
     20DEBUG = True
    2021
    2122# Default for options
     
    2627
    2728# Serial connection port status is cached globally to avoid overhead
    28 serial = None
     29ser = None
    2930port_status_synced = False
    3031ports_status = None
     
    99100    print "Going to send: " + hex_to_str(raw_command)
    100101    send_line = "".join([chr(item) for item in raw_command])
    101     serial.write(send_line)
    102     recv_line = serial.read(response_size)
     102    ser.write(send_line)
     103    recv_line = ser.read(response_size)
    103104    recv_command = str_to_hex(recv_line)
    104105    print "Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line))
     
    166167    return []
    167168  length = len(hex(number)[2:]) + (len(hex(number)[2:]) % 2)
    168   return [hex(number)[2:].zfill(length)[x:x+2] for x in range(0,length,2)]
     169  return [int(hex(number)[2:].zfill(length)[x:x+2],16) for x in range(0,length,2)]
    169170
    170171
     
    173174  """ Send CRC computed command to serial device and wait for response """
    174175  (command, response_size, timeout) = line[action]
    175   command = command[0:1] + num_to_hex(opt_address_code) + num_to_hex(argument)
     176  command = command[0:2] + num_to_hex(opt_address_code) + num_to_hex(argument)
    176177  serial.timeout = timeout
    177178  raw_command = command + [make_checksum(command)]
     
    199200  port_status_synced = False
    200201  return send_command('port_off', port)
    201 
    202202
    203203def get_ports_status():
     
    213213   port_array = [False] * 25
    214214
     215   action_login()
    215216   retval = send_command('status')
    216217   status_array = bin_reverse((bin_reverse(retval[4]) << 16) ^ (bin_reverse(retval[5]) << 8) ^ bin_reverse(retval[6]),24)
     
    287288
    288289def main():
     290    global ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate
    289291    try:
    290292        opts, args = getopt.getopt(sys.argv[1:],
     
    296298    opt_action = None
    297299    for o, a in opts:
    298         if o in ("-d", "--debug"):
     300        debug("%s : %s" % (o, a))
     301        if o in ["-d", "--debug"]:
    299302            DEBUG = True
    300         elif o in ("-h", "--help"):
     303        elif o in ["-h", "--help"]:
    301304            usage("",0)
    302         elif o in ("--addresscode"):
     305        elif o in ["--addresscode"]:
    303306            opt_address_code = int(a,16)
    304         elif o in ("--password"):
     307        elif o in ["--password"]:
    305308            opt_passwd = int(a,16)
    306         elif o in ("--buadrate"):
     309        elif o in ["--buadrate"]:
    307310            opt_baudrate = a
    308         elif o in ("--serialport"):
     311        elif o in ["--serialport"]:
    309312            opt_serial_port = a
    310         elif o in ("-s", "--status"):
     313        elif o in ["-s", "--status"]:
    311314            opt_action = "status"
    312315            opt_port = a
    313         elif o in ("-t","--toggle"):
     316        elif o in ["-t","--toggle"]:
    314317            opt_action = "toggle"
    315318            opt_port = a
    316         elif o in ("-f","--off"):
     319        elif o in ["-f","--off"]:
    317320            opt_action = "off"
    318321            opt_port = a
    319         elif o in ("-o","--on"):
     322        elif o in ["-o","--on"]:
    320323            opt_action = "on"
    321324            opt_port = a
     
    323326            assert False, "unhandled option"
    324327
    325     if (opt_port == None or opt_action == None):
    326         usage("[ERROR] No port or action defined",2)
     328    if (opt_port == None):
     329        usage("[ERROR] No port defined",2)
     330    elif (opt_action == None):
     331        usage("[ERROR] No action defined",2)
    327332
    328333
     
    330335    ports = []
    331336    for port in opt_port.split(','):
     337      debug("Raw port: %s" % port)
    332338      if port == "all":
    333339          ports.extend(range(1,25))
    334       elif port[0] in ("A","B","C"):
    335           ports.extend(int(port,16))
     340      elif port[0] in "ABCabc":
     341          print hex_to_port(int(port,16))
     342          ports.append(hex_to_port(int(port,16)))
    336343      else:
    337           ports.extend(int(port))
    338     debug("Operating on ports " + ",".join(port))
    339 
    340     serial = serial.Serial(opt_serial_port, opt_baudrate, timeout=5)
     344          ports.append(int(port))
     345    debug("Operating on ports " + str(ports))
     346
     347    ser = serial.Serial(opt_serial_port, opt_baudrate, timeout=5)
    341348    debug(serial)
    342349     
     
    345352    for port in ports:
    346353      if opt_action == "status":
    347         action_status(port)
     354        get_port_status(port)
    348355      elif opt_action == "toggle":
    349356        if get_port_status(port):
     
    358365          assert False, "Option '%s' invalid" % opt_action
    359366
    360     serial.close()
     367    ser.close()
    361368
    362369if __name__ == "__main__":
Note: See TracChangeset for help on using the changeset viewer.