def set_trigger(self, active, channel, Type, Level, Delay, Auto, Range=0): if channel in 'External': LevelADC = int( mV2adc( ur(Level.replace(' ', '')).m_as('mV'), ps.PS5000A_RANGE["PS5000A_5V"], ctypes.c_int16(32767))) self.dev.setup_trigger( active, ps.PS5000A_CHANNEL["PS5000A_EXTERNAL"], LevelADC, ps.PS5000A_THRESHOLD_DIRECTION["PS5000A_{}".format( Type.upper())], Delay, int(ur(Auto.replace(' ', '')).m_as('ms'))) else: LevelADC = int( mV2adc( ur(Level.replace(' ', '')).m_as('mV'), ps.PS5000A_RANGE["PS5000A_{}".format( Range.replace(' ', '').replace('m', 'M'))], self.dev.maxADC)) self.dev.setup_trigger( active, ps.PS5000A_CHANNEL["PS5000A_CHANNEL_{}".format(channel)], LevelADC, ps.PS5000A_THRESHOLD_DIRECTION["PS5000A_{}".format( Type.upper())], Delay, int(ur(Auto.replace(' ', '')).m_as('ms'))) assert_pico_ok(self.dev.status["trigger"])
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 connect(self, numberOfChannels=1): self.makeConnection() #seuraavaksi channel asetukset omiin funktioihinsa. Mahdollisesti 4 kanavalle. self.connectChannels(numberOfChannels) # find maximum ADC count value # handle = chandle # pointer to value = ctypes.byref(maxADC) self.maxADC = ctypes.c_int16() self.status["maximumValue"] = ps.ps5000aMaximumValue( self.chandle, ctypes.byref(self.maxADC)) assert_pico_ok(self.status["maximumValue"]) self.setTrigger(ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"], int(mV2adc(500, self.chRange[0], self.maxADC)), 1000) self.maxSamples = self.preTriggerSamples + self.postTriggerSamples #print(self.maxSamples) self.returnData = np.zeros((self.numberOfChannels, self.maxSamples)) self.clearDatabuffers() for index in range(0, self.numberOfChannels): self.setDatabuffer(index) self.setTimebase(8) self.overflow = ctypes.c_int16() # create converted type maxSamples self.cmaxSamples = ctypes.c_int32(self.maxSamples)
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 set_trigger(self, scope, **kwargs): # 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 = ['A', 'B'] possible_sources.append('Ext') assert settings[ 'source'] in possible_sources, 'the trigger source channel should be enabled' source = scope.channels[settings['source']] if 'timeIntervalSeconds' in settings: delay = int(settings['delay_s'] / scope.settings['timeIntervalSeconds']) else: delay = 0 _source = ctypes.c_int16( ps.PS2000_CHANNEL[channel_index[settings['source']]]) _chRange = ps.PS2000_VOLTAGE_RANGE[range_index[ source.settings['chRange']]] _threshold = ctypes.c_int16( int(mV2adc(1e3 * settings['threshold'], _chRange, scope._maxADC))) _direction = ctypes.c_int16(int( direction_index[settings['direction']])) _delay = ctypes.c_int16(delay) _autoTrigger_ms = ctypes.c_int16(settings['auto_trigger_ms']) # direction = PS5000A_RISING = 2 # delay = 0 s # auto Trigger = 1000 ms self.status["trigger"] = ps.ps2000_set_trigger(self.chandle, _source, _threshold, _direction, _delay, _autoTrigger_ms) assert_pico2000_ok(self.status["trigger"]) self.settings = settings
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 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
# coupling type = PS3000A_DC = 1 # range = PS3000A_10V = 8 # analogue offset = 0 V chARange = 8 status["setChA"] = ps.ps3000aSetChannel(chandle, 0, 1, 1, chARange, 0) assert_pico_ok(status["setChA"]) # Finds the max ADC count # Handle = chandle # Value = ctypes.byref(maxADC) maxADC = ctypes.c_int16() status["maximumValue"] = ps.ps3000aMaximumValue(chandle, ctypes.byref(maxADC)) assert_pico_ok(status["maximumValue"]) # Set an advanced trigger adcTriggerLevel = mV2adc(500, chARange, maxADC) # set trigger channel properties # handle = chandle channelProperties = ps.PS3000A_TRIGGER_CHANNEL_PROPERTIES( adcTriggerLevel, 10, adcTriggerLevel, 10, ps.PS3000A_CHANNEL["PS3000A_CHANNEL_A"], ps.PS3000A_THRESHOLD_MODE["PS3000A_LEVEL"]) nChannelProperties = 1 # auxOutputEnabled = 0 autoTriggerMilliseconds = 10000 status["setTrigProp"] = ps.ps3000aSetTriggerChannelProperties( chandle, ctypes.byref(channelProperties), nChannelProperties, 0, autoTriggerMilliseconds) assert_pico_ok(status["setTrigProp"])
status["setChA"] = ps.ps5000aSetChannel(chandle, channel, 1, coupling_type, chARange, 0) assert_pico_ok(status["setChA"]) # 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
def set_builtin(self,**kwargs): ''' sets the scope built-in arbitrary generator: arguments: none keyword arguments: offsetVoltage: (in V) pkToPk: (in V) waveType: sine/square/triangle/dc_voltage freq: start and stop frequencies, for sweep (List, in Hz) from 30 mHz up to 20 MHz ---Advanced---- increment: frequency increment (in Hz) dwellTime: frequency increment rate (in Hz/s) sweepType: up/down/updown/downup the type of frequency sweep shots: number of waveform cycles to output, set 0 for continuous generation for shots>0, sweeps should be 0 sweeps: number of sweeps to output, set 0 to use shots ---Trigger options--- direction: the direction of the trigger (rising/falling/gate_high/gate_low) triggerSource: the source that triggers the signal generator (none/scope_trig/ext_in/soft_trig) if not 'none', either sweep or shots must be nonzero threshold: external trigger level (in V) ''' #handles default values settings = {} for key,current_value in self.settings.items(): settings[key] = kwargs[key] if key in kwargs else current_value check_kwargs_scope(**kwargs) assert settings['pkToPk'] <= 2.0, 'max pkToPk is 2 Vpp' assert settings['offsetVoltage'] <= 2.0, 'max offset is 2 V' assert (min(settings['freq'])>=30e-3 and max(settings['freq'])<=20e6), 'freq can range from 30 mHz up to 20 MHz' assert settings['shots']*settings['sweeps']==0, 'only sweeps or shots can be nonzero' if not settings['triggerSource'] is 'none': if settings['direction'] in ['gate_high','gate_low']: assert settings['shots']==0 and settings['sweeps']==0, 'when triggerSource is not none, and direction is gate_high or gate_low, shots and sweeps must be zero' _offsetVoltage = ctypes.c_int32(int(settings['offsetVoltage']*1e6)) _pkToPk = ctypes.c_uint32(int(settings['pkToPk']*1e6)) _waveType = ctypes.c_int32(wave_index[settings['waveType']]) _freqMin = ctypes.c_double(settings['freq'][0]) _freqMax = ctypes.c_double(settings['freq'][-1]) _increment = ctypes.c_double(settings['increment']) _dwellTime = ctypes.c_double(settings['dwellTime']) _sweepType = ctypes.c_int32(sweep_type_index[settings['sweepType']]) _operation = 0 #(not available on 5000A) _shots = ctypes.c_uint32(settings['shots']) _sweeps = ctypes.c_uint32(settings['sweeps']) _triggerType = ctypes.c_int32(direction_index[settings['direction']]) _triggerSource = ctypes.c_int32(trigger_source_index[settings['triggerSource']]) _chRange = ps.PS5000A_RANGE[range_index[5.]] _extInThreshold = ctypes.c_int16(int(mV2adc(int(1e3*settings['threshold']),_chRange, self.scope._maxADC))) self.status["setSigGenBuiltInV2"] = ps.ps5000aSetSigGenBuiltInV2(self.chandle, _offsetVoltage, _pkToPk, _waveType, _freqMin, _freqMax, _increment, _dwellTime, _sweepType, _operation, _shots, _sweeps, _triggerType, _triggerSource, _extInThreshold) assert_pico_ok(self.status["setSigGenBuiltInV2"]) self.settings = settings
status["setChB"] = ps.ps5000aSetChannel(chandle, channel, 1, coupling_type, chBRange, 0) assert_pico_ok(status["setChB"]) # find maximum ADC count value # handle = chandle # pointer to value = ctypes.byref(maxADC) maxADC = ctypes.c_int16() status["maximumValue"] = ps.ps5000aMaximumValue(chandle, ctypes.byref(maxADC)) assert_pico_ok(status["maximumValue"]) # Set up single trigger # handle = chandle # enabled = 1 source = ps.PS5000A_CHANNEL["PS5000A_CHANNEL_A"] threshold = 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"]) # Set number of pre and post trigger samples to be collected preTriggerSamples = 2500 postTriggerSamples = 2500 maxSamples = preTriggerSamples + postTriggerSamples # Get timebase information # handle = chandle timebase = 8
status["setTriggerChannelConditions"] = ps.ps6000SetTriggerChannelConditions( chandle, ctypes.byref(triggerConditions), nTriggerConditions) assert_pico_ok(status["setTriggerChannelConditions"]) status["setTriggerChannelDirections"] = ps.ps6000SetTriggerChannelDirections( chandle, ps.PS6000_THRESHOLD_DIRECTION["PS6000_INSIDE"], ps.PS6000_THRESHOLD_DIRECTION["PS6000_NONE"], ps.PS6000_THRESHOLD_DIRECTION["PS6000_NONE"], ps.PS6000_THRESHOLD_DIRECTION["PS6000_NONE"], ps.PS6000_THRESHOLD_DIRECTION["PS6000_NONE"], ps.PS6000_THRESHOLD_DIRECTION["PS6000_NONE"]) assert_pico_ok(status["setTriggerChannelDirections"]) maxADC = ctypes.c_int16(32512) threshold = mV2adc(109.2, chARange, maxADC) hysteresis = mV2adc((109.2 * 0.015), chARange, maxADC) channelProperties = ps.PS6000_TRIGGER_CHANNEL_PROPERTIES( threshold, hysteresis, (threshold * -1), hysteresis, ps.PS6000_CHANNEL["PS6000_CHANNEL_A"], ps.PS6000_THRESHOLD_MODE["PS6000_WINDOW"]) nChannelProperties = 1 auxOutputEnable = 0 autoTriggerMilliseconds = 10000 status["setTriggerChannelProperties"] = ps.ps6000SetTriggerChannelProperties( chandle, ctypes.byref(channelProperties), nChannelProperties, auxOutputEnable, autoTriggerMilliseconds) assert_pico_ok(status["setTriggerChannelProperties"]) pwqConditions = ps.PS6000_PWQ_CONDITIONS( ps.PS6000_TRIGGER_STATE["PS6000_CONDITION_TRUE"],
rangeA, offsetA) assert_pico_ok(status["setChA"]) # B enableB = 1 # Turn on (1) or off (0) channelB = 1 # PS2000A_CHANNEL_B couplingB = 1 # coupling type: PS2000A_DC = 1 rangeB = 7 offsetB = 0 # analogue offset status["setChB"] = ps.ps2000aSetChannel(handle, channelB, enableB, couplingB, rangeB, offsetB) assert_pico_ok(status["setChB"]) # Set the trigger adcTriggerLevel = mV2adc(triggerVoltage, rangeA, maxADC) # Trigger channel properties channelAProperties = ps.PS2000A_TRIGGER_CHANNEL_PROPERTIES( adcTriggerLevel, # Upper threshold 10, # Threshold's Hysteresis adcTriggerLevel, # Lower threshold 10, ps.PS2000A_CHANNEL["PS2000A_CHANNEL_A"], ps.PS2000A_THRESHOLD_MODE["PS2000A_LEVEL"] # LEVEL = THRESHOLD or WINDOW ) channelBProperties = ps.PS2000A_TRIGGER_CHANNEL_PROPERTIES( adcTriggerLevel, # Upper threshold 10, # Threshold's Hysteresis adcTriggerLevel, # Lower threshold
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()