def __init__(self, comm_port="/dev/ttyUSB0"): self.TIME = time.strftime('%Y%m%d%H%M%S') self.DEBUG = True self.COMM_PORT = comm_port self.PASSWD = ('\x20' * 20) self.USER_STR = ('\x53\x4d\x41\x43\x4B' + '\x20' * 5) # SMACK self.USER_NUM = 2 self.INVERT = 0 self.NEGO_ON = True # Initialize Comm Port self.SER_CONN0 = c12serial.SERCONN(debug=self.DEBUG) self.SER_CONN0.serInit(port=self.COMM_PORT, invert=self.INVERT) # Setup and configure packet self.packet = self.config_packet() self.packet.set_debug(self.DEBUG) self.packet.set_nego(self.NEGO_ON) if self.DEBUG: print 'Debug:', self.DEBUG if self.DEBUG: print 'comm_port:', self.COMM_PORT if self.DEBUG: print 'passwd:', bt.print_data(self.PASSWD) if self.DEBUG: print 'user_str:', bt.print_data(self.USER_STR) if self.DEBUG: print 'user_num:', self.USER_NUM if self.DEBUG: print 'Negotiation:', self.NEGO_ON if self.DEBUG: print 'Invert:', self.INVERT
def __init__(self, comm_port = "/dev/ttyUSB0"): self.TIME = time.strftime('%Y%m%d%H%M%S') self.DEBUG = True self.COMM_PORT = comm_port self.PASSWD = ('\x20' * 20) self.USER_STR = ('\x53\x4d\x41\x43\x4B' + '\x20' * 5) # SMACK self.USER_NUM = 2 self.INVERT = 0 self.NEGO_ON = True # Initialize Comm Port self.SER_CONN0 = c12serial.SERCONN(debug = self.DEBUG) self.SER_CONN0.serInit(port = self.COMM_PORT, invert = self.INVERT) # Setup and configure packet self.packet = self.config_packet() self.packet.set_debug(self.DEBUG) self.packet.set_nego(self.NEGO_ON) if self.DEBUG: print 'Debug:',self.DEBUG if self.DEBUG: print 'comm_port:',self.COMM_PORT if self.DEBUG: print 'passwd:',bt.print_data(self.PASSWD) if self.DEBUG: print 'user_str:',bt.print_data(self.USER_STR) if self.DEBUG: print 'user_num:',self.USER_NUM if self.DEBUG: print 'Negotiation:',self.NEGO_ON if self.DEBUG: print 'Invert:',self.INVERT
def do_action_tread(optic): '''Action: Read the Standard or Manufacturer table provided by the user.''' print "Running: Read Table Function" # Get new user number or use default from configuration file user_num = raw_input(user_menu) if user_num == '': user_num = optic.USER_NUM else: user_num = int(user_num) print "Logging on as User: "******"Logon setup failed." return # Logon and send security code if not optic.packet.login_seq_passwd(optic.SER_CONN0, user_num, optic.PASSWD): print "Logon setup failed." return # Read Table print "Reading Table:", table_num results = optic.packet.full_table_read(optic.SER_CONN0, table_num) if not results[0]: print "Read table failed." return else: print "Data Format: <OK - 1 byte><Data length - 2 bytes><Data><Data CRC - 1 byte>" print "Table ", table_num, " results: ", bt.print_data(results[1]) # Logoff #if not optic.packet.send_logoff(optic.SER_CONN0): if not optic.packet.send_terminate(optic.SER_CONN0): print "Logoff setup failed." # Return return
def do_action_tread(optic): '''Action: Read the Standard or Manufacturer table provided by the user.''' print "Running: Read Table Function" # Get new user number or use default from configuration file user_num = raw_input(user_menu) if user_num == '': user_num = optic.USER_NUM else: user_num = int(user_num) print "Logging on as User: "******"Logon setup failed." return # Logon and send security code if not optic.packet.login_seq_passwd(optic.SER_CONN0, user_num, optic.PASSWD): print "Logon setup failed." return # Read Table print "Reading Table:",table_num results = optic.packet.full_table_read(optic.SER_CONN0, table_num) if not results[0]: print "Read table failed." return else: print "Data Format: <OK - 1 byte><Data length - 2 bytes><Data><Data CRC - 1 byte>" print "Table ",table_num," results: ",bt.print_data(results[1]) # Logoff #if not optic.packet.send_logoff(optic.SER_CONN0): if not optic.packet.send_terminate(optic.SER_CONN0): print "Logoff setup failed." # Return return
def nego_setup(self,ser_conn): '''Initiates nego sequence. Handles the negotiate message''' # Create and reset packet #global SEQ #packet = c12packet.C1218_packet() # Test if turned off and just return success if not self.nego_on: return SUCCESS #Send ident resp = [False,''] self.reset_packet(ctrl = self.seq) #Send nego resp = [False,''] self.reset_packet(ctrl = self.seq) if not self.nego(): print "login_setup: Failed to build nego" data = self.full_packet() while resp[0] == False: if self.debug: print "Sending: %s: %s" % ('nego',self.print_packet(data)) self.send_data(ser_conn,data) # Read Response resp = self.read_response(ser_conn) if resp[0] == False: print "login_setup: nego response failed - ",resp[1] # This should return OK plus negotiation data if self.debug: print "Incoming data: ",bt.print_data(resp[1]) result = struct.unpack('B',resp[1][0])[0] # Cycle Control Byte #SEQ ^= 1 self.seq ^= 1 if result: print "login_setup: Failed nego - ", resp_names[result] return FAIL # Return the sequence where we left off return SUCCESS
def login_user(self,ser_conn, user,user_str=''): '''Provides the ability to logon using a specific user and specific passwd. Sends logon and security messages.''' # Create and reset packet #global SEQ #packet = c12packet.C1218_packet() resp = [False,''] #Send logon self.reset_packet(ctrl = self.seq) if user_str: if not self.logon_num(num=user,data=user_str): print "login_user: Failed to build logon" else: if not self.logon_num(num=user): print "login_user: Failed to build logon" data = self.full_packet() while resp[0] == False: if self.debug: print "Sending: %s: %s" % ('logon',self.print_packet(data)) self.send_data(ser_conn,data) # Read Response resp = self.read_response(ser_conn) if resp[0] == False: print "login_user: logon response failed - ",resp[1] # This should return OK if self.debug: print "Incoming data: ",bt.print_data(resp[1]) result = struct.unpack('B',resp[1][0])[0] # Cycle Control Byte #SEQ ^= 1 self.seq ^= 1 if result: print "login_user: Failed logon - ", resp_names[result] return FAIL # Return the sequence where we left off return SUCCESS
def ident_setup(self,ser_conn): '''Initiates ident sequence. Handles the ident message''' # Create and reset packet #global SEQ #packet = c12packet.C1218_packet() fail = 5 #Send ident resp = [False,''] self.reset_packet(ctrl = self.seq) if not self.ident(): print "ident_setup: Failed to build ident" data = self.full_packet() while resp[0] == False: if self.debug: print "Sending: %s: %s" % ('ident',self.print_packet(data)) self.send_data(ser_conn,data) # Read Response resp = self.read_response(ser_conn) if resp[0] == False: print "ident_setup: ident response failed - ",resp[1] return FAIL # This should return OK if self.debug: print "Incoming data: ",bt.print_data(resp[1]) result = struct.unpack('B',resp[1][0])[0] # Cycle Control Byte #SEQ ^= 1 self.seq ^= 1 if result: print "ident_setup: Failed ident - ", resp_names[result] return FAIL # Return the sequence where we left off return SUCCESS
def run_proc(self,ser_conn, proc, indata=''): '''Run the specified procedure. Standard or Manufacturer table shoule be handled before the call.''' # Create and reset packet #global SEQ #packet = c12packet.C1218_packet() #Send ident resp = [False,''] self.reset_packet(ctrl = self.seq) if not self.proc(proc=proc,data=indata): print "run_proc: Failed to build packet" data = self.full_packet() while resp[0] == False: if self.debug: print "Sending: %s: %s" % ('run_proc',self.print_packet(data)) self.send_data(ser_conn,data) # Read Response resp = self.read_response(ser_conn) if resp[0] == False: print "run_proc: ident response failed - ",resp[1] # This should return OK if self.debug: print "Incoming data: ",bt.print_data(resp[1]) result = struct.unpack('B',resp[1][0])[0] # Cycle Control Byte #SEQ ^= 1 self.seq ^= 1 if result: print "run_proc: Failed to run proc - ", resp_names[result] return FAIL # Return the sequence where we left off return SUCCESS
def send_terminate(self,ser_conn): '''Sends terminate message''' # Create and reset packet #global SEQ #packet = c12packet.C1218_packet() #Send ident resp = [False,''] self.reset_packet(ctrl = self.seq) if not self.term(): print "send_terminate: Failed to build packet" data = self.full_packet() while resp[0] == False: if self.debug: print "Sending: %s: %s" % ('terminate',self.print_packet(data)) self.send_data(ser_conn,data) # Read Response resp = self.read_response(ser_conn) if resp[0] == False: print "send_terminate: ident response failed - ",resp[1] # This should return OK if self.debug: print "Incoming data: ",bt.print_data(resp[1]) result = struct.unpack('B',resp[1][0])[0] # Cycle Control Byte #SEQ ^= 1 self.seq ^= 1 if result: print "send_terminate: Failed termination of connection - ", resp_names[result] return FAIL # Return the sequence where we left off return SUCCESS
def read_response(self,ser_conn): '''Get serial input and return list. [ok/nok,data]''' # <packet> ::= <stp><identity><ctrl><seq-nbr><length><data><crc> # B B B B W Var W # Read ack and test for '\x06' else return error # Read 6 bytes, test first byte for <stp>, test ctrl for multi-packet, and then get length # Read in len + 2 to get all data plus packet <crc> # return data read_ok = True # Results are bad read_nok = False # Results are good multi = 1 # Number of incoming packets header = 6 # Default size of packet header plength = 0 # Incoming size of packet's data section pdata = '' # Incoming packet's data mdata = '' # Combined incoming data inbyte = '' # Initial storage fdata = '' fail = 2 # Fail after 5 bad attempts # Read until ACK inbyte = ser_conn.read_byte() while inbyte not in ack_bytes: if self.debug and (inbyte not in ack_bytes): print "read_response: did not receive ack byte:",bt.print_data(inbyte) if not fail: #return [read_nok,'Nothing returned'] #return [read_nok,bt.print_data(inbyte)] self.send_nack(ser_conn) fail = 2 fail -= 1 inbyte = ser_conn.read_byte() if inbyte == NAK: return [read_nok,'No ACK'] # Get multiple packets by starting with at least one packet while multi: # Grab header inbyte = ser_conn.read_sbytes(header) # Test STP if inbyte[0] != STP: return [read_nok,'Bad STP Value'] # Test for multiple packets by looking at seq_num multi = struct.unpack('B',inbyte[3])[0] # Get data length plength = struct.unpack('>H',inbyte[-2:])[0] # Get data pdata = ser_conn.read_sbytes(plength) # Get Packet CRC pcrc = ser_conn.read_sbytes(2) fdata = inbyte + pdata if self.debug: print "read_response: received data: ", bt.print_data(fdata + pcrc) if not self.test_crc(data=fdata,crc=pcrc): # TODO: c12_18_optical_client.py: make read_response read again for 3 (?) tries before error return [read_nok,'Packet CRC failed'] # Pull multiple packet data together mdata += pdata # If multiple packets send ACK to get next packet if multi: self.send_ack(ser_conn,cmd_pause) # Send ACK and return read is ok and any data received self.send_ack(ser_conn,cmd_pause) return [read_ok,mdata]
COMM_PORT = comm_port COMM_BAUD = 9600 OUTFILE = 'c1218_optical_results_' + self.TIME + '.txt' PASSWD = ('\x20' * 20) USER_STR = ('\x53\x4d\x41\x43\x4B' + '\x20' * 5) # SMACK USER_NUM = 2 INVERT = 0 NEGO_ON = False PASSWD_FILE = '' if DEBUG: print 'Debug:',DEBUG if DEBUG: print 'config file:',CONFIG_FILE if DEBUG: print 'comm_port:',COMM_PORT if DEBUG: print 'comm_baud:',COMM_BAUD if DEBUG: print 'outfile:',OUTFILE if DEBUG: print 'passwd:',bt.print_data(PASSWD) if DEBUG: print 'user_str:',bt.print_data(USER_STR) if DEBUG: print 'user_num:',USER_NUM if DEBUG: print 'Invert:',INVERT if DEBUG: print 'Negotiation:',NEGO_ON if DEBUG: print 'passwd_file:',PASSWD_FILE ###################################### # End: Parse C12.18 Optical Configuration file ###################################### ###################################### # VARIABLES - These values will not change ###################################### # Delay values #start_delay = 100 #start_delay = 1
COMM_PORT = comm_port COMM_BAUD = 9600 OUTFILE = 'c1218_optical_results_' + self.TIME + '.txt' PASSWD = ('\x20' * 20) USER_STR = ('\x53\x4d\x41\x43\x4B' + '\x20' * 5) # SMACK USER_NUM = 2 INVERT = 0 NEGO_ON = False PASSWD_FILE = '' if DEBUG: print 'Debug:', DEBUG if DEBUG: print 'config file:', CONFIG_FILE if DEBUG: print 'comm_port:', COMM_PORT if DEBUG: print 'comm_baud:', COMM_BAUD if DEBUG: print 'outfile:', OUTFILE if DEBUG: print 'passwd:', bt.print_data(PASSWD) if DEBUG: print 'user_str:', bt.print_data(USER_STR) if DEBUG: print 'user_num:', USER_NUM if DEBUG: print 'Invert:', INVERT if DEBUG: print 'Negotiation:', NEGO_ON if DEBUG: print 'passwd_file:', PASSWD_FILE ###################################### # End: Parse C12.18 Optical Configuration file ###################################### ###################################### # VARIABLES - These values will not change ###################################### # Delay values #start_delay = 100 #start_delay = 1