Limited throughput on high repetition rate using TCP/IP connection using Instrument Control Toolbox

4 views (last 30 days)
I have embedded hardware that we developed in house that acquires data at rates of up to 5 kHz and streams this data (at throughput levels of 100+ Mbps). It is a simple TCP protocol that includes a defined header (specifiying the payload size) followed by the payload. I am able to succesfully consume this data stream in other languages (Python, LabVIEW, C##, etc.). However I am having difficutly creating an API in MATLAB that can keep up.
To simplify I have created a client and server scripts in MATLAB to test the TCP implmenetation in MATLAB (shown below, start server first, then start client).
The data throughput is very low here (only a few bytes per TCP read/write). However, the server can't send data much faster than 1500 Hz and the client can barely keep up at 1 kHz. Note I am running MATLAB on a Mac. Similar performance on a Windows 10 machine.
Am I missing something in the configuration? In other languages, I can do this well above 5 kHz withouth a problem.
Here is the server:
clc
clear all
instrreset
number_of_packets = 10000;
instrument_server = tcpip('0.0.0.0', 51971, 'NetworkRole', 'server');
fopen(instrument_server);
tic
for k1 = 1:number_of_packets
if mod(k1, 100) == 0
sprintf('Sending %d . . .', k1)
end
message = sprintf('Sending %d', k1);
fwrite(instrument_server, uint8(length(message)));
fwrite(instrument_server, uint8(message));
end
et = toc;
sprintf('Average data rate = %f Hz', 1/(et/number_of_packets))
fclose(instrument_server);
Here is the client:
clc
clear all
instrreset
number_of_packets = 10000;
instrument_client = tcpip('localhost', 51971, 'timeout', 1);
fopen(instrument_client);
tic
for k1 = 1:number_of_packets
if mod(k1, 100) == 0
sprintf('Reading %d . . .', k1)
end
message_length = fread(instrument_client, 1);
message = fread(instrument_client, message_length);
end
et = toc;
sprintf('Average data rate = %f Hz', 1/(et/number_of_packets))
fclose(instrument_client);
  3 Comments
Justin Stay
Justin Stay on 17 May 2019
I think this could be a limiting case at some point, but the header and payload in the attached example above is well below 512 bytes; less than 20.
Just to add some additional information, on a Windows 10 machine, the above code can send around 4 kHz, and received at under 3 kHz.
I can keep up with a much larger paylod in other langauges at 5 kHz when our embeded software running on custom hardware is acting as the server. I fully expect the above code to run much faster.
Walter Roberson
Walter Roberson on 17 May 2019
You are right, on my Mac, setting input and output buffer sizes to fairly large did not make any notable difference.

Sign in to comment.

Answers (0)

Categories

Find more on Instrument Control Toolbox in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!