- Timestamp:
- Jul 16, 2010, 12:38:38 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
powerbar/serial-npm4000.py
r158 r159 24 24 opt_address_code = 0xFFFF 25 25 opt_baudrate = 19200 26 opt_delay = 0.0 26 27 27 28 # Serial connection port status is cached globally to avoid overhead … … 29 30 port_status_synced = False 30 31 ports_status = None 32 ports_ampere = None 33 # Segment A, B, C 34 grid_status = None 31 35 32 36 def make_checksum(command): … … 195 199 def action_port_on(port): 196 200 """ Enable port on device """ 197 global port s_status198 port s_status = None201 global port_status_synced 202 port_status_synced = False 199 203 200 204 action_login() … … 203 207 def action_port_off(port): 204 208 """ Disable port on device """ 205 global port s_status206 port s_status = None209 global port_status_synced 210 port_status_synced = False 207 211 208 212 action_login() … … 210 214 211 215 def get_ports_status(): 212 global ports_status 216 global ports_status, port_status_synced, ports_ampere, grid_status 217 ports_status_synced = False 213 218 # Example port 1 is off 214 219 # d1 28 ff ff fe ff ff … … 221 226 #[01:38:14] Comm: Refresh 222 227 port_array = [False] * 25 228 ports_ampere = [0] * 25 229 grid_status = [0] * 3 223 230 224 231 action_login() … … 228 235 if (status_array & (1 << port)) > 0: 229 236 port_array[port+1] = True 237 # retval[7] is the sensor state? 238 ports_ampere = [0] + retval[8:16] + retval[17:25] + retval[26:34] 239 grid_status = [retval[16], retval[25], retval[34]] 230 240 231 241 # Update global state 232 242 ports_status = port_array 243 port_status_synced = True 244 233 245 return port_array 246 234 247 235 248 236 249 def get_port_status(port): 237 250 """ Get specific port status """ 238 global ports_status 239 240 if ports_status: 241 return ports_status[port] 242 else: 243 return get_ports_status()[port] 244 245 251 global ports_status, port_status_synced 252 253 if not port_status_synced: 254 get_ports_status() 255 return ports_status[port] 256 257 258 259 def get_port_ampere(port): 260 """ Get specific port ampere usage """ 261 global ports_ampere, port_status_synced 262 263 if not port_status_synced: 264 get_ports_status() 265 return ports_ampere[port] 266 267 268 269 def get_grid_status(): 270 """ Get grid ampere usage """ 271 global grid_status, ports_status_synced 272 273 if not port_status_synced: 274 get_ports_status() 275 return grid_status 246 276 247 277 248 249 def runTest():250 print "TEST: Start will all port Offline"251 doCommand('allPortsOff')252 253 print "TEST: All On and Off again, in mass execution"254 doCommand('allPortsOn',0x01)255 doCommand('allPortsOff')256 257 print "TEST: Enable and disable ports one by one"258 for i in range(1,25):259 print getPortName(i)260 doCommand('portOn', getPortHex(i))261 doCommand('portOff', getPortHex(i))262 263 print "TEST: Send known error"264 doCommand('knownError')265 278 266 279 def bool_to_str(boolean, raw=False): … … 280 293 Arguments: 281 294 [-h|--help] Reading right know 295 [-a|--ampere] Display ampere readings 282 296 [-d|--debug] Print extra communication output 283 297 [-r|--raw] Status(es) is bits like output 284 --serialport= Serial Port to connect to [%s] 285 --password= Password to use in hex notation [%s] 286 --addresscode= Internal device number in hex notation [%s] 298 --serialport=<path> Serial Port to connect to [%s] 299 --password=<hex> Password to use in hex notation [%s] 300 --addresscode=<hex> Internal device number in hex notation [%s] 301 --delay=<int> Delay used between port operations [%s] 302 --baudrate=<int> Bautrate used for communication (19200,9600) [%s] 287 303 [-s <port>|--status=<port>] Current port(s) configuration 288 304 [-t <port>|--toggle=<port>] Toggle port(s) … … 294 310 --changeaddresscode= Change addresscode [TODO: Implement] 295 311 --changetimerdelay= Change internal timer delay [TODO: Implement] 296 --delay= Delay used between port operations [TODO: Implement] 297 --baudrate= Bautrate used for communication (19200,9600) [%s] 312 313 Note: [TODO: Implement] bit codes are in the source code, feel free to drop me 314 an email <info@rickvanderzwet.nl> if you really to need to be in there. 298 315 299 316 Note: <port> has different notations: … … 301 318 Actual value of port A1,..,A8,B1,..,B8,C1,..,C8 302 319 All ports all 303 """ % (msg, sys.argv[0], opt_serial_port, hex(opt_password), hex(opt_address_code), opt_ baudrate)320 """ % (msg, sys.argv[0], opt_serial_port, hex(opt_password), hex(opt_address_code), opt_delay, opt_baudrate) 304 321 if exitcode: 305 322 sys.exit(exitcode) 306 323 307 324 def main(): 308 global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate 325 global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate, opt_delay 309 326 try: 310 327 opts, args = getopt.getopt(sys.argv[1:], 311 " df:s:t:ro:v",312 [" debug", "verbose", "serialport=", "password=",328 "adf:s:t:ro:v", 329 ["ampere", "debug", "delay=", "verbose", "serialport=", "password=", 313 330 "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw="]) 314 331 except getopt.GetoptError, err: … … 318 335 opt_action = None 319 336 opt_raw = False 337 opt_ampere = False 320 338 for o, a in opts: 321 339 debug("%s : %s" % (o, a)) 322 if o in ["-d", "--debug"]: 340 if o in ["-a", "--ampere"]: 341 opt_ampere = True 342 elif o in ["-d", "--debug"]: 323 343 DEBUG = True 344 elif o in ["--delay"]: 345 opt_delay = float(a) 324 346 elif o in ["-h", "--help"]: 325 347 usage("",0) … … 368 390 debug("Operating on ports " + str(ports)) 369 391 392 # Open serial port 370 393 ser = serial.Serial(opt_serial_port, opt_baudrate, timeout=5) 371 394 debug(serial) 372 373 395 374 396 # Status needs real integers, hack 375 397 for port in ports: 376 398 if opt_action == "status": 377 print bool_to_str(get_port_status(port),opt_raw), 399 if opt_raw: 400 print bool_to_str(get_port_status(port),opt_raw), 401 else: 402 ampere_str = "" 403 if opt_ampere: 404 ampere_str = "[%s]" % get_port_ampere(port) 405 print "Port %02i : %s %s" % (port, get_port_status(port), ampere_str) 378 406 elif opt_action == "toggle": 379 407 if get_port_status(port): … … 387 415 else: 388 416 assert False, "Option '%s' invalid" % opt_action 389 417 # Backoff if we need to be patient 418 time.sleep(opt_delay) 419 sys.stdout.flush() 420 421 # Be nice and close correctly 390 422 ser.close() 423 424 if opt_ampere: 425 if opt_raw: 426 print " ".join(map(str, get_grid_status())) 427 else: 428 print "Grid A: %s" % grid_status[0] 429 print "Grid B: %s" % grid_status[1] 430 print "Grid C: %s" % grid_status[2] 391 431 392 432 if __name__ == "__main__":
Note:
See TracChangeset
for help on using the changeset viewer.