how to use tcpclient in simulink matlab function block

9 visualizzazioni (ultimi 30 giorni)
Hi
Trying to use tcpclient in simulink's matlab function block(function is attached). But I get stuck with following error.
I am aware that it is somehow linked to tcpclient function.
I am looking for alternative solution for this. Please suggest ideal solution for this.
The same code work when I use it as matlab script.
========================
Function writeline is not supported for code generation. Function 'CS9 Action Communication' (#24.196.237), line 8, column 1: "writeline(tcpClient,sprintf("%f",action))" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Function readline is not supported for code generation. Function 'CS9 Action Communication' (#24.291.310), line 11, column 13: "readline(tcpClient)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Code generation does not support explicit calls to the 'delete' method. Function 'CS9 Action Communication' (#24.343.360), line 13, column 1: "delete(tcpClient)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'dReceived'. The first assignment to a local variable determines its class. Function 'CS9 Action Communication' (#24.387.396), line 15, column 25: "dReceived" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'dataReceived'. The first assignment to a local variable determines its class. Function 'CS9 Action Communication' (#24.556.568), line 21, column 9: "dataReceived" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'dataReceived'. The first assignment to a local variable determines its class. Function 'CS9 Action Communication' (#24.586.598), line 22, column 12: "dataReceived" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Undefined function or variable 'dataReceived'. The first assignment to a local variable determines its class. Function 'CS9 Action Communication' (#24.611.623), line 23, column 7: "dataReceived" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Errors occurred during parsing of MATLAB function 'HIL_rlwork_v3/CS9 Action Communication'
Component:MATLAB Function | Category:Coder error

Risposta accettata

Ayush
Ayush il 9 Gen 2024
I understand that you are getting the error when using tcpclient in simulink matlab function block. Most of your errors are associated with Code generation aspect. MATLAB Function block within Simulink are designed to be compatible with code generation. Simulink uses the MATLAB Coder to automatically generate C/C++ code from the algorithms you write inside MATLAB Function blocks. You may fix these errors by following workarounds:
  • Use a Supported Function: For example, you might use lower-level file I/O functions that are supported, although this may not be directly applicable for a TCP client.
  • Use Interpreted Execution: If the writeline function is essential and there is no alternative that is compatible with code generation, you can configure the MATLAB Function block to execute as interpreted MATLAB code instead of generating C code. This means the code will run in MATLAB rather than being compiled to C/C++. Refer this: https://www.mathworks.com/help/simulink/ug/simulation-modes.html
  • External Mode: If you are using the MATLAB Function block for real-time communication in external mode, you may need to create a custom S-Function or a System object that handles the TCP communication and is designed to support code generation. Refer this: https://www.mathworks.com/help/simulink/sfg/what-is-an-s-function.html
  • Generate a MEX Function: If you are not trying to deploy the code but only want to accelerate simulation, you can generate a MEX function instead of standalone C/C++ code. MEX generation may support more functions than standalone code generation. Refer this: https://www.mathworks.com/help/matlab/call-mex-file-functions.html
Thanks,
Ayush
  1 Commento
Chetan Thakur
Chetan Thakur il 16 Gen 2024
Modificato: Chetan Thakur il 16 Gen 2024
I tried interpreted execution for the matlab subsystem but it did not work. Whereas for external mode and MEX function, I did not understand how to get it to work. The instruction and guides are not easy to understand.
But to run the part of the function which cannot be built with simulink coder, I used coder.extrinsic() function. e.g. as follows
coder.extrinsic("writeline") writeline(tcpClient,sprintf("%f",action));
I did it for every blocking function call used in my function. For details on the use of this function see following link
It resovled my build errors and model is not executing properly. I still need to check the performance impact due to this. But the main goal is achieved.
Thanks
Chetan

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by