def command_fibre_delay(par): """Get the command to set a fibre delay""" if par > _max_fibre_delay or par < 0: raise tellie_exception.TellieException("Invalid fibre delay: %s" % par) adjusted, adj_delay, setting = parameters.fibre_delay(par) print "COMMAND", par, adjusted, adj_delay, setting if adjusted is True: raise tellie_exception.TellieException("Invalid delay: %s" % (par)) command = [_cmd_fd + chr(setting)] buffer_check = _cmd_fd return command, buffer_check
def command_pulse_number(par): """Get the command to set a pulse number""" if par > _max_pulse_number or par < 0: raise tellie_exception.TellieException("Invalid pulse number: %s" % (par)) par = int(par) adjusted, actual_par, hi, lo = parameters.pulse_number(par) if adjusted is True: raise tellie_exception.TellieException("Invalid pulse number: %s" % (par)) command = [_cmd_pn_hi+chr(hi)] command += [_cmd_pn_lo+chr(lo)] buffer_check = _cmd_pn_hi + _cmd_pn_lo return command, buffer_check
def _check_clear_buffer(self): """Many commands expect an empty buffer, fail if they are not! """ buffer_read = self._serial.read(100) if buffer_read != "": raise tellie_exception.TellieException( "Buffer not clear: %s" % (buffer_read))
def command_trigger_delay(par): """Get the command to set a trigger delay""" if par > _max_trigger_delay or par < 0: raise tellie_exception.TellieException("Invalid trigger delay: %s" % par) command = [_cmd_td+chr(par/5)] buffer_check = _cmd_td return command, buffer_check
def _send_command(self, command, readout=True, buffer_check=None): """Send a command to the serial port. Command can be a chr/str (single write) or a list. Lists are used for e.g. a high/low bit command where the high bit could finish with an endline (i.e. endstream)""" self.logger.debug("_send_command:%s" % command) if type(command) is str: command = [command] if type(command) is not list: raise tellie_exception.TellieException( "Command is not a list: %s %s" % (command, type(command))) try: for c in command: self._serial.write(c) except: raise tellie_exception.TellieException( "Lost connection with TELLIE control!") if not buffer_check: # assume returns same as input buffer_check = '' for c in command: buffer_check += c if readout is True: # One read command (with default timeout of 0.1s) should be # enough to get all the chars from the readout. buffer_read = self._serial.read(len(buffer_check)) if str(buffer_read) != str(buffer_check): self.logger.debug( "problem reading buffer, send %s, read %s" % (command, buffer_read)) # clear anything else that might be in there time.sleep(0.1) remainder = self._serial.read(100) self._serial.write("X") # send a stop time.sleep(0.1) self._serial.write("C") # send a clear time.sleep(0.1) self._serial.read(100) message = ("Unexpected buffer output:\nsaw: %s, remainder " "%s\nexpected: %s" % (buffer_read, remainder, buffer_check)) self.logger.warn(message) raise tellie_exception.TellieException(message) else: self.logger.debug("success reading buffer:%s" % buffer_read) else: self.logger.debug("not a readout command")
def enable_external_trig(self, while_fire=False): """Tell TELLIE to fire on any external trigger. Can send a fire command while already in fire mode if required.""" self.logger.debug("Enable ext triggering mode") if self._firing is True and while_fire is False: raise tellie_exception.TellieException( "Cannot set ext. trig, already in firing mode") self._send_command(_cmd_enable_ext_trig)
def command_pulse_delay(par): """Get the command to set a pulse delay""" if par > _max_pulse_delay or par < 0: raise tellie_exception.TellieException("Invalid pulse delay: %s" % par) ms = int(par) us = int((par-ms)*250) command = [_cmd_pd+chr(ms)] command += [chr(us)] buffer_check = _cmd_pd return command, buffer_check
def command_pulse_height(par): """Get the command to set a pulse height""" if par > _max_pulse_height or par < 0: raise tellie_exception.TellieException( "Invalid pulse height: %s" % par) hi = par >> 8 lo = par & 255 command = [_cmd_ph_hi+chr(hi)] command += [_cmd_ph_lo+chr(lo)] command += [_cmd_ph_end] buffer_check = _cmd_ph_hi + _cmd_ph_lo + _cmd_ph_end return command, buffer_check
def fire_continuous(self): """Fire Tellie in continous mode. """ if self._firing is True: if _buffer_end_sequence in self.read_buffer(): self._firing = False else: raise tellie_exception.TellieException( "Cannot fire, already in firing mode") self._send_command(_cmd_fire_continuous, False) self._firing = True self._force_setting = False
def trigger_single(self): """Fire single pulse upon receiving an external trigger. """ if self._firing is True: if _buffer_end_sequence in self.read_buffer(): self._firing = False else: raise tellie_exception.TellieException( "Cannot fire, already in firing mode") self._send_command(_cmd_fire_ext_trig, False) self._firing = True
def command_pulse_width(par): """Get the command to set a pulse width""" if par > _max_pulse_width or par < 0: raise tellie_exception.TellieException( "Invalid pulse width: %s %s %s" % (par, _max_pulse_width, par > _max_pulse_width)) hi = par >> 8 lo = par & 255 command = [_cmd_pw_hi + chr(hi)] command += [_cmd_pw_lo + chr(lo) + _cmd_pw_end] buffer_check = _cmd_pw_hi + _cmd_pw_lo + _cmd_pw_end return command, buffer_check
def set_fibre_delay(self, par): """Set the fibre (channel) delay for the selected channel""" if len(self._channel) != 1: raise tellie_exception.TellieException( "Cannot set parameter with channels set as %s" % (self._channel)) if par == self._current_fd[self._channel[0]] and \ not self._force_setting: pass else: self.logger.debug("Set Fibre delay %s %s" % (par, type(par))) command, buffer_check = command_fibre_delay(par) self._send_channel_setting_command(command=command, buffer_check=buffer_check) self._current_fd[self._channel[0]] = par
def check_ready(self): """Check that all settings have been set""" not_set = [] if self._current_ph is None: not_set += ["Pulse height"] #if self._current_fd is None: # not_set += ["Fibre delay"] if self._current_pn is None: not_set += ["Pulse number"] if self._current_pd is None: not_set += ["Pulse delay"] #if self._current_td is None: # not_set += ["Trigger delay"] if not_set != []: raise tellie_exception.TellieException( "Undefined options: %s" % (", ".join(opt for opt in not_set)))
def _send_setting_command(self, command, buffer_check=None, while_fire=False): """Send non-firing command. All of these should have a clear buffer before being used. Can set while_fire to True to allow a non-fire command to be sent while firing (will cause PIN readout to be flushed to buffer). """ self.logger.debug("Send non-firing command") if self._firing is True: if while_fire is False: raise tellie_exception.TellieException( "Cannot run command, in firing mode") else: # Assume that we CANNOT readout the buffer here! self._send_command(command=command, readout=False) else: self._check_clear_buffer() self._send_command(command=command, buffer_check=buffer_check)
def fire_single(self): """Fire single pulse """ if self._firing is True: if _buffer_end_sequence in self.read_buffer(): self._firing = False else: raise tellie_exception.TellieException( "Cannot fire, already in firing mode") if self._channel <= 56: # up to box 7 cmd = _cmd_read_single_lower else: cmd = _cmd_read_single_upper self._send_command(cmd, False) self._firing = True pin = self.read_pin(self._channel[0]) while not pin: pin = self.read_pin(self._channel[0]) return pin
def fire(self, while_fire=False): """Fire tellie, place class into firing mode. Can send a fire command while already in fire mode if required.""" self.logger.debug("Fire!") if self._firing is True and while_fire is False: if _buffer_end_sequence in self.read_buffer(): self._firing = False else: raise tellie_exception.TellieException( "Cannot fire, already in firing mode") self.check_ready() buffer_check = _cmd_fire_series if (self._current_pn * self._current_pd) < 500: buffer_check += _buffer_end_sequence self._send_command(_cmd_fire_series, buffer_check=buffer_check) else: self._send_command(_cmd_fire_series, buffer_check=buffer_check) self._firing = True self._force_setting = False