pythics - interfacing to lab instruments tutorial€¦ ·  ·...

12
Page | 1 Pythics Interfacing Tutorial (Windows Operating System) 1. Overview Pythics is a tool for creating simple interactive interfaces to laboratory instruments and numerical simulations. It is intended as an open source alternative to LabVIEW. The front end, or graphical user interface (GUI), is specified using HTML/CSS (HyperText Markup Language/Cascading Style Sheets), which is commonly used to format web pages. Pythics interfaces to instruments through Python code. This code uses functions in the PyVisa library to communicate with laboratory instruments. The instructions used to control them are written in SCPI (Standard Commands for Programmable Instruments). SCPI is supported by most of the manufacturers of programmable instruments; however, some details are specific to a given manufacturer. The best place to start is to look at the manuals that came with the instrument you are trying to control, especially the programming manual. If you have no information, try searching the internet for SCPI examples for your specific instrument. Note that Python is a derivative of the C/C++ programming languages and examples written in these languages can be helpful. 2. Interface Test Tool Before writing a Pythics app to interface with an instrument you should play with some interactive tools that allow you to remotely control the instrument by manually typing SCPI commands. Examples of these are National Instrument’s MAX (Measurement and Automation Explorer) and Keysight’s Command Expert. For example, I found a GW Instek GPD3303S power supply on a shelf in my lab. It has a USB port on the back, so I am going to try to interface it to my laptop. I do not have a manual for this device and know nothing about it. When I plugged it into my laptop, Windows installed a device driver. When I use NI’s MAX, I see the power supply as a VISA device under Devices and Interfaces. The screen shot below shows what is displayed when I click on the ASRL3::INSTR“COM3” entry in the list. In the information panel MAX tells me that the VISA Resource Name for this device is ‘ASRL3::INSTR’. The VISA Resource Name is an important piece of information we will need later on when we write the Python code.

Upload: lamdieu

Post on 06-Apr-2018

228 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  1    

Pythics  -­‐  Interfacing  Tutorial  (Windows  Operating  System)  

1. Overview  Pythics  is  a  tool  for  creating  simple  interactive  interfaces  to  laboratory  instruments  and  numerical  simulations.  It  is  intended  as  an  open  source  alternative  to  LabVIEW.  The  front  end,  or  graphical  user  interface  (GUI),  is  specified  using  HTML/CSS  (HyperText  Markup  Language/Cascading  Style  Sheets),  which  is  commonly  used  to  format  web  pages.    

Pythics  interfaces  to  instruments  through  Python  code.  This  code  uses  functions  in  the  PyVisa  library  to  communicate  with  laboratory  instruments.  The  instructions  used  to  control  them  are  written  in  SCPI  (Standard  Commands  for  Programmable  Instruments).  SCPI  is  supported  by  most  of  the  manufacturers  of  programmable  instruments;  however,  some  details  are  specific  to  a  given  manufacturer.  The  best  place  to  start  is  to  look  at  the  manuals  that  came  with  the  instrument  you  are  trying  to  control,  especially  the  programming  manual.  If  you  have  no  information,  try  searching  the  internet  for  SCPI  examples  for  your  specific  instrument.  Note  that  Python  is  a  derivative  of  the  C/C++  programming  languages  and  examples  written  in  these  languages  can  be  helpful.  

2.  Interface  Test  Tool  Before  writing  a  Pythics  app  to  interface  with  an  instrument  you  should  play  with  some  interactive  tools  that  allow  you  to  remotely  control  the  instrument  by  manually  typing  SCPI  commands.  Examples  of  these  are  National  Instrument’s  MAX  (Measurement  and  Automation  Explorer)  and  Keysight’s  Command  Expert.  

For  example,  I  found  a  GW  Instek  GPD-­‐3303S  power  supply  on  a  shelf  in  my  lab.  It  has  a  USB  port  on  the  back,  so  I  am  going  to  try  to  interface  it  to  my  laptop.    I  do  not  have  a  manual  for  this  device  and  know  nothing  about  it.  When  I  plugged  it  into  my  laptop,  Windows  installed  a  device  driver.  When  I  use  NI’s  MAX,  I  see  the  power  supply  as  a  VISA  device  under  Devices  and  Interfaces.  The  screen  shot  below  shows  what  is  displayed  when  I  click  on  the  ASRL3::INSTR“COM3”  entry  in  the  list.  In  the  information  panel  MAX  tells  me  that  the  VISA  Resource  Name  for  this  device  is  ‘ASRL3::INSTR’.  The  VISA  Resource  Name  is  an  important  piece  of  information  we  will  need  later  on  when  we  write  the  Python  code.  

Page 2: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  2    

 

When  I  click  on  the  Open  VISA  Test  Panel  link  I  see  the  following  

 

Finally  I  click  on  the  Input/Output  menu  item  at  the  top  of  the  panel  and  get  to  an  interactive  input/output  tool  that  will  allow  me  to  communicate  with  the  VISA  instrument.  

 

