Skip to content

Instantly share code, notes, and snippets.

@ColinBrosseau
Created September 9, 2013 20:01
Show Gist options
  • Save ColinBrosseau/6500730 to your computer and use it in GitHub Desktop.
Save ColinBrosseau/6500730 to your computer and use it in GitHub Desktop.
Fake getter/ real setter
Except for 2-3 exception, the Newport 842-PE doesn't have real getter functions, but it has many setters. But is has a function (sta) that read device status for a lot of parameters. The previous file do a fake getter/setter.
def status
returns a dict where the key/value pairs correspond to each device parameter.
At device initialisation, the device's configuration is read and stored in self.DeviceStatus
The getter scale just read corresponding parameter in self.DeviceStatus and return it.
The setter scale actually write to the device to change its state.
# -*- coding: utf-8 -*-
"""
lantz.drivers.newport.842PE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Newport 842-PE powermeter
:copyright: 2013 by Lantz Authors, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
import time
import warnings
from lantz import Action, Feat
from lantz.serial import SerialDriver
from lantz.errors import InstrumentError
class N842PE(SerialDriver):
"""Newport 842-PE powermeter
"""
ENCODING = 'ascii'
RECV_TERMINATION = '\r\n'
SEND_TERMINATION = '\r\n'
BAUDRATE = 115200
BYTESIZE = 8
PARITY = 'none'
STOPBITS = 1
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.initialize()
self.DeviceStatus = self.status # Read the device status
Scales = ('auto', '1p', '3p', '10p', '30p', '100p', '300p', '1n',
'3n', '10n', '30n', '100n', '300n', '1u', '3u', '10u',
'30u', '100u', '300u', '1m', '3m', '10m', '30m',
'100m', '300m', '1', '3', '10', '30', '100', '300',
'1k', '3k', '10k', '30k', '100k', '300k', '1M', '3M',
'10M', '30M', '100M', '300M',)
@Feat(values=set(Scales))
def scale(self):
out = self.DeviceStatus['CurrentScale']
return self.Scales[out]
@scale.setter
def scale(self, value):
"""This command is used to force the display of the current data into a
specific range. (ssa)
"""
self.query('*ssa {}'.format(value))
self.DeviceStatus = self.status # update cached device status
# The device is a bit buggy for this one as it return the info with 3 \n\r.
# So the hack here repare it.
# We have to read it 3 times.
# In addition, it seems to never return the last field (dBm).
@Feat()
def status(self):
"""This command is used to query the current device status. (sta)
Returns a dict containing device variables's values.
"""
DeviceStatus = {}
out1 = self.parse_query('*sta', format='Head Type: {}\t'
'Head Version: {}\t'
'Head Serial Number: {}\t{}\t'
'Calibration Sensitivity: {} V/W\t'
'Default WaveLength: {} nm\t'
'Active Sensitivity: {} V/W\t'
'Active WaveLength: {} nm\t'
'Scale Min Power : {}\t'
'Scale Max Power: {}\t'
'Scale Min Energy : {}\t'
'Scale Max Energy: {}\t'
'Current Scale: {}\t'
'Energy Mode: {}\t'
'Anticipation: {}\t'
'Trig Level: {}%\t'
'Zero Offset: {}\t'
'Multiplier #1: {}\t'
'Offset #1: {}\t'
'Multiplier #2: {}\t'
'Offset #2: {}\t'
'Currently Logging data: {}\t'
'Analog Output: {}\t'
'Resolution: {}\t'
'Currently Calculating Stats: {}\t'
'High Resolution Display: {}\t'
'Min Wavelength index: {}\t'
'Max Wavelength index: {}\t'
'Upper Bound: {}\t'
'Lower Bound: {}\t'
'Reference Value: {}\t'
'P/F Status: {}\t'
'Threshold: {}')
out1.append(self.parse_query('', format='Attenuator: {}'))
out1.append(self.parse_query('', format='AutoScale: {}'))
DeviceStatus['HeadType'] = out1[0] # WattMeter, Photodiode
#DeviceStatus['Head Version'] = out1[1] # Is that really the head version?
DeviceStatus['SerialNumber'] = out1[2]
DeviceStatus['HeadName'] = out1[3]
DeviceStatus['CalibrationSensitivity'] = out1[4] # unit V/W
DeviceStatus['CalibrationWavelength'] = out1[5] # unit nm, seems non-sense for photodiode
DeviceStatus['ActiveSensitivity'] = out1[6] # unit V/W
DeviceStatus['ActiveWavelength'] = out1[7] # unit nm
DeviceStatus['ScaleMinPower'] = out1[8]
DeviceStatus['ScaleMaxPower'] = out1[9]
DeviceStatus['ScaleMinEnergy'] = out1[10] # is N/A for photodiodes
DeviceStatus['ScaleMaxEnergy'] = out1[11] # is N/A for photodiodes
DeviceStatus['CurrentScale'] = int(out1[12])
DeviceStatus['EnergyMode'] = 1 if (out1[13] == 'On') else 0
DeviceStatus['Anticipation'] = 1 if (out1[14] == 'On') else 0
DeviceStatus['TrigLevel'] = out1[15] # unit %
DeviceStatus['ZeroOffset'] = out1[16]
DeviceStatus['Multiplier'] = [out1[17], out1[19]]
DeviceStatus['Offset'] = [out1[18], out1[20]]
DeviceStatus['CurrentlyLoggingdata'] = 1 if (out1[21] == 'Yes') else 0
DeviceStatus['AnalogOutput'] = 1 if (out1[22] == 'On') else 0
DeviceStatus['Resolution'] = out1[23]
DeviceStatus['CurrentlyCalculatingStats'] = 1 if (out1[24] == 'Yes') else 0
DeviceStatus['HighResolutionDisplay'] = 1 if (out1[25] == 'On') else 0
DeviceStatus['MinWavelengthindex'] = out1[26] # For photodiodes, it's the minimum wavelength in nm
DeviceStatus['MaxWavelengthindex'] = out1[27] # For photodiodes, it's the minimum wavelength in nm
DeviceStatus['UpperBound'] = out1[28]
DeviceStatus['LowerBound'] = out1[29]
DeviceStatus['ReferenceValue'] = out1[30]
DeviceStatus['P/FStatus'] = out1[31] # N/A, .... (To be tested)
DeviceStatus['Threshold'] = out1[32] # On, Off (To be tested)
DeviceStatus['Attenuator'] = 1 if (out1[33] == 'On') else 0 # On, Off or N/A. N/A is when there is not
DeviceStatus['AutoScale'] = 1 if (out1[34] == 'On') else 0
return DeviceStatus
def query(self, command, *, send_args=(None, None),
recv_args=(None, None)):
answer = super().query(command, send_args=send_args,
recv_args=recv_args)
if answer == 'ERROR':
raise InstrumentError
return answer
if __name__ == '__main__':
import argparse
import lantz.log
parser = argparse.ArgumentParser(description='Newport 842-PE powermeter')
parser.add_argument('-i', '--interactive', action='store_true',
default=False, help='Show interactive GUI')
parser.add_argument('-p', '--port', type=str, default='17',
help='Serial port to connect to')
args = parser.parse_args()
lantz.log.log_to_screen(lantz.log.DEBUG)
with N842PE(args.port) as inst:
if args.interactive:
from lantz.ui.qtwidgets import start_test_app
start_test_app(inst)
else:
print('Testing instrument...')
print(inst.scale)
inst.scale = '30u'
print(inst.scale)
time.sleep(1)
print(inst.scale)
inst.scale = '300n'
print(inst.scale)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment