def test_handling_malformed_binary(self): containers = (list, tuple) + ((np.array, np.ndarray) if np else ()) # Use this to generate malformed data which should in theory be # impossible class DumbBytes(bytes): def __len__(self): return 10 for container in containers: with pytest.raises(ValueError) as e: util.from_binary_block(DumbBytes(b"\x00\x00\x00"), container=container) assert ( "malformed" if container in (list, tuple) else "buffer" in e.exconly() )
def query_binary_values(self, command, datatype='f', container=np.array, is_big_endian=False, bufsize=None): """Write a string message to device and read binary values, which are returned as iterable. Uses a pyvisa utility function. Args: command: String command sent to instrument. values: Data to be sent to instrument. datatype: Format string for single element. container: Iterable to return number of as. is_big_endian: Bool indicating endianness. Returns: Iterable of data values to be retuned """ if bufsize is None: bufsize = self.bufsize self.write(command, bufsize=bufsize) block = self.read_raw() return from_binary_block(block, datatype=datatype, is_big_endian=is_big_endian, container=container)
def parse_binary_responses(self, kind, res_in): if kind == 'fetch': glob_offset = 0 for idx, key in enumerate(self.fetch_kinds[:3]): offset, length = util.parse_ieee_block_header(res_in) glob_offset += offset self.last_reading[key] = util.from_binary_block( res_in, offset=glob_offset, data_length=length, datatype='i', is_big_endian=True, container=np.array) glob_offset += length + 4 glob_offset -= 3 # separation between last binary block and timestamp string is only one byte isntead of 4 parsed = res_in[glob_offset:].split(b';') for idx, key in enumerate(self.fetch_kinds[3:]): self.last_reading[key] = self.str_conv( parsed[idx].decode('ascii'), key) ending = parsed[-1].decode('ascii') if ending.split('\n')[0] == '4': res = self.visa_res.query(':SYSTEM:ERROR?;*STB?') self.errors.append(res) while res[0] != '0': print("Error code: {}".format(res)) res = self.visa_res.query(':SYSTEM:ERROR?;*STB?') self.errors.append(res)