Page 3: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  3    

 

There  is  a  drop-­‐down  menu  at  the  top  of  the  panel  that  allows  me  to  select  commonly  used  SCIP  commands,  or  I  can  type  any  command  I  want  to  directly  into  the  command  box.  Here  is  a  table  of  commonly  used  SCIP  commands  (from  NI  website:  http://www.ni.com/white-­‐paper/3388/en/  ).  

 

The  commands  that  end  in  a  question  mark  are  queries  and  return  a  value.  You  use  the  Query  button  to  send  these  commands  and  retrieve  the  instrument’s  response.  For  example,  when  I  query  the  command  *IDN?  I  get  back  the  identity  of  the  device,  highlighted  in  blue  in  the  figure  below.  

Page 4: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  4    

 

Next  I  locate  a  manual  for  the  GPD-­‐3303S  on  the  internet  that  gives  me  a  table  of  the  SCIP  commands  it  recognizes.  For  example,  the  command  ‘VSET1:2.5\n’  sets  the  output  voltage  on  channel  1  to  2.5V.  This  command  is  sent  with  the  Write  button  because  it  does  not  return  anything.  The  ‘\n’  is  the  new  line  character.  Then  the  ‘OUT1\n’  command  is  sent  to  turn  the  output  on.    

At  this  point  I  know  that  communication  is  working  between  the  laptop  and  the  instrument,  and  know  a  couple  of  commands  that  actually  work.  The  next  step  is  to  try  to  do  this  using  Python.  But  first,  I  need  to  close  the  MAX  VISA  Test  Panel  so  it  will  release  control  of  the  instrument.  If  you  fail  to  release  control  of  a  VISA  Resource  Name,  running  a  different  app  will  give  the  following  error:    

Page 5: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  5    

Error  opening  resource:  ASRL3::INSTR  VISA:  (Hex  0xBFFF0072)    The  resource  is  valid  but  VISA  cannot  currently  access  it.  

 3. Python  Console  

Now  we  are  going  to  try  using  Python  and  the  PyVisa  library  to  replicate  the  actions  we  executed  with  the  interface  test  tool.  We  need  to  do  this  to  verify  the  correct  Python  instructions  because  the  PyVisa  library  has  gone  through  several  versions  and  we  cannot  assume  that  what  worked  before  will  work  now.  Fortunately,  the  Spyder  IDE  has  a  Python  console  that  allows  us  to  interactively  execute  Python  statements.  We  will  use  this  to  prototype  the  correct  Python  code.    The  Python  console  is  in  the  lower  right  corner  of  Spyder.  Python  commands  are  entered  at  the  ‘>>>’  prompt.      

   The  first  thing  we  need  to  know  is  the  commands  to  enter  in  the  console.  I  am  using  PyVisa  version  1.80  and  found  this  example  code  

Page 6: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  6    

 on  the  PyVisa  website  https://pyvisa.readthedocs.org/en/master/tutorial.html#an-­‐example    In  the  rm.open_resource()  statement  we  will  enter  the  VISA  Resource  Name  ‘ASRL3::INSTR’  that  we  obtained  from  MAX.  Here  is  a  screen  shot  of  the  console  session    

   At  the  completion  of  these  commands  I  measured  the  output  of  the  power  supply  and  it  was  1.23  V.    

4. Example  Pythics  App  We  are  all  set  to  write  a  Pythics  app,  called  dc_ramp  that  linearly  varies  the  DC  output  of  the  power  supply  from  Vstart  to  Vend  in  steps  of  Vdelta  with  a  dwell  time  of  Tdelta  at  each  step.    We  will  begin  by  cloning  the  function_plotter  app  found  in  the  Pythics  examples.  Copy  the  function_plotter  files,  paste  them  into  MyPythicsApps  in  your  Document  folder,  and  rename  them  to  dc_ramp.  Open  Spyder  and  change  all  occurrences  of  function_plotter  to  dc_ramp  in  both  the  XML  and  Python  files.      In  the  XML  file  we  rip  out  the  plot  object,  add  a  number  box  for  Tdelta  to  the  table,  and  change  the  number  box  headers  and  id’s  to  reflect  the  names  parameters  listed  above.  We  also  remove  the  function  text  box  object.  The  result  is  shown  below.      

Page 7: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  7    

 

 

Page 8: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  8    

You  may  notice  the  ParameterLoader  object  at  the  bottom  of  the  XML  file.  This  is  used  to  store  default  values  for  the  app  parameters  that  loaded  automatically  when  the  app  is  launched.  This  is  very  convenient.  To  set  the  default  parameters,  enter  the  desired  value  for  each  parameter  into  the  GUI,  then  click  on  the  Save  As  Defaults  entry  in  the  Pythics  Parameters  menu.    In  the  Python  file  we  added  imports  of  the  visa  and  time  libraries,  and  created  an  instance  of  the  power  supply  with  initial  output  set  to  off.  In  the  clear  function  we  delete  initialization  of  plot.  We  added  a  reset  of  the  power  supply  output  to  off.  In  the  run  function  we  removed  the  parameters  function  and  plot,  and  renamed  the  others  to  Vstart,  Vend,  and  Vdelta;  and  added  a  new  parameter  called  Tdelta.  Here  is  the  result.  The  final  output  voltage  is  left  on  at  the  end  of  the  ramp.    

 

   

Page 9: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  9    

And  here  is  a  screen  shot  of  the  GUI.  

   When  I  click  the  Run  button,  I  see  the  voltage  on  the  attached  DMM  increase  steadily  with  time  and  then  level  off  at  the  final  value  of  5.0  V.  When  I  click  the  Clear  button  the  ‘Done’  message  disappears  and  the  output  voltage  drops  to  0  V.    Before  we  move  on  and  look  at  interfacing  other  instruments  we  need  to  look  more  closely  at  VISA  Resource  Names.  You  will  notice  that  in  the  example  the  VISA  Resource  Name  was  hard-­‐coded  into  Python.  What  happens  when  I  swap  the  current  power  supply  with  another  GPD-­‐3303S  power  supply?  I  tried  this,  and  the  VISA  Resource  Name  changed  to  'ASRL4::INSTR'.  I  tried  another  one  and  its  VISA  Resource  Name  was  'ASRL5::INSTR'.  Then  I  went  back  to  the  original  power  supply  and  its  VISA  Resource  Name  was  again  to  'ASRL3::INSTR'.  Next  I  tried  moving  the  USB  cable  to  different  ports  on  my  laptop.  The  VISA  Resource  Name  did  not  change.      So,  if  you  have  a  number  of  otherwise  identical  instruments,  they  will  have  different  VISA  Resource  Names,  and  if  you  do  not  use  the  same  instrument  each  time,  you  will  need  to  open  the  Python  code  and  update  the  argument  of  the  rm.open_resource()  function  each  time  you  run  the  app.      The  code  snippet  below  shows  another  way  of  instantiating  the  instrument  that  avoids  this  problem.    

   

If  you  attach  only  one  VISA  instrument  to  your  laptop  it  will  be  the  first  entry  in  the  resource  list.  Now  the  dc_ramp  will  work  with  any  GPD-­‐3303S  DMM  you  grab  off  the  shelf.  The  problem  arises  

Page 10: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  10    

when  you  attach  more  than  one  GPD-­‐3303S  DMM  to  your  laptop.  They  will  appear  in  the  VISA  resource  list  in  alphabetic  order  and  you  will  need  to  put  stickers  on  them  so  you  can  visually  tell  them  apart.  

 5. Other  Instruments  

Here  are  some  examples  of  interfacing  Pythics  to  other  laboratory  instruments.    a. Agilent  33210A  Function  Generator  

An  instance  of  the  function  generator  is  created  using  the  procedure  followed  for  the  power  supply  example.  The  function  generator  has  a  richer  set  of  SCIP  commands.  These  are  listed  in  Chapter  4.  Remote  Interface  Reference  of  the  33210A  manual    http://literature.cdn.keysight.com/litweb/pdf/33210-­‐90001.pdf?id=1489923    Here  is  an  example  code  snippet.    

   

 b. Agilent  34450A  DMM  

You  can  find  a  Programmer’s  Reference  Help  document  for  the  34450A  at    http://www.keysight.com/main/editorial.jspx?cc=US&lc=eng&ckey=2301607&nid=-­‐536902435.1043561.00&id=2301607    Here  is  an  example  of  the  relevant  code.    

Page 11: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  11    

   

c. Tektronix    2012C    Digital  Storage  Oscilloscope  In  general  a  digital  storage  oscilloscope  is  more  complex  than  any  other  instrument  to  which  you  will  interface.  First,  you  will  be  getting  an  array  of  values  back  from  each  query,  instead  of  a  single  value.  For  the  Tektronix  2012C  you  will  get  2048  values.  For  an  Agilent  Infiniivision  4000  series  scope  you  can  get  up  to  2  million  values.  Second,  the  values  you  get  are  the  raw  numbers  from  the  scope’s  digitizer.  You  will  also  need  to  download  header  information  from  the  scope  that  will  allow  you  to  convert  these  into  actual  voltages.  But  a  digital  storage  oscilloscope  is  a  very  fast,  high-­‐bandwidth  digitizer  that  is  cheap  for  its  performance  level  and  is  readily  available  in  just  about  every  lab.    I  found  the  following  code  example  on  the  internet  for  the  Tektronix  scope.  It  will  download  the  waveform  from  the  scope  and  return  a  two-­‐column  array  of  times  and  voltages.  

Page 12: Pythics - Interfacing to Lab Instruments Tutorial€¦ ·  · 2015-07-19Pythics-%Interfacing%Tutorial% ... Microsoft Word - Pythics - Interfacing to Lab Instruments Tutorial.docx

P a g e  |  12