def connect(self): try: #Creates the serial connection to the Arduino as specified in the config file #print("creating serial stuff here - Arduino_RPI_comm line 230") ard_log.info("Connecting to port: %s with baudrate: %s" % (self.port, self.baudrate)) self.ser = serial.Serial(port=self.port, baudrate=self.baudrate, bytesize=self.bytesize, parity=self.parity, stopbits=self.stopbits, timeout=self.timeout, xonxoff=self.xonxoff, rtscts=self.rtscts, dsrdtr=self.dsrdtr, writeTimeout=self.writeTimeout) self.worker = GetHWPoller(self.sleeptime, self.poll_HW) self.worker.start() except serial.SerialException as e: ard_log.error(e) print("except serial.SerialException as e") except ValueError as e: ard_log.error(e) print("except ValueError as e")
def receive(self): ard_log.info('Last response: "%s"' % self.response) if self.response != None: msg = self.response.split(",") self.ID_received = msg[0] self.fields_received = msg[1] self.data_received = msg[2].strip("#").split(";") ard_log.info('Received - msg_id:%s, num_fields:%s' % (self.ID_received, self.fields_received)) return
def load(self): '''Function that loads the configurations from the arms.config module. If the module can't be reached it will load default configurations. ''' try: UART_config = c.var.data["uart_config"] ARDUINO_config = c.var.data["arduino_config"] self.port = UART_config[ "port"] #Port for Uart, might need to be reconfigured self.baudrate = UART_config["baudrate"] self.bytesize = UART_config["bytesize"] #number of bits per bytes self.parity = UART_config["parity"] #set parity check: no parity self.stopbits = UART_config["stopbits"] #number of stop bits self.timeout = UART_config["timeout"] self.xonxoff = UART_config[ "xonxoff"] #disable software flow control self.rtscts = UART_config[ "rtscts"] #disable hardware (RTS/CTS) flow control self.dsrdtr = UART_config[ "dsrdtr"] #disable hardware (DSR/DTR) flow control self.writeTimeout = UART_config["writeTimeout"] #timeout for write self.sleeptime = UART_config[ "sleeptime"] #sets the time between message polling self.adjustment_pulses = ARDUINO_config[ 'adjustment_pulses'] #number of pulses to send when adjusting self.upper_bound = ARDUINO_config[ 'upper_bound'] #adjusts the acceptable upper bound relative error self.upper_bias = ARDUINO_config['upper_bias'] #Bias limit on self.lower_bound = ARDUINO_config[ 'lower_bound'] #adjust the acceptable lower bound self.lower_bias = ARDUINO_config['lower_bias'] #Bias limit self.max_recursion = ARDUINO_config[ 'max_recursion'] #determines the maximum number of adjustments in the command ard_log.info("Loaded configurations") except (KeyError, TypeError): self.port = "COM2" #Port for Uart, might need to be reconfigured on self.baudrate = 9600 self.bytesize = serial.EIGHTBITS #number of bits per bytes self.parity = serial.PARITY_NONE #set parity check: no parity self.stopbits = serial.STOPBITS_ONE #number of stop bits self.timeout = 1 #non-block read self.xonxoff = False #disable software flow control self.rtscts = False #disable hardware (RTS/CTS) flow control self.dsrdtr = False #disable hardware (DSR/DTR) flow control self.writeTimeout = 2 #timeout for write self.sleeptime = 1 #sets the time between message polling 0.1 is good maybe ard_log.error( "Could not load configurations, using default values") return
def evaluate_response(self): if (self.response != None): msg = self.response.split(",") self.ID_received = msg[0] #print("printing msg[0]" + msg[0]) self.fields_received = msg[1] self.data_received = msg[2].strip("#").split(";") print(self.data_received) ##PRINTING HERE ard_log.info('Received - msg_id:%s, num_fields:%s' % (self.ID_received, self.fields_received)) function_vec = [ self.command_close, self.command_open, self.command_activate_solenoid, self.command_deactivate_solenoid, self.command_acknowledge_error, self.command_request_info ] #function_ID = [ "0" , "1" , "2" , "3 , "4" , "5" ] function_vec[int(self.ID_received)]( ) #Execute the function given by the command ard_log.info('Executed Command %s' % (self.ID_received)) return True
def kill(self): ard_log.info("WORKER END") sys.stdout.flush() self.aliveflag.clear() # kills the thread self.join()
def command_request_info(self): self.pressure_received = float(self.data_received[0]) ard_log.info("Pressure at this time is: %s" % (self.data_received[0])) self.Arduino_replied = True return
def disconnect(self): ard_log.info("Disconnected from port %s" % (self.port)) self.kill() #ends the listner thread self.ser.close() #closes the serial connection return