def setSimpleTrigger(self, channel, threshold_mV, direction, delay_samples, timeout_ms=1000): # TODO: handling of external trigger maxADC = ctypes.c_int16() self.status['maximumValue'] = ps.ps5000aMaximumValue( self.chandle, ctypes.byref(maxADC)) assert_pico_ok(self.status['maximumValue']) if (channel.upper() == 'EXT'): _channel = ps.PS5000A_CHANNEL['PS5000A_EXTERNAL'] _voltage_range = ps.PS5000A_RANGE['PS5000A_5V'] else: _channel = ps.PS5000A_CHANNEL['PS5000A_CHANNEL_' + channel.upper()] _voltage_range = ps.PS5000A_RANGE[ 'PS5000A_' + self.channel_info[channel]['voltage_range']] _threshold_ADC = int(mV2adc(threshold_mV, _voltage_range, maxADC)) _direction = ps.PS5000A_THRESHOLD_DIRECTION['PS5000A_' + direction.upper()] self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, 1, _channel, _threshold_ADC, _direction, delay_samples, timeout_ms) assert_pico_ok(self.status["trigger"]) self.trigger_info = { 'channel': channel.upper(), 'threshold_mV': threshold_mV, 'direction': direction.upper(), 'delay_samples': delay_samples }
def set_trigger(self, channel_name, threshold=0., direction='RISING', is_enabled=True, delay=0, auto_trigger=0): """Set the oscilloscope trigger condition. :param channel_name: the source channel for the trigger (e.g. 'A') :param threshold: the trigger threshold (in V) :param direction: the direction in which the signal must move to cause a trigger :param is_enabled: (boolean) enable or disable the trigger :param delay: the delay between the trigger occuring and the start of capturing data, in number of sample periods :param auto_trigger: the maximum amount of time to wait for a trigger before starting capturing data in seconds The direction parameter can take values of 'ABOVE', 'BELOW', 'RISING', 'FALLING' or 'RISING_OR_FALLING'. """ channel = _get_channel_from_name(channel_name) threshold = self._rescale_V_to_adc(channel_name, threshold) direction = _get_trigger_direction_from_name(direction) assert_pico_ok( ps.ps5000aSetSimpleTrigger(self._handle, is_enabled, channel, threshold, direction, delay, auto_trigger))
def setTrigger(self, source, thresholdmv, delay, timeout=10000): thresh = int(mV2adc(thresholdmv, self.channela_range, self.maxADC)) enabled = 1 #obvs... direction = 2 #PS5000A_RISING self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, enabled, source, thresh, direction, delay, timeout) assert_pico_ok(self.status["trigger"])
def setTrigger(self, triggermV, external, delay): self.threshold = triggermV self.external = external # Set up single trigger # handle = chandle # enabled = 1 if external: source = ps.PS5000A_CHANNEL["PS5000A_EXTERNAL"] else: source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"] threshold = int(mV2adc(self.threshold, self.chARange, self.maxADC)) # direction = PS5000A_RISING = 2 # delay = 0 s # auto Trigger = 0 (never) (whole number for milliseconds) if (self.runtimeNs) > 2 ^ 16 - 1: autotriggerDelay = 2 ^ 16 - 1 else: autotriggerDelay = int(self.runtimeNs) try: self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, 1, source, threshold, 2, delay, ctypes.c_int16(100)) assert_pico_ok(self.status["trigger"]) except: print("Invalid picoscope trigger settings")
def setTrigger(self, source, threshold, autotrig): # Set up single trigger # handle = chandle # enabled = 1 #self.source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"] #self.threshold = int(mV2adc(500,self.chARange, self.maxADC)) # direction = PS5000A_RISING = 2 # delay = 0 s # auto Trigger = 1000 ms self.autotrig = autotrig self.source = source self.threshold = threshold self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, 1, self.source, self.threshold, 2, 0, self.autotrig) assert_pico_ok(self.status["trigger"])
def set_trigger(self,**kwargs): ''' set-up the oscilloscope trigger. arguments: none keyword arguments (defaults in self.settings): enabled: True/False source: A/B/C/D/Ext direction: rising/falling/gate_high/gate_low (only the latter two are available when using source: Ext) threshold: trigger threshold (in V) delay_s: post-trigger delay before recording (in s) auto_trigger_ms: trigger auto re-arming delay after recording (in ms), set to 0 for REPEAT and SINGLE modes after successfully setting the trigger, self.settings is updated ''' # Set up single trigger settings = {} for key,current_value in self.settings.items(): settings[key] = kwargs[key] if key in kwargs else current_value check_kwargs_scope(**kwargs) possible_sources = self.scope.enabledChannels possible_sources.append('Ext') assert settings['source'] in possible_sources, 'the trigger source channel should be enabled' delay = int(settings['delay_s']/self.scope.settings['timeIntervalSeconds']) _source = ps.PS5000A_CHANNEL[channel_index[settings['source']]] _enabled = int(settings['enabled']) if settings['source'] in ['A','B','C','D']: #analog channels source = self.scope.channels[settings['source']] _chRange = ps.PS5000A_RANGE[range_index[source.settings['chRange']]] else: #source is Ext, range is -5V+5V allowed_directions = ['gate_high','gate_low'] assert settings['direction'] in allowed_directions, 'direction should belong to {0}'.format(allowed_directions) _chRange = ps.PS5000A_RANGE[range_index[5.]] _threshold = int(mV2adc(int(1e3*settings['threshold']),_chRange, self.scope._maxADC)) _direction = int(direction_index[settings['direction']]) _delay = ctypes.c_uint32(delay) _autoTrigger_ms = ctypes.c_int16(settings['auto_trigger_ms']) time.sleep(0) # direction = PS5000A_RISING = 2 # delay = 0 s # auto Trigger = 1000 ms self.status["trigger"] = ps.ps5000aSetSimpleTrigger(self.chandle, _enabled,_source, _threshold, _direction, _delay, _autoTrigger_ms) assert_pico_ok(self.status["trigger"]) self.settings = settings
# Finds the max ADC count # Handle = chandle # Value = ctype.byref(maxADC) maxADC = ctypes.c_int16() status["maximumValue"] = ps.ps5000aMaximumValue(chandle, ctypes.byref(maxADC)) # Set up single trigger # handle = chandle # enabled = 1 source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"] threshold = int(mV2adc(500,chARange, maxADC)) # direction = PS5000A_RISING = 2 # delay = 0 s # auto Trigger = 1000 ms status["trigger"] = ps.ps5000aSetSimpleTrigger(chandle, 1, source, threshold, 2, 0, 1000) assert_pico_ok(status["trigger"]) # Setting the number of sample to be collected preTriggerSamples = 400 postTriggerSamples = 400 maxsamples = preTriggerSamples + postTriggerSamples # Gets timebase innfomation # Handle = chandle timebase = 2 # Nosample = maxsamples # TimeIntervalNanoseconds = ctypes.byref(timeIntervalns) # MaxSamples = ctypes.byref(returnedMaxSamples) # Segement index = 0 timeIntervalns = ctypes.c_float()
def setTriggerAutotrig(self, autotrig): self.autotrig = autotrig self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, 1, self.source, self.threshold, 2, 0, self.autotrig) assert_pico_ok(self.status["trigger"])
def setTriggervoltage(self, threshold): self.threshold = threshold self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, 1, self.source, self.threshold, 2, 0, self.autotrig) assert_pico_ok(self.status["trigger"])
def getTimeSignal(self, channel=None, check=True, trigger=None, triggerThreshold=0, reportOverflow=True): # get all channel data but only return required if check: nMaxSamples = c_long() ps.ps5000aMemorySegments(self.chandle, 1, byref(nMaxSamples)) nMaxSamples.value = math.floor(nMaxSamples.value / self.nEnabledChannels()) if self.maxSamples > nMaxSamples.value: raise Exception("samples will be larger than memory") self.status["getTimebase2"] = ps.ps5000aGetTimebase2(self.chandle, \ self.timebase, self.maxSamples, byref(self.timeInternalns),\ None, 0) assert_pico_ok(self.status["getTimebase2"]) if trigger is not None: source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_{trigCh}".format( trigCh=trigger)] self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, 1, source, triggerThreshold, 2, 0, 0) assert_pico_ok(self.status["trigger"]) else: source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_{trigCh}".format( trigCh='D')] self.status["trigger"] = ps.ps5000aSetSimpleTrigger( self.chandle, 0, source, triggerThreshold, 2, 0, 0) assert_pico_ok(self.status["trigger"]) preTriggerSamples = 0 self.status["runBlock"] = ps.ps5000aRunBlock(self.chandle, \ preTriggerSamples, self.maxSamples, self.timebase, None, 0, None, None) assert_pico_ok(self.status["runBlock"]) ready = c_int16(0) check = c_int16(0) while ready.value == check.value: self.status["isReady"] = ps.ps5000aIsReady(self.chandle, byref(ready)) time.sleep(1e-3) for key in self.chs: if self.chs[key].enabled: self.chs[key].buffer = (c_int16 * self.maxSamples)() self.status["setDataBuffer"+key] = ps.ps5000aSetDataBuffer(self.chandle, \ self.chs[key].channel, byref(self.chs[key].buffer), self.maxSamples, 0, 0) assert_pico_ok(self.status["setDataBuffer" + key]) overflow = c_int16() cmaxSamples = c_uint32(self.maxSamples) self.status["getValues"] = ps.ps5000aGetValues(self.chandle, 0 , \ byref(cmaxSamples), 0, 0, 0, byref(overflow)) assert_pico_ok(self.status["getValues"]) overflow = '{0:04b}'.format(overflow.value) chOF = [bool(int(i)) for i in overflow] self.chs['A'].overflow = chOF[-1] self.chs['B'].overflow = chOF[-2] self.chs['C'].overflow = chOF[-3] self.chs['D'].overflow = chOF[-4] channels = ['A', 'B', 'C', 'D'] if reportOverflow: for i in range(4): if chOF[-(i + 1)]: print('channel {0} overflow'.format(channels[i])) maxADC = c_int16() self.status["maximumValue"] = ps.ps5000aMaximumValue( self.chandle, byref(maxADC)) assert_pico_ok(self.status["maximumValue"]) for key in self.chs: if self.chs[key].enabled: self.chs[key].timeSignal = (np.array(self.chs[key].buffer) / maxADC.value) * \ self.psVoltageRange[self.chs[key].range] * 1e-3 if channel is not None: return self.chs[channel].timeSignal
def setup_trigger(self, Enable, channelID, LevelADC, Type, Delay, Auto): # Set up single trigger # handle = self.chandle # enabled = 1 # direction = PS5000A_RISING = 2 self.status["trigger"] = ps.ps5000aSetSimpleTrigger(self.chandle, Enable, channelID, LevelADC, Type, Delay, Auto)
class BuildModel_M3_Single(object): if __name__ == "__main__": # Serial port communication: look this up in 'device manager' port = '/dev/ttyUSB0' # Serial port step = 1000 # Number of traces N = 5000 * 23 # Number of samples samples = 1000 # 2ns, sampling rate= 500MSa/s(p22 2.7 Timebase) timebase = 1 # post-trigger or before-trigger post_trigger = True # trigger threshold(mV) threshold = 2000 # trigger direction posedge_trigger = True # vertical offset vertical_offset = 0.1 #delay delay = 0 plain_len = 32 # byte length of the plaintext cipher_len = 32 # byte length of the ciphertext # Intiliazed random generator random.seed() # Open serial port ser = serial.Serial(port) # Wait for 200ms time.sleep(0.2) if (post_trigger): preTriggerSamples = 0 postTriggerSamples = samples else: preTriggerSamples = samples postTriggerSamples = 0 # Connect the scope chandle = ctypes.c_int16() status = ps.ps5000aOpenUnit( ctypes.byref(chandle), None, ps.PS5000A_DEVICE_RESOLUTION["PS5000A_DR_8BIT"]) if status != PICO_STATUS['PICO_OK']: raise PicoSDKCtypesError("PicoSDK returned '{}'".format( PICO_STATUS_LOOKUP[status])) # Set up channel A status = ps.ps5000aSetChannel(chandle, ps.PICO_CHANNEL["A"], 1, ps.PICO_COUPLING['DC'], ps.PS5000A_RANGE["PS5000A_1V"], vertical_offset) if status != PICO_STATUS['PICO_OK']: raise PicoSDKCtypesError("PicoSDK returned '{}'".format( PICO_STATUS_LOOKUP[status])) # Set up channel B status = ps.ps5000aSetChannel(chandle, ps.PICO_CHANNEL["B"], 1, ps.PICO_COUPLING['DC'], ps.PS5000A_RANGE["PS5000A_5V"], 0) if status != PICO_STATUS['PICO_OK']: raise PicoSDKCtypesError("PicoSDK returned '{}'".format( PICO_STATUS_LOOKUP[status])) # Set up trigger if (posedge_trigger): status = ps.ps5000aSetSimpleTrigger( chandle, 1, ps.PICO_CHANNEL["B"], mV2adc(threshold, ps.PS5000A_RANGE["PS5000A_5V"], ctypes.c_int16(32512)), ps.PS5000A_THRESHOLD_DIRECTION["PS5000A_RISING"], delay, 0) else: status = ps.ps5000aSetSimpleTrigger( chandle, 1, ps.PICO_CHANNEL["B"], mV2adc(threshold, ps.PS5000A_RANGE["PS5000A_5V"], ctypes.c_int16(32512)), ps.PS5000A_THRESHOLD_DIRECTION["PS5000A_FALLING"], delay, 0) if status != PICO_STATUS['PICO_OK']: raise PicoSDKCtypesError("PicoSDK returned '{}'".format( PICO_STATUS_LOOKUP[status])) # Create buffers ready for assigning pointers for data collection Databuffer = (ctypes.c_int16 * samples)() status = ps.ps5000aSetDataBuffers(chandle, 0, ctypes.byref(Databuffer), None, samples, 0, 0) if status != PICO_STATUS['PICO_OK']: raise PicoSDKCtypesError("PicoSDK returned '{}'".format( PICO_STATUS_LOOKUP[status])) trs = TRS_TraceSet.TRS_TraceSet("BuildModel_M3_Single_1000Samples.trs") trs.write_header(N, samples, True, plain_len + cipher_len, 2E-9, 1 / 65536) for i in range(0, N): # Generate plaintext & mask plaintext = bytearray( [secrets.randbits(8) for j in range(0, plain_len)]) plaintext[28] = int(i / 5000) + 1 # Start status = ps.ps5000aRunBlock(chandle, preTriggerSamples, postTriggerSamples, timebase, None, 0, None, None) if status != PICO_STATUS['PICO_OK']: raise PicoSDKCtypesError("PicoSDK returned '{}'".format( PICO_STATUS_LOOKUP[status])) # Send plaintext ser.write(plaintext) # Read out ciphertext ciphertext = bytearray(ser.read(cipher_len)) # Check for data collection to finish using ps5000aIsReady ready = ctypes.c_int16(0) check = ctypes.c_int16(0) while ready.value == check.value: status = ps.ps5000aIsReady(chandle, ctypes.byref(ready)) # Create overflow location overflow = ctypes.c_int16() # create converted type totalSamples cTotalSamples = ctypes.c_int32(samples) # Retried data from scope to buffers assigned above status = ps.ps5000aGetValues(chandle, 0, ctypes.byref(cTotalSamples), 0, 0, 0, ctypes.byref(overflow)) if status != PICO_STATUS['PICO_OK']: raise PicoSDKCtypesError("PicoSDK returned '{}'".format( PICO_STATUS_LOOKUP[status])) if (overflow.value != 0): print("overflow!") # Write trace trs.write_trace(plaintext, ciphertext, np.array(Databuffer), True) # print to screen if i % step == 0 and i > 0: print("i=" + str(i)) print("Instr=" + str(plaintext[28])) print("plain=") PrintHexData(plaintext) print("cipher=") PrintHexData(ciphertext) trs.close()