Reading only latest data over TCP/IP/UDP with Instrument Control Toolbox

13 views (last 30 days)
Hello,
I've been stuck on this problem for some time and I hope somebody knows how to solve it.
I have two instances of MATLAB running on two computers in a LAN. They are connected through UDP because reliable data transfer and the existence of connection between two computers are not requirements and I need to exchange data as fast as possible (in the end it will run in an infinite loop until user interrupts it). Each instance has to send data to each other and each instance has to read the data the other one is streaming.
Reading is done asynchronously. The size of data to be sent and read is not changing, e.g. it will always be an array of doubles of a constant length.
If ReadAsyncMode is set to continuous, and I start sending data from one machine and then after some delay I start reading data with the other machine the data is retrieved in a FIFO manner, i.e. the oldest data is retrieved first. However, I only need to read the latest data (LIFO), not all data. On the other hand, if ReadAsyncMode is set to manual, this basically solves the issue but if connection link is broken the loop is slowed down to 1 second per iteration due to timeout (Timeout property is rounded to a nearest full second). Continuous always checks the server inherently, so I only read if there are BytesAvailable. I can't do this in manual mode.
Is it possible to read only latest data that was written or overwrite the old data using fread and fwrite?
UDP Receive Simulink block has the option OutputLatestData, but I need to do this in MATLAB.
Here is the minimum working example to further illustrate my problem:
%PC1 connects to remote host PC2
remotehost = udp('192.168.1.83', 'RemotePort', 30000, 'LocalPort', 30001);
remotehost.InputBufferSize = 8;
remotehost.ReadAsyncMode = 'continuous';
fopen(remotehost);
%PC2 connects to remote host PC1
remotehost = udp('192.168.1.81', 'RemotePort', 30001, 'LocalPort', 30000);
remotehost.OutputBufferSize = 8;
fopen(remotehost);
I send data from PC2 to PC1:
data = 1;
fwrite(remotehost, data(:), 'double');
data = 2;
fwrite(remotehost, data(:), 'double');
If I read data at PC1:
if remotehost.BytesAvailable
data = fread(remotehost, 1, 'double')
end
if remotehost.BytesAvailable
data = fread(remotehost, 1, 'double')
end
I will get:
data =
1
data =
2
At the first call of fread I just want to read data = 2 and ignore the first data that was sent. Ideally, I don't want the first data to even be in the stack for reading. I want empty buffer after every read operation of the newest data. When I send data sequentially, I don't want it to be queued. I want to overwrite whatever was before.
Can this be done? Am I using a correct procedure for what I want to implement? Correct protocol?
Thank you in advance.

Accepted Answer

Lexi Crommett
Lexi Crommett on 9 Apr 2021
Hello Matej,
It sounds like your goal is to read only the latest data, not necessarily the data in the order it was received. You may be able to achieve this using the udpport interface (introduced in R2020b) in datagram mode (https://www.mathworks.com/help/instrument/udp-interface.html?s_tid=CRUX_lftnav) since it sounds like your data is always a constant length. I have done something like this below. I am using the echoudp function to mimic the functionality without connecting to an actual device.
echoudp("on",4040)
u = udpport("datagram");
% Send data to server
inputdata = 1;
write(u,inputdata(:),"double","localhost",4040)
inputdata = 2;
write(u,inputdata(:),"double","localhost",4040)
% Read data from server -- finalData contains what you are looking for
if u.NumDatagramsAvailable > 0
data = read(u,u.NumDatagramsAvailable,"double");
finalData = data(end).Data
end
% Clean up
clear u
echoudp("off")
You can also use the DatagramsAvailableFcn property of the UDP object, which can be configured using configureCallback (https://www.mathworks.com/help/instrument/udpport.configurecallback.html) to do some of the things in this example, but I tried to match your example more closely. See an example of writing and reading data over UDP (using the udpport interface) here: https://www.mathworks.com/help/instrument/read-and-write-binary-data-over-udp.html
Hope this helps!

More Answers (0)

Categories

Find more on Direct Interface Communication in Simulink in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!