TCP/IP data send delay

조회 수: 10 (최근 30일)
Kemal
Kemal 2011년 11월 17일
Hi, I am trying to communicate MATLAB and Python over TCP connection.
System works as such; Python file sends an array like "1001,1002,1003,1004,1005" to MATLAB and MATLAB replies this message with "10003,10004,10005". The problem is that MATLAB's response is too slow (about 30 secs - 120 secs)
I will use this system in a real-time simulation. So I need to resolve delay problem.
How can I overcome this problem?
Thanks

답변 (1개)

Walter Roberson
Walter Roberson 2011년 11월 17일
TCP/IP is buffered by default; the I/O subsystem may accumulate data to be sent until the packet is full or the timers time out or the user explicitly requests that the data be sent (by setting the PSH control bit in a packet.)
Therefore the first thing you should do is monitor the network to be sure your data is being sent when you think it is.
Then, when MATLAB receives packets, it will not necessarily pass them on immediately to the application, if the connection has been configured to use a Terminator character. The description of the rules is here. Notice the small line suggesting setting the Terminator to '' for binary files.
  댓글 수: 2
Kemal
Kemal 2011년 11월 17일
Thank you for your reply,
I am sorry I am new to TCP/IP connections and MATLAB so Could you say please for the following code what should I do to fix it?
t = tcpip('127.0.0.1', 5050);
set(t, 'InputBufferSize', 59); % Buffer size is determined for client send data (it is exactly same with received data size)
set(t,'Terminator','')
fopen(t);
signalTime1 = 10000;
signalTime2 = 10000;
signalTime3 = 10000;
while (1)
while (get(t, 'BytesAvailable') > 0)
DataReceived = fscanf(t);
x = str2num(DataReceived);
signalTime1 = (x(1)+x(2)-2000)+10000;
signalTime2 = (x(3)+x(4)-2000)+10000;
signalTime3 = (x(5)+x(6)-2000)+10000;
end
data = sprintf('%d %d %d',signalTime1,signalTime2,signalTime3);
fprintf(t,'%s',data);
end
Walter Roberson
Walter Roberson 2011년 11월 17일
Consider setting a BytesAvailableFcn callback instead of looping polling the bytes available. Also, you can use t.BytesAvailable instead of the get() operation.
If you have control over the protocol, it would be better for you to define \n (newline) or \r\n (carriage return and newline) as the terminator, and to send those as part of the string, and then set the Terminator to \n .
Something I should have pointed out earlier but forgot to, is that MATLAB is not designed for real-time work. It is probably good enough for "soft real time" in low-bandwidth environments, but not for "hard real time". TCP/IP itself is not suitable for "hard real time" as TCP/IP places no upper bound on the amount of time a packet can be in transmission.
Also, be advised that older versions of the Instrument Control Toolbox did not permit MATLAB to be a TCP/IP server, only a client (that is, it would have to initiate connections to outside programs.) My recollection is that that restriction was removed in R2011B, but I could be wrong about that or off by a version.
There is a MATLAB File Exchange contribution "tcpudpip", http://www.mathworks.com/matlabcentral/fileexchange/345-tcpudpip-toolbox-2-0-6 which can substitute for the Instrument Control Toolbox and does NOT have restrictions on being a TCP/IP server.

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

카테고리

Help CenterFile Exchange에서 Instrument Control Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by