pyvisa / pyvisa-py

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

Absoulte beginner - some basic help with pyvisa #303

Closed jlozanol closed 3 years ago

jlozanol commented 3 years ago

Hi Team,

Sorry if this is not the right forum to put this question. I'm brand new to general programming and I'm trying to understand how to communicate with some devices via Python. My short term goal is to create a python program to communicate a Thorlabs Spectrometer CCS175 and I just installed the Instrumental library.

I just tried to run the list of instruments to see if the spectrometer appeared and I just got the following:

(instruments) PS C:\> python
Python 3.9.4 (default, Apr  9 2021, 11:43:21) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from instrumental import instrument, list_instruments
>>> paramsets = list_instruments()
C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\visa.py:13: 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.
  warnings.warn(
C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\instrumental\drivers\motion\ecc100.py:24: UserWarning: Driver 'instrumental.drivers.motion.ecc100' is out of date and incompatible with the current Instrumental core. Pull requests fixing this are welcome.
  warnings.warn(
>>> paramsets
[] # seems instrumental is not able to pick the devices that are connected...

Then I checked what I have available on visa:

(instruments) PS C:\> python -m visa info
C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\visa.py:13: 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.
  warnings.warn(
Machine Details:
   Platform ID:    Windows-10-10.0.18362-SP0
   Processor:      Intel64 Family 6 Model 142 Stepping 12, GenuineIntel

Python:
   Implementation: CPython
   Executable:     C:\Users\s3853339\Miniconda3\envs\instruments\python.exe
   Version:        3.9.4
   Compiler:       MSC v.1916 64 bit (AMD64)
   Bits:           64bit                                                                                                                                                       Build:          Apr  9 2021 11:43:21 (#default)                                                                                                                             Unicode:        UCS4                                                                                                                                                                                                                                                                                                                                 PyVISA Version: 1.11.3

Backends:
   ivi:
      Version: 1.11.3 (bundled with PyVISA)
      #1: C:\WINDOWS\system32\visa32.dll:
         found by: auto
         bitness: 64
         Vendor: National Instruments
         Impl. Version: National Instruments
         Spec. Version: National Instruments
      #2: C:\WINDOWS\system32\visa64.dll:
         found by: auto
         bitness: 64
         Vendor: National Instruments
         Impl. Version: National Instruments
         Spec. Version: National Instruments
   py:
      Version: 0.5.2
      ASRL INSTR:
         Please install PySerial (>=3.0) to use this resource type.
         No module named 'serial'
      USB INSTR: Available via PyUSB (1.1.1). Backend: libusb0
      USB RAW: Available via PyUSB (1.1.1). Backend: libusb0
      TCPIP INSTR: Available
      TCPIP SOCKET: Available
      GPIB INSTR:
         Please install linux-gpib (Linux) or gpib-ctypes (Windows, Linux) to use this resource type. Note that installing gpib-ctypes will give you access to a broader range of funcionality.
         No module named 'gpib'

So it looks PyUSB is configured correctly. I tried to check what's going on with PyUSB:

>>> import usb.core
>>> dev = usb.core.find()
>>> print(dev)
DEVICE ID 1313:8087 on Bus 000 Address 001 =================
 bLength                :   0x12 (18 bytes)
 bDescriptorType        :    0x1 Device
 bcdUSB                 :  0x200 USB 2.0
 bDeviceClass           :    0x0 Specified at interface
 bDeviceSubClass        :    0x0
 bDeviceProtocol        :    0x0
 bMaxPacketSize0        :   0x40 (64 bytes)
 idVendor               : 0x1313
 idProduct              : 0x8087
 bcdDevice              :    0x0 Device 0.0
 iManufacturer          :    0x1 Thorlabs
 iProduct               :    0x2 CCS175
 iSerialNumber          :    0x3 M00601006
 bNumConfigurations     :    0x1
  CONFIGURATION 1: 500 mA ==================================
   bLength              :    0x9 (9 bytes)
   bDescriptorType      :    0x2 Configuration
   wTotalLength         :   0x2e (46 bytes)
   bNumInterfaces       :    0x1
   bConfigurationValue  :    0x1
   iConfiguration       :    0x0
   bmAttributes         :   0x80 Bus Powered
   bMaxPower            :   0xfa (500 mA)
    INTERFACE 0: Vendor Specific ===========================
     bLength            :    0x9 (9 bytes)
     bDescriptorType    :    0x4 Interface
     bInterfaceNumber   :    0x0
     bAlternateSetting  :    0x0
     bNumEndpoints      :    0x4
     bInterfaceClass    :   0xff Vendor Specific
     bInterfaceSubClass :    0x0
     bInterfaceProtocol :    0x0
     iInterface         :    0x0
      ENDPOINT 0x2: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x2 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x4: Bulk OUT ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :    0x4 OUT
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x86: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x86 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0
      ENDPOINT 0x88: Bulk IN ===============================
       bLength          :    0x7 (7 bytes)
       bDescriptorType  :    0x5 Endpoint
       bEndpointAddress :   0x88 IN
       bmAttributes     :    0x2 Bulk
       wMaxPacketSize   :  0x200 (512 bytes)
       bInterval        :    0x0

And it's able to see the Spectrometer! However, for some reason pyvisa is not picking this information, I think???

>>> import pyvisa as visa
>>> rm = visa.ResourceManager('@py')
>>> rm.list_resources()
()

Can you tell me what I'm missing here?

Thanks heaps for your help!

Regards,

jlozanol commented 3 years ago

Also, I know from NI MAX that this spectrometer has the following VISA resource name...

image

MatthieuDartiailh commented 3 years ago

By default the resource manager only list INSTR resources. To get it to list all present resources, you can do rm.list_resources('?*'). But since you know the address you can directly pass the address to open_resource

jlozanol commented 3 years ago

Thx @MatthieuDartiailh !

I tried both things and still no success:

>>> rm.list_resources('?*')
()
>>> test=rm.open_resource('USB0::0x1313::0x8087::M00601006::RAW')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\highlevel.py", line 3304, in open_resource
    res.open(access_mode, open_timeout)
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\resources\resource.py", line 297, in open
    self.session, status = self._resource_manager.open_bare_resource(
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\highlevel.py", line 3232, in open_bare_resource
    return self.visalib.open(self.session, resource_name, access_mode, open_timeout)
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa_py\highlevel.py", line 167, in open
    sess = cls(session, resource_name, parsed, open_timeout)
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa_py\sessions.py", line 323, in __init__
    self.after_parsing()
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa_py\usb.py", line 81, in after_parsing
    self.interface = self._intf_cls(
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa_py\protocols\usbraw.py", line 37, in __init__
    super(USBRawDevice, self).__init__(vendor, product, serial_number, **kwargs)
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa_py\protocols\usbtmc.py", line 199, in __init__
    raise ValueError("No device found.")
ValueError: No device found.
MatthieuDartiailh commented 3 years ago

Since you are running on windows with NI-VISA installed you have no reason to run through pyvisa-py do not pass any argument to the ResourceManager and it will pick the NI-VISA which should be able to connect since NI-MAX manage to do so.

jlozanol commented 3 years ago

Legend! Thanks so much @MatthieuDartiailh for your help!

Regards,

jlozanol commented 3 years ago

Hey @MatthieuDartiailh,

Sorry again, re this same device. After opening it I'm not able to get some information form the spectrometer:

>>> test=rm.open_resource('USB0::0x1313::0x8087::M00601006::RAW')
>>> test.timeout = 10000   #just adding a random number, it didn't affect on anything changing this parameter or not adding it at all
>>> test.read_termination = '\n'
>>> test.write_termination = '\n'
>>> test.query('*IDN?')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\resources\messagebased.py", line 638, in query
    self.write(message)
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\resources\messagebased.py", line 197, in write
    count = self.write_raw(message.encode(enco))
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\resources\messagebased.py", line 157, in write_raw
    return self.visalib.write(self.session, message)[0]
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\ctwrapper\functions.py", line 2796, in write
    ret = library.viWrite(session, data, len(data), byref(return_count))
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\ctwrapper\highlevel.py", line 222, in _return_handler
    return self.handle_return_value(session, ret_value)  # type: ignore
  File "C:\Users\s3853339\Miniconda3\envs\instruments\lib\site-packages\pyvisa\highlevel.py", line 251, in handle_return_value
    raise errors.VisaIOError(rv)
pyvisa.errors.VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.

Tracking what was performed on #397 and #513 but I don't get what could it be wrong. Tried with NI VISA Test Panel although I think it's sending me weird information after the *IDN?

image

Just checked in the CCS175 manual and unfortunately cannot see any command list or parameter list to follow along.

Thoughts?

Thanks again!

MatthieuDartiailh commented 3 years ago

USB RAW devices do not use the usual text based interface but a lower level vendor specific protocol. However Throlabs does not describe in the manual, since they want you to use their library (see section 5 of the manual). You can have a look at the C source code of the NI-VISA based driver (C:\Program Files\IVI Foundation\VISA\WinNT\ThorlabsCCSseries\TLCCS.c according to the manual) and infer the protocol from there. If you share the file I may be able to provide some guidance.

jlozanol commented 3 years ago

Attaching the file as txt.

Thanks heaps!

TLCCS.txt

MatthieuDartiailh commented 3 years ago

I will try to come back to you early next week. Ping me if you do not hear from me.

jlozanol commented 3 years ago

Hi @MatthieuDartiailh,

No need for that anylonger, I have successfully communicated with the spectrometer via Instrumental library. Thanks heaps for all the help!