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

조회 수: 3 (최근 30일)
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);

채택된 답변

Christopher Saltonstall
Christopher Saltonstall 2021년 11월 18일
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'));

추가 답변 (1개)

Walter Roberson
Walter Roberson 2021년 11월 10일
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
Christopher Saltonstall
Christopher Saltonstall 2021년 11월 10일
편집: Christopher Saltonstall 2021년 11월 10일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by