pyvisa/pyvisa-py

Vxi11 fails to decode buffer value returned by an instrument (BK9100)

tristanlorriaux opened this issue ยท 5 comments

Hi there ๐Ÿš€,

The error :

We encounter a major bug with pyvisa-py.
Here are the different pyvisa first commands in iPython :

$ import pyvisa as visa
$ rm = visa.ResourceManager('@py')
$ bk = rm.open_resource('TCPIP0::192.168.0.133::inst0::INSTR')

Then $ bk.query('*IDN?') works and returns :

OUT: 'B&K PRECISION,9185B,508H19110,0.09\r\n'

which is our instrument.
But, when we query $ bk.query('MEAS:CURR?'), it fails.

To be more explicit, the $ bk.write('MEAS:CURR?') or for instance $ bk.write('OUT?') work and return with debug mode ($visa.log_to_screen()) :

$ bk.write('MEAS:VOLT?')

2023-02-16 15:00:13,318 - pyvisa - DEBUG - Make call 11, (12984800, 2000, 10000, 8, b'MEAS:VOLT?\r\n'), <bound method Vxi11Packer.pack_device_write_parms of <pyvisa_py.protocols.vxi11.Vxi11Packer object at 0x000001ed1758a3d8>>, <bound method Vxi11Unpacker.unpack_device_write_resp of <pyvisa_py.protocols.vxi11.Vxi11Unpacker object at 0x000001ed1758a410>>
b"\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x07\xaf\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc6!\xe0\x00\x00\x07\xd0\x00\x00'\x10\x00\x00\x00\x08\x00\x00\x00\x0cMEAS:VOLT?\r\n"
2023-02-16 15:00:13,318 - pyvisa - DEBUG - Sending record through <socket.socket fd=9856, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.0.14', 57717), raddr=('192.168.0.133', 49155)>: b"\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x07\xaf\x00\x00\x00\x01\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc6!\xe0\x00\x00\x07\xd0\x00\x00'\x10\x00\x00\x00\x08\x00\x00\x00\x0cMEAS:VOLT?\r\n"
2023-02-16 15:00:13,318 - pyvisa - DEBUG - RawTCPClient: procedure type 11
2023-02-16 15:00:13,318 - pyvisa - DEBUG - received b'\x80\x00\x00 '
2023-02-16 15:00:13,318 - pyvisa - DEBUG - received b'\x00\x00\x00\x06\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c'
2023-02-16 15:00:13,318 - pyvisa - DEBUG - Received record through <socket.socket fd=9856, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.0.14', 57717), raddr=('192.168.0.133', 49155)>: bytearray(b'\x00\x00\x00\x06\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c')
b'\x00\x00\x00\x06\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0c'

OUT : 12

But if we read(), raw_read(), read_ascii_values(), read_binary_values(), read_bytes(), it fails and return the EOF error :

2023-02-16 15:00:20,168 - pyvisa - DEBUG - TCPIP0::192.168.0.133::inst0::INSTR - reading 20480 bytes (last status <StatusCode.success_max_count_read: 1073676294>)
20480
2023-02-16 15:00:20,168 - pyvisa - DEBUG - Make call 12, (12984800, 20480, 2000, 10000, 0, 0), <bound method Vxi11Packer.pack_device_read_parms of <pyvisa_py.protocols.vxi11.Vxi11Packer object at 0x000001ed1758a3d8>>, <bound method Vxi11Unpacker.unpack_device_read_resp of <pyvisa_py.protocols.vxi11.Vxi11Unpacker object at 0x000001ed1758a410>>
b"\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x07\xaf\x00\x00\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc6!\xe0\x00\x00P\x00\x00\x00\x07\xd0\x00\x00'\x10\x00\x00\x00\x00\x00\x00\x00\x00"
2023-02-16 15:00:20,168 - pyvisa - DEBUG - Sending record through <socket.socket fd=9856, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.0.14', 57717), raddr=('192.168.0.133', 49155)>: b"\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x02\x00\x06\x07\xaf\x00\x00\x00\x01\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc6!\xe0\x00\x00P\x00\x00\x00\x07\xd0\x00\x00'\x10\x00\x00\x00\x00\x00\x00\x00\x00"
2023-02-16 15:00:20,168 - pyvisa - DEBUG - RawTCPClient: procedure type 12
2023-02-16 15:00:20,178 - pyvisa - DEBUG - received b'\x80\x00\x00*'
2023-02-16 15:00:20,178 - pyvisa - DEBUG - received b'\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x060.00\r\n'
2023-02-16 15:00:20,178 - pyvisa - DEBUG - Received record through <socket.socket fd=9856, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('192.168.0.14', 57717), raddr=('192.168.0.133', 49155)>: bytearray(b'\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x060.00\r\n')
b'\x00\x00\x00\x07\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x060.00\r\n'

EOFError                                  Traceback (most recent call last)
Cell In[56], line 1
----> 1 bk.read()

File ~\mambaforge-pypy3\lib\site-packages\pyvisa\resources\messagebased.py:486, in MessageBasedResource.read(self, termination, encoding)
    484 if termination is None:
    485     termination = self._read_termination
--> 486     message = self._read_raw().decode(enco)
    487 else:
    488     with self.read_termination_context(termination):

File ~\mambaforge-pypy3\lib\site-packages\pyvisa\resources\messagebased.py:442, in MessageBasedResource._read_raw(self, size)
    435     while status == loop_status:
    436         logger.debug(
    437             "%s - reading %d bytes (last status %r)",
    438             self._resource_name,
    439             size,
    440             status,
    441         )
