Receiving TCP Data Laggy

4 次查看(过去 30 天)
Hi
I'm trying to stream data into matlab using the TCP library and while I'm getting data it is quite laggy. If I monitor the data using a program like Putty there is no lag. So I was wondering if there were settings or something I could change in matlab to make this work better/be less slow. For those curious here is my code for streaming data.
%Matlab script for reading from TCP/IP server
%Clear Console and workspace
close all;
clear all;
clc;
%Set Up Connection
rhost = '18.111.48.184';
rport = 23;
disp('Welcome to Real Time TCP/IP Data Logger!')
disp('Attempting to connect to:')
disp(rhost)
x = ['port: ',num2str(rport)];
disp(x)
t = tcpip(rhost,rport);
t.InputBufferSize = 300000;
%wait for connection
disp('Connecting...')
fopen(t); %Open the socket
disp('Connected.')
disp('To stop connection press Ctrl+C')
%start collecting data line by line
disp('Collecting Data...')
while (t.BytesAvailable > 0)
data = fscanf(t) %One line of data
disp(data)
end
disp('Data Collected')
fclose(t)
fdelete(t)
clear t

采纳的回答

Walter Roberson
Walter Roberson 2016-6-13
You might be testing the speed of disp() rather than the speed of the tcp.
You would probably also be slightly faster to use fgetl() than fgets(). But faster still would be fread(t, t.BytesAvailable) which should be equivalent to what you have because you appear to be organizing your data as binary rather than as line oriented (if you had wanted it to be line oriented you would have configured a Terminator property)

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by