Index: /powerbar/serial-npm4000.py
===================================================================
--- /powerbar/serial-npm4000.py	(revision 154)
+++ /powerbar/serial-npm4000.py	(revision 155)
@@ -77,6 +77,5 @@
     recv_command = str_to_hex(recv_line)
     print "Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line))
-    
-    #return(recv_command)
+    return(recv_command)
 
 def send_command(ser, command, response_size=1024):
@@ -116,5 +115,5 @@
 # 55 07 FF FF 12 34 56 78 5A D1 03 FF FF D2 00	
 login_line = [0x55, 0x07, 0xff, 0xff, 0x12, 0x34, 0x56, 0x78]
-refresh_line = [0xd1, 0x03, 0xff, 0xff, 0xd2]
+refresh_line = [0xd1, 0x03, 0xff, 0xff]
 allon_line = [0xb1, 0x03, 0xff, 0xff]
 alloff_line = [0xc1, 0x03, 0xff, 0xff]
@@ -123,4 +122,6 @@
 power_on_interval_125_line = [0xd6, 0x04, 0xff, 0xff, 0xfa, 0x28]
 power_on_interval_05_line = [0xd6, 0x04, 0xff, 0xff, 0x01, 0xd3]
+change_address_code_line = [0x05, 0x00, 0xff, 0xff, 0x00, 0x04]
+modify_password_line = [0xd3, 0x07, 0xff, 0xff, 0x11, 0x11, 0x11, 0x11]
 
 def port_on_cmd(port_number):
@@ -134,11 +135,38 @@
   return line
 
+def address_to_num(address):
+   return (address[0] << 8) ^ address[1]
+
+def num_to_address(npm_number):
+   return [npm_number >> 8, npm_number & 0x00ff]
+
+def bin_reverse(number,width=8):
+  """Little hacking using string logic to binary reverse number"""
+  return int(bin(number)[2:].zfill(width)[::-1],2)
+
+
+def get_port_status(retval):
+   # Example port 1 is off
+   # d1 28 ff ff fe ff ff
+   #             ^^ ^^ ^^
+   port_array = [False] * 25
+   status_array = bin_reverse((bin_reverse(retval[4]) << 16) ^ (bin_reverse(retval[5]) << 8) ^ bin_reverse(retval[6]),24)
+   for port in range(0,24):
+      if (status_array & (1 << port)) > 0:
+        port_array[port+1] = True
+   return port_array
+      
+
+#send_command(ser, login_line, 5)
 # Note: you will need to wait at least timeout * 24 to complete
 #ser.timeout = 13
-#send_raw_command(ser, allon_line, 6)
-#send_raw_command(ser, alloff_line,6)
-#send_raw_command(ser, refresh_line, 42)
-#send_raw_command(ser, power_on_interval_05_line,6)
-#send_raw_command(ser,one_off_line,6)
+#send_command(ser, allon_line, 6)
+#send_command(ser, alloff_line,6)
+
+#send_command(ser, power_on_interval_05_line,6)
+#send_command(ser,port_on_cmd(24),6)
+#send_command(ser,port_off_cmd(24),6)
+#retval = send_command(ser, refresh_line, 42)
+#print get_port_status(retval)
 while True:
   port_number = random.randint(1,24)
@@ -149,75 +177,6 @@
   send_command(ser, port_off_cmd(port_number),6)
   
-
 ser.close()
-
-
-
-
-sys.exit(0)
-
-# XOR all the 8bit values to get a checksum
-def checksum(s):
-    crc = 0
-    for p in range(0, len(s),2):
-        crc ^= int(s[p:p+2], 16)
-    return "%02X" % crc
-
-passwd = 0x12345678
-addr_code = 0xFFFF 
-
-verbose = False
-
-line2code = { 'login'       : (0x5507, 1),
-         'portOn'      : (0xB204, 2),
-         'portOff'     : (0xC204, 2),
-         'allPortsOff' : (0xC103, 6),
-         'allPortsOn'  : (0xB104, 15),
-         'status'      : (0xD103, 2),
-         'knownError'  : (0xFFFF, 1),
-       }
-
-# Socket used troughout the code
-s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-
-def getCode(type):
-    return(line2code[type][0])
-
-def getTimeout(type):
-    return(line2code[type][1])
-
-def dprint(msg):
-    if verbose:
-        print time.strftime('[%Y-%m-%d %H:%M:%S]'), msg
-
-def getPortNumber(name):
-    port_char = name[0]
-    port_number = int(name[1])
-    number = -1
-    if port_char == 'A':
-        number = port_number
-    elif port_char == 'B':
-        number = port_number + 8
-    elif port_char == 'C':
-        number = port_number + 16
-    else:
-        raise ValueError, "Port %s not defined" % name
-
-    return(number)
-
-def getPortName(number):
-    name = "U3"
-    if number <= 8:
-        name = "A%i" % number
-    elif number <= 16:
-        name = "B%i" % (number - 8)
-    elif number <= 24:
-        name = "C%i" % (number - 16)
-    else:
-        raise ValueError, "Port %i not defined" % number
-    return(name)
-
-def getPortHex(number):
-    return(int(getPortName(number),16))
+exit(0)
 
 def generateCommand(type,arg=None):
