| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import pyvisa |
| 26 |
from serial.tools import list_ports |
| 27 |
|
| 28 |
|
| 29 |
def list_resources(): |
| 30 |
""" |
| 31 |
Prints the available resources, and returns a list of VISA resource names |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
resources = list_resources() |
| 36 |
#prints (e.g.) |
| 37 |
#0 : GPIB0::22::INSTR : Agilent Technologies,34410A,****** |
| 38 |
#1 : GPIB0::26::INSTR : Keithley Instruments Inc., Model 2612, ***** |
| 39 |
dmm = Agilent34410(resources[0]) |
| 40 |
|
| 41 |
""" |
| 42 |
rm = pyvisa.ResourceManager() |
| 43 |
instrs = rm.list_resources() |
| 44 |
for n, instr in enumerate(instrs): |
| 45 |
# trying to catch errors in communication |
| 46 |
try: |
| 47 |
res = rm.open_resource(instr) |
| 48 |
# try to avoid errors from *idn? |
| 49 |
try: |
| 50 |
# noinspection PyUnresolvedReferences |
| 51 |
idn = res.query('*idn?')[:-1] |
| 52 |
except pyvisa.Error: |
| 53 |
idn = "Not known" |
| 54 |
finally: |
| 55 |
res.close() |
| 56 |
print(n, ":", instr, ":", idn) |
| 57 |
except pyvisa.VisaIOError as e: |
| 58 |
print(n, ":", instr, ":", "Visa IO Error: check connections") |
| 59 |
print(e) |
| 60 |
rm.close() |
| 61 |
return instrs |
| 62 |
|
| 63 |
|
| 64 |
def find_serial_port(vendor_id=None, product_id=None, serial_number=None): |
| 65 |
"""Find the VISA port name of the first serial device with the given USB information. |
| 66 |
|
| 67 |
Use `None` as a value if you do not want to check for that parameter. |
| 68 |
|
| 69 |
.. code-block:: python |
| 70 |
|
| 71 |
resource_name = find_serial_port(vendor_id=1256, serial_number="SN12345") |
| 72 |
dmm = Agilent34410(resource_name) |
| 73 |
|
| 74 |
:param int vid: Vendor ID. |
| 75 |
:param int pid: Product ID. |
| 76 |
:param str sn: Serial number. |
| 77 |
:return str: Port as a VISA string for a serial device (e.g. "ASRL5" or "ASRL/dev/ttyACM5"). |
| 78 |
""" |
| 79 |
for port in sorted(list_ports.comports()): |
| 80 |
if ((vendor_id is None or port.vid == vendor_id) |
| 81 |
and (product_id is None or port.pid == product_id) |
| 82 |
and (serial_number is None or port.serial_number == str(serial_number))): |
| 83 |
# remove "COM" from windows serial port names. |
| 84 |
port_name = port.device.replace("COM", "") |
| 85 |
return "ASRL" + port_name |
| 86 |
raise AttributeError("No device found for the given data.") |