diff --git a/lantz/visa.py b/lantz/visa.py index bdd380d..6498354 100644 --- a/lantz/visa.py +++ b/lantz/visa.py @@ -61,6 +61,9 @@ class MessageVisaDriver(TextualMixin, Driver): ENCODING = 'ascii' RECV_CHUNK = -1 + # When RECV_CHUNK==-1, read all available data, RECV_BUFFER_SIZE bytes at a + # time. + RECV_BUFFER_SIZE = 1<<8 def __init__(self, resource_name, *args, **kwargs): super().__init__(*args, **kwargs) @@ -111,6 +114,30 @@ def finalize(self): def is_open(self): return self.vi is not None + def raw_recv(self, size): + """Receive raw bytes to the instrument. + + :param size: number of bytes to receive + :return: received bytes + :return type: bytes + + If a timeout is set, it may return less bytes than requested. + If size == -1, then the number of available bytes will be read. + + """ + + if not size or size == -1: + buffers = [] + while True: + data = self.visa.read(self.vi, self.RECV_BUFFER_SIZE) + buffers.append(data) + if len(data) < self.RECV_BUFFER_SIZE: + # Timeout or terminator + return b''.join(buffers) + else: + data = self.visa.read(self.vi, size) + return data + class SerialVisaDriver(MessageVisaDriver): """Base class for drivers that communicate with instruments