Clear Filters
Clear Filters

Slow communication with NA though LAN cable, using the fread fucntion

1 view (last 30 days)
Slow communication with NA, I use direct Lan cable for connection: I use the TCPIP to connect to a Network Analyzer using the following code:
obj_e5080_ = tcpip('169.254.64.111', 5025); obj_e5080_.InputBufferSize = 1e5; obj_e5080_.OutputBufferSize = 1e5; obj_e5080_.ByteOrder = 'littleEndian';
Then I want to check the elapsed time for reading the data using this code:
tic fprintf(obj_e5080_, ':INITiate:IMMediate'); opc1 = sscanf(query(obj_e5080_, '*OPC?'), '%d'); fprintf(obj_e5080_,':CALCulate:MEASure:DATA:SDATa?'); sdata1 = fread(obj_e5080_,81,'double'); toc
However, the results are not good and it is very slow: Elapsed time = 0.2 seconds, can Anyone help please?
I attached the real time results as well

Answers (1)

Tony Mohan Varghese
Tony Mohan Varghese on 14 Aug 2017
The issue may be related to this line:
sdata1 = fread(obj_e5080_,81,'double');
fread function will read data as double precision and stores them as double array. This is a memory heavy operation and is thus slow. I would suggest to read the data as characters and convert them back to double array later.
It was also observed that fread takes much longer to execute when the default terminator setting is used / Terminator is not empty (“”). This behavior is mentioned in the FREAD doc page:
See "Note: Set the terminator property to '' (null), if appropriate, to ensure efficient throughput of binary data." Try to use a null/empty terminator .
  1 Comment
Milad Mom
Milad Mom on 14 Aug 2017
Thank you Tony,
I applied your comment adding the obj_e5080_.Terminator = ' '; command to fasten the fread function, however, the results were the same. Actually, we noticed that the fread gives us wrong results from time to time, so I used binblockread instead which gives us correct answers all the times. But the problem of slow reading is still there. Here is the code: (please note that I just read 5 data points from the instrument) 
tic  fprintf(obj_e5080_, ':INITiate:IMMediate');  fprintf(obj_e5080_,':CALCulate:MEASure:DATA:FDATa?');  toc  [A,count,msg] = binblockread(obj_e5080_, 'double');  toc   
%and the results:
Elapsed time is 0.000920 seconds.  Elapsed time is 0.222298 seconds. 
>> count
count =
     9
which is still very slow. I appreciate your help.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!