pyvisa / pyvisa-py

A pure python PyVISA backend
https://pyvisa-py.readthedocs.io
MIT License
288 stars 124 forks source link

[COM] Communication issue with HP 54542C (oscilloscope) #290

Open blackbit42 opened 3 years ago

blackbit42 commented 3 years ago

Instrument details

Output of pyvisa-info

/usr/local/lib/python3.7/dist-packages/visa.py:23: FutureWarning: The visa module provided by PyVISA is being deprecated. You can replace `import visa` by `import pyvisa as visa` to achieve the same effect.

The reason for the deprecation is the possible conflict with the visa package provided by the https://github.com/visa-sdk/visa-python which can result in hard to debug situations.
  FutureWarning,
Machine Details:
   Platform ID:    Linux-4.19.0-12-686-i586-with-debian-10.6
   Processor:      

Python:
   Implementation: CPython
   Executable:     /usr/bin/python3
   Version:        3.7.3
   Compiler:       GCC 8.3.0
   Bits:           32bit
   Build:          Jul 25 2020 13:03:44 (#default)
   Unicode:        UCS4

PyVISA Version: 1.11.3

Backends:
   ivi:
      Version: 1.11.3 (bundled with PyVISA)
      Binary library: Not found
   py:
      Version: 0.5.1
      ASRL INSTR:
         Please install PySerial (>=3.0) to use this resource type.
         No module named 'serial'
      USB INSTR:
         Please install PyUSB to use this resource type.
         No module named 'usb'
      USB RAW:
         Please install PyUSB to use this resource type.
         No module named 'usb'
      TCPIP INSTR: Available 
      TCPIP SOCKET: Available 
      GPIB INSTR: Available via Linux GPIB (b'4.3.3')
      GPIB INTFC: Available via Linux GPIB (b'4.3.3')

I managed to get a screenshot as a HPGL file out of the scope after some fiddling, though I am not doing it by the book.

import pyvisa as visa

rm = visa.ResourceManager('@py')
lib = rm.visalib

instr = rm.open_resource('GPIB0::7::INSTR')
instr.read_termination = ''
instr.write_termination = '\n'

# 10 CLEAR 707   ! Initialize instrument interface
# 15 HARDcopy:mode plot
# 20 ON INTR 7,5 GOTO 160      ! Exit printing routine after SRQ
# 30 ENABLE INTR 7;2    ! Enable SRQ on bus #7

instr.write('*CLS?')  # 40 OUTPUT 707;"*CLS"  ! Clear status data structures
instr.write('*ESE 1')  # 50 OUTPUT 707;"*ESE 1"! Enable OPC
instr.write('*SRE 32')  # 60 OUTPUT 707;"*SRE 32"      ! Enable Event Status Register
instr.write(':HARDCOPY:PAGE AUTOMATIC')  # 70 OUTPUT 707;":HARDCOPY:PAGE AUTOMATIC"
instr.write(':HARDCOPY:LENGTH 12')  # 80 OUTPUT 707;":HARDCOPY:LENGTH 12"
instr.write(':PLOT?;*OPC')  # 90 OUTPUT 707;":PLOT?;*OPC"  ! Set OPC when plot is complete
# TODO missing: 100 SEND 7;UNT UNL    ! Clear bus
# TODO missing: 110 SEND 7;TALK 7     ! Scope to talk mode
# TODO missing: 120 SEND 7;LISTEN 1   ! Printer to listen mode
# Not implemented lib.gpib_control_atn(instr.session, visa.constants.VI_GPIB_ATN_DEASSERT)  # 130 SEND 7;DATA ! Lower ATN line @ controller
# Not implemented instr.wait_for_srq()
plot = instr.read_raw()
with open("plot", 'wb') as f:
    f.write(plot)

instr.close()

Please see the 'missing' and 'Not implemented' comments above. How do I go about clearing the bus, explicitly putting the scope in talk mode, myself in listen mode, setting the ATN line and waiting for SRQ? Also, does what I am doing above appear to be correct? I guess the commented stuff (lines 10..30) are irrelevant as that is HP BASIC for the controller.

MatthieuDartiailh commented 3 years ago

Currently there is no support for event in pyvisa-py. It could be added but due to limited access to proper hardware and time it has not been done (there is a WIP PR but it has been dormant for some time #175 ). For the ATN line you need to access the interface resource not the instrument and may need #276 which is waiting for me to have time to properly test it. Anything you are willing to contribute or test will be appreciated. For the rest you could send low level gpib commands but I am not sure how much is already abstracted away by VISA.