Changeset 158
- Timestamp:
- Jul 16, 2010, 11:26:58 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
powerbar/serial-npm4000.py
r157 r158 18 18 # Set to true to enable verbose communication aka debugging 19 19 DEBUG = False 20 DEBUG = True21 20 22 21 # Default for options 23 22 opt_serial_port = '/dev/ttyUSB0' 24 opt_password = 0x12 4567823 opt_password = 0x12345678 25 24 opt_address_code = 0xFFFF 26 25 opt_baudrate = 19200 … … 98 97 """ Send raw command to serial device and wait for response """ 99 98 100 print "Going to send: " + hex_to_str(raw_command)99 debug("Going to send: " + hex_to_str(raw_command)) 101 100 send_line = "".join([chr(item) for item in raw_command]) 102 101 ser.write(send_line) 103 102 recv_line = ser.read(response_size) 104 103 recv_command = str_to_hex(recv_line) 105 print "Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line))104 debug("Received: %s (%i)" % (hex_to_str(recv_command), len(recv_line))) 106 105 return(recv_command) 107 106 … … 169 168 return [int(hex(number)[2:].zfill(length)[x:x+2],16) for x in range(0,length,2)] 170 169 171 172 173 def send_command(action, argument=None): 170 #print hex_to_str(num_to_hex(opt_password)) 171 #exit(0) 172 173 174 175 def send_command(action, argument=[]): 174 176 """ Send CRC computed command to serial device and wait for response """ 175 177 (command, response_size, timeout) = line[action] 176 command = command[0:2] + num_to_hex(opt_address_code) + num_to_hex(argument) 178 if not isinstance(argument, list): 179 argument = num_to_hex(argument) 180 command = command[0:2] + num_to_hex(opt_address_code) + argument 177 181 serial.timeout = timeout 178 182 raw_command = command + [make_checksum(command)] … … 191 195 def action_port_on(port): 192 196 """ Enable port on device """ 197 global ports_status 198 ports_status = None 199 193 200 action_login() 194 port_status_synced = False 195 return send_command('port_on', port) 201 return send_command('port_on', port_to_hex(port)) 196 202 197 203 def action_port_off(port): 198 204 """ Disable port on device """ 205 global ports_status 206 ports_status = None 207 199 208 action_login() 200 port_status_synced = False 201 return send_command('port_off', port) 209 return send_command('port_off', port_to_hex(port)) 202 210 203 211 def get_ports_status(): 212 global ports_status 204 213 # Example port 1 is off 205 214 # d1 28 ff ff fe ff ff … … 222 231 # Update global state 223 232 ports_status = port_array 224 port_status_synced = True225 233 return port_array 226 234 … … 228 236 def get_port_status(port): 229 237 """ Get specific port status """ 230 if port_status_synced: 238 global ports_status 239 240 if ports_status: 231 241 return ports_status[port] 232 242 else: … … 254 264 doCommand('knownError') 255 265 256 266 def bool_to_str(boolean, raw=False): 267 if not raw: 268 return str(boolean) 269 elif boolean: 270 return "1" 271 else: 272 return "0" 273 257 274 258 275 def usage(msg="",exitcode=None): … … 264 281 [-h|--help] Reading right know 265 282 [-d|--debug] Print extra communication output 283 [-r|--raw] Status(es) is bits like output 266 284 --serialport= Serial Port to connect to [%s] 267 285 --password= Password to use in hex notation [%s] … … 288 306 289 307 def main(): 290 global ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate308 global DEBUG, ser, opt_serial_port, opt_password, opt_address_code, opt_baudrate 291 309 try: 292 310 opts, args = getopt.getopt(sys.argv[1:], 293 "df:s:t:o:v", ["debug","verbose", "serialport=", "password=", "addresscode=","toggle=","off=", "on=", "status=", "buadrate="]) 311 "df:s:t:ro:v", 312 ["debug", "verbose", "serialport=", "password=", 313 "addresscode=","toggle=","off=", "on=", "status=", "buadrate=", "raw="]) 294 314 except getopt.GetoptError, err: 295 315 usage(str(err),2) … … 297 317 opt_port = None 298 318 opt_action = None 319 opt_raw = False 299 320 for o, a in opts: 300 321 debug("%s : %s" % (o, a)) … … 320 341 opt_action = "off" 321 342 opt_port = a 343 elif o in ["-r","--raw"]: 344 opt_raw = True 322 345 elif o in ["-o","--on"]: 323 346 opt_action = "on" … … 352 375 for port in ports: 353 376 if opt_action == "status": 354 get_port_status(port)377 print bool_to_str(get_port_status(port),opt_raw), 355 378 elif opt_action == "toggle": 356 379 if get_port_status(port):
Note:
See TracChangeset
for help on using the changeset viewer.