--> 442         chunk, status = self.visalib.read(self.session, size)
    443         ret.extend(chunk)
    444 except errors.VisaIOError as e:

File ~\mambaforge-pypy3\lib\site-packages\pyvisa_py\highlevel.py:512, in PyVisaLibrary.read(self, session, count)
    510 # from the session handle, dispatch to the read method of the session object.
    511 try:
--> 512     data, status_code = self.sessions[session].read(count)
    513 except KeyError:
    514     return (
    515         b"",
    516         self.handle_return_value(session, StatusCode.error_invalid_object),
    517     )

File ~\mambaforge-pypy3\lib\site-packages\pyvisa_py\tcpip.py:534, in TCPIPInstrVxi11.read(self, count)
    530 while reason & end_reason == 0:
    531     # Decrease timeout so that the total timeout does not get larger
    532     # than the specified timeout.
    533     timeout = max(0, timeout - int((time.time() - start_time) * 1000))
--> 534     error, reason, data = read_fun(
    535         self.link, chunk_length, timeout, self.lock_timeout, flags, term_char
    536     )
    537     print(data)
    538     print(read_data)

File ~\mambaforge-pypy3\lib\site-packages\pyvisa_py\protocols\vxi11.py:235, in CoreClient.device_read(self, link, request_size, io_timeout, lock_timeout, flags, term_char)
    233 params = (link, request_size, io_timeout, lock_timeout, flags, term_char)
    234 try:
--> 235     return self.make_call(
    236         DEVICE_READ,
    237         params,
    238         self.packer.pack_device_read_parms,
    239         self.unpacker.unpack_device_read_resp,
    240     )
    241 except socket.timeout as e:
    242     return ErrorCodes.io_error, e.args[0], ""

File ~\mambaforge-pypy3\lib\site-packages\pyvisa_py\protocols\rpc.py:478, in RawTCPClient.make_call(self, proc, args, pack_func, unpack_func)
    472 # In case of a timeout because the instrument cannot answer, the
    473 # instrument should let use something went wrong. If we hit the hard
    474 # timeout of the rpc, it means something worse happened (cable
    475 # unplugged).
    476 self.timeout += 1.0
--> 478 return super(RawTCPClient, self).make_call(proc, args, pack_func, unpack_func)

File ~\mambaforge-pypy3\lib\site-packages\pyvisa_py\protocols\rpc.py:236, in Client.make_call(self, proc, args, pack_func, unpack_func)
    234 self.do_call()
    235 if unpack_func:
--> 236     result = unpack_func()
    237 else:
    238     result = None

File ~\mambaforge-pypy3\lib\site-packages\pyvisa_py\protocols\vxi11.py:184, in Vxi11Unpacker.unpack_device_read_resp(self)
    182 error = self.unpack_int()
    183 reason = self.unpack_int()
--> 184 data = self.unpack_opaque()
    185 return error, reason, data

File ~\mambaforge-pypy3\Lib\xdrlib.py:217, in Unpacker.unpack_string(self)
    215 def unpack_string(self):
    216     n = self.unpack_uint()
--> 217     return self.unpack_fstring(n)

File ~\mambaforge-pypy3\Lib\xdrlib.py:209, in Unpacker.unpack_fstring(self, n)
    207 j = i + (n+3)//4*4
    208 if j > len(self.__buf):
--> 209     raise EOFError
    210 self.__pos = j
    211 return self.__buf[i:i+n]

EOFError:

(Seems to come from decoding the response in the Vxi11 protocol.)

The context :

I use mamba with python 3.9, but it also fails on other computers with other distros but it works with an old pyvisa package (1.11, not using pyvisa-py). Current versions used :

PyVISA 1.13.0
PyVISA-py 0.6.2

Output of pyvisa-info

Machine Details:
Platform ID: Windows-10-10.0.22000-SP0
Processor: Intel64 Family 6 Model 165 Stepping 5, GenuineIntel

Python:
Implementation: PyPy
Executable: C:....\mambaforge-pypy3\python.exe
Version: 3.9.16
Compiler: MSC v.1929 64 bit (AMD64)
Bits: 64bit
Build: Jan 18 2023 (#feeb267e)
Unicode: UCS4

PyVISA Version: 1.13.0

Backends:
ivi:
Version: 1.13.0 (bundled with PyVISA)
Binary library: Not found
py:
Version: 0.6.2
ASRL INSTR: Available via PySerial (3.5)
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
Resource discovery:
- VXI-11: ok
- hislip: ok
VICP INSTR:
Please install PyVICP to use this resource type.
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'

Thank in advance for your responses !
Best regards,

Tristan

Can you share the manual of your instrument ?

Do you see any change if you specify \r\n as termination character ? I doubt it will make a difference but I would like to be sure.

Indeed, the read termination is /r/n but with or without, it doesn't change anything !
Here is the PDF of the BK :
A700000007901997.pdf
Thanks a lot for your quick response ๐Ÿš€

From your instrument it does not appear to support VXI-11 protocol (and I am quite surprised you got an IDN). However page 104 raw socket connection is mentioned. Can you try again using a raw socket resource ?
TCPIP::192.168.0.133::5025::SOCKET

Hi there ๐Ÿ‘‹,

Seems to work, thanks a lot ! It was indeed BK instuments not supporting VXI-11 protocol. The IDN has actually misled us a bit, but raw socket connection with 5025 port works (we should have worked a little more on the manual oops).

Thanks a lot ! ๐Ÿš€

Happy you got it to work