PyVISA error reading from TCP/IP unless exact number of incoming bytes is specified

95 views Asked by At

I have some Python code for reading from a device, which returns a read error unless I specify exactly the number of bytes returned or fewer. The following code successfully returns SA-14212, which is exactly 8 characters and is the correct response. But we will not always know the correct length of the response in advance and replacing data = session.read_bytes(8) with data = session.read_bytes(9) or data = session.read_bytes(1024) or data = session.read() causes an error.

import pyvisa as visa

HOST = '192.168.0.15'
PORT = 7088
message = '*IDN?'

visa.log_to_screen()

# Open a TCPIP connection using VISA
resourceManager = visa.ResourceManager() 
dev = 'TCPIP0::' + HOST + '::' + str(PORT) + '::SOCKET'
session = resourceManager.open_resource(dev)
    
session.write(message)

data = session.read_bytes(8)
print(data.decode())

session.close()

Here are the error and proximate debugging statements for data = session.read_bytes(9):

2023-12-02 01:17:01,872 - pyvisa - DEBUG - viWrite(1, b'*IDN?\r\n', 7, 'c_ulong(7)') -> 0
2023-12-02 01:17:01,872 - pyvisa - DEBUG - TCPIP0::192.168.0.15::7088::SOCKET - reading 9 bytes (last status None)
2023-12-02 01:17:01,887 - pyvisa - DEBUG - viRead(1, <ctypes.c_char_Array_9 object at 0x0000029043084CC0>, 9, 'c_ulong(8)') -> -1073807298
2023-12-02 01:17:01,887 - pyvisa - DEBUG - TCPIP0::192.168.0.15::7088::SOCKET - exception while reading: VI_ERROR_IO (-1073807298): Could not perform operation because of I/O error.
Buffer content: bytearray(b'')

The device is an obscure Fibre-Bragg-Grating demodulation device with very little documentation. To my knowledge it uses no termination character for input or output (changing it from '' to '\0' or '\n' doesn't alter things). I have looked up the error code here: https://knowledge.ni.com/KnowledgeArticleDetails?id=kA00Z000000P6FmSAK but it sheds no light.

We have separate working code using the Python socket library and it ostensibly does exactly the same as here: opens a TCPIP connection, writes a byte string, reads a response (into a buffer with 1024 characters). Unfortunately the goal is a PyVisa implementation, not a raw sockets implementation.

0

There are 0 answers