Use fscanf to Read Binary Data from Keysight Power Analyzer with Unknown Length

9 visualizaciones (últimos 30 días)
I am communicating with a Keysight N6705C Power analyzer and trying to read data from a buffer that is continuously collecting data. I use SCPI commands to query the buffer and fscanf to read the response. Currently, the analyzer is set to respond with the ASCII format. Therefore, the response is an character string of values of unknown length which I have to split and convert to doubles. The code used for this is shown below.
However, reading ASCII values is 4x slower than reading binary data. But if I set the output of the analyzer to binary it returns a string of binary data that looks like gibberish which I am having trouble converting to doubles. See attached file. The manual say that "Data is returned in binary IEEE single precision floating point". This data should be a sinusoidal curve.
How do I convert this binary/char string into an array of doubles?'
Alternatively, how do I read this data from the analyzer directly into double format?
%% ASCII Method
%query analyzer buffer and receive a response of upto 10000 values
fprintf(visaObj, 'FETC:ELOG? 10000,(@1)');
%read response
data_temp = fscanf(visaObj);
%split resulting char string at commas
datasplit = strsplit(data_temp,',');
%convert char array to double array
data_num = str2double(datasplit);

Respuesta aceptada

Christopher Saltonstall
Christopher Saltonstall el 18 de Nov. de 2021
So, it turns out that "fscanf" should only be used to read ASCII data as described here. Instead "binblockread" or "readbinblock" should be used. The returned array of bytes can then be converted to single precision floats using the following command.
data_byte = binblockread(visaObj);
data_float = swapbytes(typecast(uint8(data_byte),'single'));

Más respuestas (1)

Walter Roberson
Walter Roberson el 10 de Nov. de 2021
Your .mat file contains 38 bytes of char. uint8() the char and take the first 36 bytes (need a multiple of 4). uint8() that, and typecast() the result to 'single'; the swapbytes() the result.
The values are then in the range of 1.47 .
[though at the moment, I am not confident that the very first output is correct.)
  1 comentario
Christopher Saltonstall
Christopher Saltonstall el 10 de Nov. de 2021
Editada: Christopher Saltonstall el 10 de Nov. de 2021
This is close but not quite right (see attached fig). This should be a sinusoidal curve centered at 1 and oscillates between 0.5 and 1.5 (see ascii.fig). These plots were generated from 20 reads of the analyzer (see attached .mat file). It looks like we are skipping some data and incorrectly converting others.
The function that I wrote based on your description is here.
function data = bin2single(data_temp)
data_mod = uint8(data_temp);
nbytes = length(data_mod);
nvalues = floor(nbytes/4);
data_mod = uint8(data_mod(1:4*nvalues));
data_mod = typecast(data_mod,'single');
data = swapbytes(data_mod);
end

Iniciar sesión para comentar.

Categorías

Más información sobre Data Import and Analysis en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by