def _read_response(self, command: Command): try: # Read exactly one byte. This is the verification of the cesar unit # its either ACK (0x06) or NACK (0x15) verification = self._transport.read(1) self._logger.debug( "Received ACK/NAK byte: {}".format(verification)) if verification == self.ACK: pass elif verification == self.NAK: raise CommunicationError("Device returned NAK") # in fact, we can compute how long the response will be, based on the first two bytes read # but this works for us and we dont care about more details... raw_response = self._transport.read(self.RESPONSE_MAX_LENGTH) self._logger.debug("Received {}".format(" ".join( map(hex, raw_response)))) # Now send back a ACK since we got our data, even if the data is not valid # We just don't care about this. If the data is not valid, we throw an exception # and the calling api will re-engage into sending the message self._transport.write(bytearray([self.ACK])) self._logger.debug("Sent ACK to device") except SerialTimeoutException: raise CommunicationError("Could not read response. Timeout") raw_response = bytearray(raw_response) msg = MessagePacket.from_raw(raw_response) if not msg.is_valid(): raise CommunicationError("Received response is not valid") response = Response(command.get_outputs()) # we received not the same amount of data than expected. This occurs only in two cases: # 1. The protocol was not correctly specified by the user # 2. The device returns just an CSR code instead of the data if command.get_expected_response_data_length() != msg.get_data_length( ): # This is then the case 2 if msg.get_data_length() == 1: response.get_csr().set_raw(msg.get_data()) else: raise CommunicationError( "Received an unexpected amount of data ({} bytes) from device. Expected {} bytes (excluding CSR)" .format(msg.get_data_length, command.get_expected_response_data_length())) else: # Here we just assign the data to the correct output self._assign_data(msg.get_data(), command.get_outputs()) return response
def get_recipe_setpoint_and_time(self, recipe_number): number_input = Input(recipe_number, Parameter.Recipe.Number()) return self._protocol.execute( Command(188, [number_input], [ Output(Parameter.Recipe.Setpoint()), Output(Parameter.Recipe.RunTime()) ]))
def set_reflected_power_parameters(self, turn_off_time, power_limit_trigger): time_input = Input(turn_off_time, Parameter.ReflectedPowerParameter.TimeLimit()) power_input = Input(power_limit_trigger, Parameter.ReflectedPowerParameter.PowerTrigger()) return self._protocol.execute( Command(33, [time_input, power_input], []))
def turn_off(self): return self._protocol.execute(Command(1, [], []))
def get_time_limit(self): return self._protocol.execute( Command(243, [], [Output(Parameter.OnTimeLimit())]))
def restore(self, preset_number): preset_input = Input(preset_number, Parameter.Preset()) return self._protocol.execute(Command(25, [preset_input], []))
def set_user_port_scaling(self, voltage_scaling): voltage_input = Input(voltage_scaling, Parameter.VoltageScaling()) return self._protocol.execute(Command(30, [voltage_input], []))
def set_setpoint(self, power_or_voltage): power_input = Input(power_or_voltage, Parameter.Setpoint()) return self._protocol.execute(Command(8, [power_input], []))
def set_control_mode(self, control_mode): control_input = Input(control_mode, Parameter.ControlMode()) return self._protocol.execute(Command(14, [control_input], []))
def turn_on(self): return self._protocol.execute(Command(2, [], []))
def set_forward_power_limit(self, power_limit): power_input = Input(power_limit, Parameter.ForwardPower()) return self._protocol.execute(Command(4, [power_input], []))
def get_forward_power_limit(self): return self._protocol.execute( Command(169, [], [Output(Parameter.ForwardPower())]))
def get_reflected_power_limit(self): return self._protocol.execute( Command(170, [], [Output(Parameter.ReflectedPower())]))
def get_delivered_power(self): return self._protocol.execute( Command(167, [], [Output(Parameter.ReflectedPower())]))
def get_setpoint(self): return self._protocol.execute( Command( 164, [], [Output(Parameter.Setpoint(), Output(Parameter.Regulation())) ]))
def get_status(self): return self._protocol.execute( Command(162, [], [FlagOutput(Parameter.Status())]))
def get_control_mode(self): return self._protocol.execute( Command(155, [], [Output(Parameter.ControlMode())]))
def get_regulation_mode(self): return self._protocol.execute( Command(154, [], [Output(Parameter.Regulation())]))
def set_regulation_mode(self, mode): mode_input = Input(mode, Parameter.Regulation()) return self._protocol.execute(Command(3, [mode_input], []))
def get_recipe_ramp_time(self, recipe_number): number_input = Input(recipe_number, Parameter.Recipe.Number()) return self._protocol.execute( Command(191, [number_input], [Output(Parameter.Recipe.RampTime())]))
def set_reflected_power_limit(self, power_limit): power_input = Input(power_limit, Parameter.ReflectedPower()) return self._protocol.execute(Command(5, [power_input], []))
def get_pulsing_frequency(self): return self._protocol.execute( Command(193, [], [Output(Parameter.PulsingFrequency())]))
def set_time_limit(self, time_limit): time_input = Input(time_limit, Parameter.OnTimeLimit()) return self._protocol.execute(Command(10, [time_input], []))
def get_pulsing_duty_cycle(self): return self._protocol.execute( Command(196, [], [Output(Parameter.PulsingDutyCycle())]))
def set_recipe_number(self, number): number_input = Input(number, Parameter.Recipe.NumberOf()) return self._protocol.execute(Command(19, [number_input], []))
def get_runtime(self): return self._protocol.execute( Command(205, [], [Output(Parameter.Runtime())]))
def set_remote_control(self, control_mode): control_input = Input(control_mode, Parameter.ControlOverride()) return self._protocol.execute(Command(29, [control_input], []))
def get_serial_address_and_baudrate(self): return self._protocol.execute( Command( 212, [], [Output(Parameter.BusAddress()), Output(Parameter.BaudRate())]))
def set_ramping_rise_time(self, time): time_input = Input(time, Parameter.RampTime()) return self._protocol.execute(Command(31, [time_input], []))
def get_fault_register(self): return self._protocol.execute( Command(223, [], [FlagOutput(Parameter.FaultRegister())]))