Skip to content

Commit

Permalink
sessions: improve handling of open connection error for TCPIP resources
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthieuDartiailh committed Sep 17, 2024
1 parent 0fb4d3f commit db8f22b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 19 deletions.
3 changes: 3 additions & 0 deletions pyvisa_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
"""

import logging
import sys

LOGGER = logging.getLogger("pyvisa.pyvisa-py")

if sys.version_info >= (3, 8):
from importlib.metadata import PackageNotFoundError, version
else:
Expand Down
5 changes: 4 additions & 1 deletion pyvisa_py/highlevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ def open(
parsed.interface_type_const, parsed.resource_class
)

sess = cls(session, resource_name, parsed, open_timeout)
try:
sess = cls(session, resource_name, parsed, open_timeout)
except sessions.OpenError as e:
return 0, e.error_code

return self._register(sess), StatusCode.success

Expand Down
7 changes: 7 additions & 0 deletions pyvisa_py/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
T = TypeVar("T", bound=Type["Session"])


class OpenError(Exception):
"""Custom exception signaling we failed to open a resource."""

def __init__(self, error_code: int = StatusCode.error_resource_not_found):
self.error_code = error_code


class UnknownAttribute(Exception):
"""Custom exception signaling a VISA attribute is not supported."""

Expand Down
55 changes: 37 additions & 18 deletions pyvisa_py/tcpip.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
from pyvisa import attributes, constants, errors, rname
from pyvisa.constants import BufferOperation, ResourceAttribute, StatusCode

from . import common
from . import common, LOGGER
from .protocols import hislip, rpc, vxi11
from .sessions import Session, UnknownAttribute, VISARMSession
from .sessions import OpenError, Session, UnknownAttribute, VISARMSession


# Let psutil be optional dependency
try:
Expand Down Expand Up @@ -135,17 +136,25 @@ def after_parsing(self) -> None:
else:
sub_address = self.parsed.lan_device_name
port = 4880
self.interface = hislip.Instrument(
self.parsed.host_address,
open_timeout=(
self.open_timeout * 1000.0
if self.open_timeout is not None
else self.open_timeout
),
timeout=self.timeout,
port=port,
sub_address=sub_address,
)

try:
self.interface = hislip.Instrument(
self.parsed.host_address,
open_timeout=(
self.open_timeout * 1000.0
if self.open_timeout is not None
else self.open_timeout
),
timeout=self.timeout,
port=port,
sub_address=sub_address,
)
except OSError as e:
LOGGER.exception(
f"Failed to open HiSLIP connection to {self.parsed.host_address} "
f"on port {port} with lan device name {sub_address}"
)
raise OpenError() from e

# initialize the constant attributes
self.attrs[ResourceAttribute.dma_allow_enabled] = constants.VI_FALSE
Expand Down Expand Up @@ -482,7 +491,10 @@ def after_parsing(self) -> None:
try:
self.interface = Vxi11CoreClient(host_address, port, self.open_timeout)
except rpc.RPCError:
raise errors.VisaIOError(constants.VI_ERROR_RSRC_NFOUND)
LOGGER.exception(
f"Failed to open VX11 connection to {host_address} on port {port}"
)
raise OpenError()

# vxi11 expect all timeouts to be expressed in ms and should be integers
self.lock_timeout = 10000
Expand Down Expand Up @@ -511,7 +523,7 @@ def close(self) -> StatusCode:
try:
self.interface.destroy_link(self.link)
except (errors.VisaIOError, socket.error, rpc.RPCError) as e:
print("Error closing VISA link: {}".format(e))
LOGGER.error("Error closing VISA link: {}".format(e))

self.interface.close()
self.link = 0
Expand Down Expand Up @@ -870,9 +882,16 @@ def after_parsing(self) -> None:
else:
port = 1861

self.interface = pyvicp.Client(
self.parsed.host_address, port, timeout=self.timeout
)
try:
self.interface = pyvicp.Client(
self.parsed.host_address, port, timeout=self.timeout
)
except OSError as e:
LOGGER.exception(
f"Failed to open VICP connection to {self.parsed.host_address} "
f"on port {port}"
)
raise OpenError() from e

# initialize the constant attributes
for name in ("SEND_END_EN", "TERMCHAR", "TERMCHAR_EN"):
Expand Down

0 comments on commit db8f22b

Please sign in to comment.