如何将 Matlab 与 Visual Studio 连接以进行实时数据交换?



我正在做一个项目,在我的计算机和外部服务器之间进行实时通信。我的计算机和服务器之间的通信运行良好。我从我的C++代码向服务器发送请求,并在C++中得到响应。

问题在于将 matlab 数据发送到 C++ 代码和在 Matlab/Simulink 中评估服务器数据。为此,我必须在 Matlab/Simulink 和 Visual Studio 中的C++项目之间实时传输数据。我觉得我在最后几天已经阅读了整个互联网,但我没有找到我的问题的完美答案。

以下是一些可行的解决方案:

  1. 在 Matlab 中创建一个 mex-File 并将其保存在特定文件夹中。然后让我的 C++ - 代码在此文件夹中搜索新文件并从文件中获取数据。在服务器响应后,C++ - 代码应创建一个新的 mex-File 并将其保存在其他特定文件夹中。同时,Matlab会搜索一个新文件并将其加载到工作区中。 我不认为这会提供实时数据传输,但可能效果很好。

  2. 在 Matlab 和 Visual Studio 之间创建 TCP/IP 连接以直接交换数据。 我不知道,是否可能,也不知道如何编写套接字。

我希望有人可能对我的问题有一些经验,可以帮助我找到处理它的最佳方法。我感谢每一个答案!

我在Windows 10上使用Matlab/Simulink R2017a和Visual Studio 2017 Enterprise。

这对我来说是一个常见的要求,我使用你的第二个选项,套接字进行实时通信。 下面是我为实时显示信号数据编写的函数。 虽然原始函数中的一些代码已被省略,但它应该可以让您很好地了解如何在 Matlab 中使用 TCP 套接字来读取实时数据。 我的要求是能够实时读取多声道音频,下面的功能在我的台式机上表现得足够好。

function [ sig ] = liveplot( host, port, signal_info, Fs, window_size_secs )
%LIVEPLOT Plot live data from the signal processing engine
%   The signal processing engine will send out data as a multiplexed stream
%   of N double-precision values.
%   The 'signal_info' argument should be a logical matrix of length N, where N is the
%   number of channels that are being multiplexed.  if signal_info(K) is
%   true, then that channel will be plotted.
MAX_SESSION_TIME_SECS = 60;
FRAMES_PER_SESSION = MAX_SESSION_TIME_SECS * Fs;
FRAMES_PER_WINDOW = window_size_secs * Fs;
REFRESH_TIME_SECS = 0.05;
SAMPLE_DURATION=1/Fs;
N = length(signal_info);
FRAME_SZ = N * 8; % sizeof(double)
conn = tcpip(host, port);
conn.InputBufferSize = 1024 * FRAME_SZ;
conn.BytesAvailableFcnCount = conn.InputBufferSize;
conn.BytesAvailableFcnMode = 'byte';
conn.BytesAvailableFcn = @bytesavailablecallback;
conn.readAsyncMode = 'continuous';
seconds_read = 0;
asyncread_tic = tic;
%   bytesavailablecallback(conn);
% time window: incremented buy SAMPLE_DURATION each frame
num_frames = 0;
%Set up Plot
% plot objects, one for  each N stream in frame
h = figure;
hold on                            %hold on makes sure all of the channels are plotted

% Start the async read thread
fopen(conn);
last_refresh = tic;
while ishandle(h) %Loop when Plot is Active will run until plot is closed
try 
%        if (toc(last_refresh) > REFRESH_TIME_SECS)
if (seconds_read < window_size_secs)
seconds_read = window_size_secs;
end
xlim([seconds_read-window_size_secs seconds_read]);
%            refreshdata(h, 'caller');
% fprintf('Refresh took %f seconds.n', toc(last_refresh));
drawnow limitrate
last_refresh = tic;
catch ME
break
end
%        end
end
fclose(conn);
delete(conn);
function bytesavailablecallback(conn, ~)
% initiate another read immediately to fill the buffer
%         readasync(conn, conn.InputBufferSize-conn.BytesAvailable);
n_frames_available = floor(conn.BytesAvailable / (N * 8));
if (n_frames_available > 0)
[frames_read, nread] = fread(conn, N * n_frames_available, 'double');
num_frames_read = nread / N;
if (num_frames_read ~= n_frames_available)
throw(MException('assert',sprintf('%d frames expected, but %d read',n_frames_available,num_frames_read )));
end
% need the following line when only partial read - fread fails to
% reshape the return value if nread < FRAMES_PER_READ
sig(:, 1+num_frames:num_frames+num_frames_read) = reshape(frames_read, N, []);
num_frames = num_frames + num_frames_read;
seconds_read = num_frames * SAMPLE_DURATION;
for i=1:N
ph = plots(i);
if (ph ~= 0)
set(ph, 'Ydata', sig(i,:));
end
end
async_read_time = toc(asyncread_tic);
asyncread_tic = tic;
fprintf('Read %d frames in %f seconds, rate is %d fpsn', num_frames_read, async_read_time, round(num_frames_read / async_read_time));
end
end
end

我建议您查看 MATLAB 帮助主题中的建议:MATLAB->高级软件开发 -> 外部编程接口

这实质上为您提供了三个调用 C/C++ 代码的选项:

  • 调用 MEX 文件中的函数(帮助主题:调用 MEX 文件)
  • 连接到 COM 或 ActiveX 服务器(帮助主题:COM 接口)
  • 调用.dll中的函数(帮助主题:C 共享库)

多年来,我一直使用最后两个选项。

使用 MATLAB 驱动通信时,真正的麻烦是当您需要回调和事件时。 这是因为通用 MATLAB 代码都在 Java Swing 的事件调度中运行。

MATLAB

还为您提供了另一种方式工作的选项:您可以从其他语言调用 MATLAB 函数。 请参阅帮助主题:

MATLAB
  • 的应用程序编程接口:用其他语言编写程序,例如 C/C++ 和 Fortran,以与 MATLAB 功能交互

碰巧的是,我从未使用过这个。


我不能建议并行处理工具箱是否有助于处理异步通信。 我也不能建议使用套接字,管道,HTTP等。


祝你的项目成功。

最新更新