使用Matlab Coder的基本联网 [英] Basic networking with Matlab Coder

查看:210
本文介绍了使用Matlab Coder的基本联网的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Matlab Coder获得非常基本的网络功能(我需要将其转换为C代码).但是,Coder不支持我尝试的所有网络类和对象. Matlab完全忽略使用此工具的网络似乎是不合理的.是否有某种与编码器兼容的通过网络发送数据的方法?

I'm trying to get very basic network functionality with Matlab Coder (I need to turn it into C code). However, all the network classes and objects I try arn't supported by Coder. It seems unreasonable that Matlab would completely neglect networking entirely with this tool. Is there some method of sending data over a network that DOES work with coder?

我更喜欢TCP,但是只要与Coder兼容,UDP或其他将实际发送/接收数据的东西都可以使用.

I'd prefer TCP, but UDP or anything else that will actually send/receive data will work, as long as it is compatible with Coder.

推荐答案

此答案假定DSP系统工具箱不可用.如果是,则系统对象 dsp.UDPSender 和可以考虑 dsp.UDPReceiver .

This answer assumes that the DSP System Toolbox is not available. If it is, the System Objects dsp.UDPSender and dsp.UDPReceiver may be considered.

由于最终目标是生成C代码,并且由于网络I/O通常是通过库完成的,所以一种好的方法是将执行网络I/O的外部C代码集成到您的MATLAB代码中.调用外部C函数的基本方法是使用coder.ceval,并且说明该过程

Since the final goal is to generate C code, and because network I/O is usually done via a library, a good approach would be to integrate external C code that does the network I/O into your MATLAB Code. The basic way to call an external C function is using coder.ceval and the process is explained here.

  1. 编写实现所需行为的C(++)函数或查找提供必要功能的C库.假设我们在文件externalUDPSend.h/.c中实现了函数externalUDPSend.
  2. 如链接文档中所示,编写一个或多个使用coder.ceval调用C(++)函数的MATLAB函数.这些将用作外部代码的包装,并将C(++)代码公开给MATLAB.链接示例中的callfoo之类的东西将起作用:

  1. Write C(++) functions implementing the behaviour you need or find a C library providing the necessary functionality. Assume we implement the function externalUDPSend in the files externalUDPSend.h/.c.
  2. Write one or more MATLAB functions that call your C(++) functions using coder.ceval as shown in the linked documentation. These will serve as a wrapper around your external code, and will expose the C(++) code to MATLAB. Something like callfoo in the linked example will work:

function y = useExternalUDP(x)
%#codegen
if coder.target('MATLAB')
  % Running in MATLAB. Use standard MATLAB
  % network I/O code here
  ...
else
  % Generating code. Call external code/library
  % Include header for external code
  coder.cinclude('externalUDPSend.h');

  % Set the type of the output. Assume double scalar
  % Change the RHS to match the return type
  y = 0;
  y = coder.ceval('externalUDPSend',x,numel(x));
end

  • 开发您的项目.调用包装函数,由于使用coder.target,该函数将在MATLAB和生成的代码中工作.
  • 使用类似以下的命令生成MEX函数:

  • Develop your project. Call the wrapper function(s), which will work in MATLAB and in the generated code because of the use of coder.target.
  • Generate a MEX function using something like:

    codegen useExternalUDP -config:mex externalUDPSend.c -args ...
    

    生成的MEX函数用作自定义代码的MATLAB接口,因此无需手动编写MEX接口. MATLAB Coder将为您生成所有MEX接口逻辑.然后在MATLAB中测试该MEX函数.测试MEX功能非常重要,因为可以在MEX中检测并报告运行时错误,例如越界索引,使用代码生成不支持的功能等.这些检查将从生成的独立代码中删除.

    The generated MEX function serves as the MATLAB interface to your custom code so there is no need to hand write a MEX interface. MATLAB Coder will generate all of MEX interfacing logic for you. Then test that MEX function in MATLAB. Testing the MEX function is important because runtime errors like out of bounds indexing, using features not supported for code generation, etc. can be detected and reported in MEX. These checks are removed from generated standalone code.

    集成外部库/封装依赖项

    请注意,如果您选择使用现有的网络I/O库,则可能还需要链接库,或者您可能需要修改所生成代码的构建.您可以使用 coder.updateBuildInfo coder.ExternalDependency 到在您的MATLAB代码中实现这一目标.

    Integrating External Libraries/Encapsulating Dependencies

    Note that you may also need to link in libraries if you choose to use an existing network I/O library, or you may need to modify the build of the generated code. You can either use coder.updateBuildInfo or coder.ExternalDependency to achieve this in your MATLAB Code.

    文件读取示例显示了一些内容更高级的自定义代码集成工具,例如coder.refcoder.opaque,并在调用外部代码时处理来自MATLAB代码的C字符串.请注意,代码生成支持使用MATLAB函数fprintffread,因此该示例仅用于指导目的,而不是执行文件I/O的必要条件.

    The file reading example shows some more advanced custom code integration tools such as coder.ref, coder.opaque, and dealing with C strings from MATLAB code when calling external code. Note that the MATLAB functions fprintf and fread are supported for code generation so this example is meant to be instructive rather than a necessity for doing file I/O.

    这篇关于使用Matlab Coder的基本联网的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