Index: /powerbar/serial-npm4000.py
===================================================================
--- /powerbar/serial-npm4000.py	(revision 156)
+++ /powerbar/serial-npm4000.py	(revision 157)
@@ -18,4 +18,5 @@
 # Set to true to enable verbose communication aka debugging
 DEBUG = False
+DEBUG = True
 
 # Default for options
@@ -26,5 +27,5 @@
 
 # Serial connection port status is cached globally to avoid overhead
-serial = None
+ser = None
 port_status_synced = False
 ports_status = None
@@ -99,6 +100,6 @@
     print "Going to send: " + hex_to_str(raw_command)
     send_line = "".join([chr(item) for item in raw_command])
-    serial.write(send_line)
-    recv_line = serial.read(response_size)
+    ser.write(send_line)
+    recv_line = ser.read(response_size)
     recv_command = str_to_hex(recv_line)
     print "Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line))
@@ -166,5 +167,5 @@
     return []
   length = len(hex(number)[2:]) + (len(hex(number)[2:]) % 2)
-  return [hex(number)[2:].zfill(length)[x:x+2] for x in range(0,length,2)]
+  return [int(hex(number)[2:].zfill(length)[x:x+2],16) for x in range(0,length,2)]
 
 
@@ -173,5 +174,5 @@
   """ Send CRC computed command to serial device and wait for response """
   (command, response_size, timeout) = line[action]
-  command = command[0:1] + num_to_hex(opt_address_code) + num_to_hex(argument)
+  command = command[0:2] + num_to_hex(opt_address_code) + num_to_hex(argument)
   serial.timeout = timeout
   raw_command = command + [make_checksum(command)]
@@ -199,5 +200,4 @@
   port_status_synced = False
   return send_command('port_off', port)
-
 
 def get_ports_status():
@@ -213,4 +213,5 @@
    port_array = [False] * 25
 
+   action_login()
    retval = send_command('status')
    status_array = bin_reverse((bin_reverse(retval[4]) << 16) ^ (bin_reverse(retval[5]) << 8) ^ bin_reverse(retval[6]),24)
@@ -287,4 +288,5 @@
 
 def main():
+    global ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate
     try:
         opts, args = getopt.getopt(sys.argv[1:], 
@@ -296,26 +298,27 @@
     opt_action = None
     for o, a in opts:
-        if o in ("-d", "--debug"):
+        debug("%s : %s" % (o, a))
+        if o in ["-d", "--debug"]:
             DEBUG = True
-        elif o in ("-h", "--help"):
+        elif o in ["-h", "--help"]:
             usage("",0)
-        elif o in ("--addresscode"):
+        elif o in ["--addresscode"]:
             opt_address_code = int(a,16)
-        elif o in ("--password"):
+        elif o in ["--password"]:
             opt_passwd = int(a,16)
-        elif o in ("--buadrate"):
+        elif o in ["--buadrate"]:
             opt_baudrate = a
-        elif o in ("--serialport"):
+        elif o in ["--serialport"]:
             opt_serial_port = a
-        elif o in ("-s", "--status"):
+        elif o in ["-s", "--status"]:
             opt_action = "status"
             opt_port = a
-        elif o in ("-t","--toggle"):
+        elif o in ["-t","--toggle"]:
             opt_action = "toggle"
             opt_port = a
-        elif o in ("-f","--off"):
+        elif o in ["-f","--off"]:
             opt_action = "off"
             opt_port = a
-        elif o in ("-o","--on"):
+        elif o in ["-o","--on"]:
             opt_action = "on"
             opt_port = a
@@ -323,6 +326,8 @@
             assert False, "unhandled option"
 
-    if (opt_port == None or opt_action == None):
-        usage("[ERROR] No port or action defined",2)
+    if (opt_port == None):
+        usage("[ERROR] No port defined",2)
+    elif (opt_action == None):
+        usage("[ERROR] No action defined",2)
 
 
@@ -330,13 +335,15 @@
     ports = []
     for port in opt_port.split(','):
+      debug("Raw port: %s" % port)
       if port == "all":
           ports.extend(range(1,25))
-      elif port[0] in ("A","B","C"):
-          ports.extend(int(port,16))
+      elif port[0] in "ABCabc":
+          print hex_to_port(int(port,16))
+          ports.append(hex_to_port(int(port,16)))
       else:
-          ports.extend(int(port))
-    debug("Operating on ports " + ",".join(port))
-
-    serial = serial.Serial(opt_serial_port, opt_baudrate, timeout=5)
+          ports.append(int(port))
+    debug("Operating on ports " + str(ports))
+
+    ser = serial.Serial(opt_serial_port, opt_baudrate, timeout=5)
     debug(serial)
       
@@ -345,5 +352,5 @@
     for port in ports:
       if opt_action == "status":
-        action_status(port)
+        get_port_status(port)
       elif opt_action == "toggle":
         if get_port_status(port):
@@ -358,5 +365,5 @@
           assert False, "Option '%s' invalid" % opt_action
 
-    serial.close()
+    ser.close()
 
 if __name__ == "__main__":
