Changeset 155
- Timestamp:
- Jul 16, 2010, 1:59:20 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
powerbar/serial-npm4000.py
r154 r155 77 77 recv_command = str_to_hex(recv_line) 78 78 print "Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line)) 79 80 #return(recv_command) 79 return(recv_command) 81 80 82 81 def send_command(ser, command, response_size=1024): … … 116 115 # 55 07 FF FF 12 34 56 78 5A D1 03 FF FF D2 00 117 116 login_line = [0x55, 0x07, 0xff, 0xff, 0x12, 0x34, 0x56, 0x78] 118 refresh_line = [0xd1, 0x03, 0xff, 0xff , 0xd2]117 refresh_line = [0xd1, 0x03, 0xff, 0xff] 119 118 allon_line = [0xb1, 0x03, 0xff, 0xff] 120 119 alloff_line = [0xc1, 0x03, 0xff, 0xff] … … 123 122 power_on_interval_125_line = [0xd6, 0x04, 0xff, 0xff, 0xfa, 0x28] 124 123 power_on_interval_05_line = [0xd6, 0x04, 0xff, 0xff, 0x01, 0xd3] 124 change_address_code_line = [0x05, 0x00, 0xff, 0xff, 0x00, 0x04] 125 modify_password_line = [0xd3, 0x07, 0xff, 0xff, 0x11, 0x11, 0x11, 0x11] 125 126 126 127 def port_on_cmd(port_number): … … 134 135 return line 135 136 137 def address_to_num(address): 138 return (address[0] << 8) ^ address[1] 139 140 def num_to_address(npm_number): 141 return [npm_number >> 8, npm_number & 0x00ff] 142 143 def bin_reverse(number,width=8): 144 """Little hacking using string logic to binary reverse number""" 145 return int(bin(number)[2:].zfill(width)[::-1],2) 146 147 148 def get_port_status(retval): 149 # Example port 1 is off 150 # d1 28 ff ff fe ff ff 151 # ^^ ^^ ^^ 152 port_array = [False] * 25 153 status_array = bin_reverse((bin_reverse(retval[4]) << 16) ^ (bin_reverse(retval[5]) << 8) ^ bin_reverse(retval[6]),24) 154 for port in range(0,24): 155 if (status_array & (1 << port)) > 0: 156 port_array[port+1] = True 157 return port_array 158 159 160 #send_command(ser, login_line, 5) 136 161 # Note: you will need to wait at least timeout * 24 to complete 137 162 #ser.timeout = 13 138 #send_raw_command(ser, allon_line, 6) 139 #send_raw_command(ser, alloff_line,6) 140 #send_raw_command(ser, refresh_line, 42) 141 #send_raw_command(ser, power_on_interval_05_line,6) 142 #send_raw_command(ser,one_off_line,6) 163 #send_command(ser, allon_line, 6) 164 #send_command(ser, alloff_line,6) 165 166 #send_command(ser, power_on_interval_05_line,6) 167 #send_command(ser,port_on_cmd(24),6) 168 #send_command(ser,port_off_cmd(24),6) 169 #retval = send_command(ser, refresh_line, 42) 170 #print get_port_status(retval) 143 171 while True: 144 172 port_number = random.randint(1,24) … … 149 177 send_command(ser, port_off_cmd(port_number),6) 150 178 151 152 179 ser.close() 153 154 155 156 157 sys.exit(0) 158 159 # XOR all the 8bit values to get a checksum 160 def checksum(s): 161 crc = 0 162 for p in range(0, len(s),2): 163 crc ^= int(s[p:p+2], 16) 164 return "%02X" % crc 165 166 passwd = 0x12345678 167 addr_code = 0xFFFF 168 169 verbose = False 170 171 line2code = { 'login' : (0x5507, 1), 172 'portOn' : (0xB204, 2), 173 'portOff' : (0xC204, 2), 174 'allPortsOff' : (0xC103, 6), 175 'allPortsOn' : (0xB104, 15), 176 'status' : (0xD103, 2), 177 'knownError' : (0xFFFF, 1), 178 } 179 180 # Socket used troughout the code 181 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 182 183 def getCode(type): 184 return(line2code[type][0]) 185 186 def getTimeout(type): 187 return(line2code[type][1]) 188 189 def dprint(msg): 190 if verbose: 191 print time.strftime('[%Y-%m-%d %H:%M:%S]'), msg 192 193 def getPortNumber(name): 194 port_char = name[0] 195 port_number = int(name[1]) 196 number = -1 197 if port_char == 'A': 198 number = port_number 199 elif port_char == 'B': 200 number = port_number + 8 201 elif port_char == 'C': 202 number = port_number + 16 203 else: 204 raise ValueError, "Port %s not defined" % name 205 206 return(number) 207 208 def getPortName(number): 209 name = "U3" 210 if number <= 8: 211 name = "A%i" % number 212 elif number <= 16: 213 name = "B%i" % (number - 8) 214 elif number <= 24: 215 name = "C%i" % (number - 16) 216 else: 217 raise ValueError, "Port %i not defined" % number 218 return(name) 219 220 def getPortHex(number): 221 return(int(getPortName(number),16)) 180 exit(0) 222 181 223 182 def generateCommand(type,arg=None):
Note:
See TracChangeset
for help on using the changeset viewer.