Basic networking with Matlab Coder

2019-06-09 06:06发布

问题:

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?

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.

回答1:

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.

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.

Recommended Steps

  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
    
  3. 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.
  4. Generate a MEX function using something like:

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

    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.

  5. Generate standalone code and either let MATLAB Coder compile it to a library or deploy the code to an external IDE and compile it there.

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.

Further Reading

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.



回答2:

If you have the DSP System Toolbox, the System Objects dsp.UDPSender and dsp.UDPReceiver are supported for code generation since they are listed in the comprehensive list of supported functions.

The code generated from them relies on prebuilt libraries shipped with MATLAB and will run on desktop platforms compatible with those libraries. See the documentation for the UDP Receive block for more details.