Changeset 157
- Timestamp:
- Jul 16, 2010, 10:58:02 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
powerbar/serial-npm4000.py
r156 r157 18 18 # Set to true to enable verbose communication aka debugging 19 19 DEBUG = False 20 DEBUG = True 20 21 21 22 # Default for options … … 26 27 27 28 # Serial connection port status is cached globally to avoid overhead 28 ser ial= None29 ser = None 29 30 port_status_synced = False 30 31 ports_status = None … … 99 100 print "Going to send: " + hex_to_str(raw_command) 100 101 send_line = "".join([chr(item) for item in raw_command]) 101 ser ial.write(send_line)102 recv_line = ser ial.read(response_size)102 ser.write(send_line) 103 recv_line = ser.read(response_size) 103 104 recv_command = str_to_hex(recv_line) 104 105 print "Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line)) … … 166 167 return [] 167 168 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)] 169 170 170 171 … … 173 174 """ Send CRC computed command to serial device and wait for response """ 174 175 (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) 176 177 serial.timeout = timeout 177 178 raw_command = command + [make_checksum(command)] … … 199 200 port_status_synced = False 200 201 return send_command('port_off', port) 201 202 202 203 203 def get_ports_status(): … … 213 213 port_array = [False] * 25 214 214 215 action_login() 215 216 retval = send_command('status') 216 217 status_array = bin_reverse((bin_reverse(retval[4]) << 16) ^ (bin_reverse(retval[5]) << 8) ^ bin_reverse(retval[6]),24) … … 287 288 288 289 def main(): 290 global ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate 289 291 try: 290 292 opts, args = getopt.getopt(sys.argv[1:], … … 296 298 opt_action = None 297 299 for o, a in opts: 298 if o in ("-d", "--debug"): 300 debug("%s : %s" % (o, a)) 301 if o in ["-d", "--debug"]: 299 302 DEBUG = True 300 elif o in ("-h", "--help"):303 elif o in ["-h", "--help"]: 301 304 usage("",0) 302 elif o in ("--addresscode"):305 elif o in ["--addresscode"]: 303 306 opt_address_code = int(a,16) 304 elif o in ("--password"):307 elif o in ["--password"]: 305 308 opt_passwd = int(a,16) 306 elif o in ("--buadrate"):309 elif o in ["--buadrate"]: 307 310 opt_baudrate = a 308 elif o in ("--serialport"):311 elif o in ["--serialport"]: 309 312 opt_serial_port = a 310 elif o in ("-s", "--status"):313 elif o in ["-s", "--status"]: 311 314 opt_action = "status" 312 315 opt_port = a 313 elif o in ("-t","--toggle"):316 elif o in ["-t","--toggle"]: 314 317 opt_action = "toggle" 315 318 opt_port = a 316 elif o in ("-f","--off"):319 elif o in ["-f","--off"]: 317 320 opt_action = "off" 318 321 opt_port = a 319 elif o in ("-o","--on"):322 elif o in ["-o","--on"]: 320 323 opt_action = "on" 321 324 opt_port = a … … 323 326 assert False, "unhandled option" 324 327 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) 327 332 328 333 … … 330 335 ports = [] 331 336 for port in opt_port.split(','): 337 debug("Raw port: %s" % port) 332 338 if port == "all": 333 339 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))) 336 343 else: 337 ports. extend(int(port))338 debug("Operating on ports " + ",".join(port))339 340 ser ial= 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) 341 348 debug(serial) 342 349 … … 345 352 for port in ports: 346 353 if opt_action == "status": 347 action_status(port)354 get_port_status(port) 348 355 elif opt_action == "toggle": 349 356 if get_port_status(port): … … 358 365 assert False, "Option '%s' invalid" % opt_action 359 366 360 ser ial.close()367 ser.close() 361 368 362 369 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.