tcpip in S function

조회 수: 3 (최근 30일)
Aleck Wu
Aleck Wu 2011년 5월 12일
hello, i'm pretty new to matlab really, so what i know is pretty superficial (i.e. following templates). i've been programming for a while though in a few other languages so if out of luck i'd still be able to get things done with low level coding. anyhow, a little bit on what i'm trying to accomplish.
i'm trying to write a level 1 s function, (level 1 due to its simplicity and my lack of knowledge). that s function will in itself act as a UAV (unmanned aerial vehicle). The S function will poll data off of the sensors on the UAV via a tcpip connection, and these sensory information will be output to i.e. another program with an algorithm to process these information. the outputs of that algorithm will serve as the inputs for my S function, and these inputs will then be transmitted to the UAV again via tcpip inside the S function.
the problem i'm having is keeping the tcpip port openned during the simulation. if you try to open a file, a fileID is returned and that can be passed one as one of the state variables. however, opening a tcpip port generates an object, and i have no idea how the port can be opened inside mdlInitializeSizes and still be used else where. (global variables didn't seem to work for me, although i might be doing that wrong as well...). the only possible work around is to open and close the port during each step, i'm guessing this will be costly timewise, and might not work for this application.
any ideas? functions i should look into etc?
Thanks!

답변 (1개)

Kaustubha Govind
Kaustubha Govind 2011년 5월 13일
If you are using Level 1 MATLAB S-functions, I would highly recommend considering Level-2 S-functions instead. I think the primary difference is actually the fact that Level 1 S-functions have long been deprecated and only exist for the sake of backwards compatibility. Level 2 S-functions have greater features and capabilities to offer, and at least Level 2 MATLAB S-functions are not that much harder to implement than their Level 1 counterparts.
Coming to your question - PWork vectors are the ideal solution here, but unfortunately, they are only supported with C/C++ MEX S-functions (Level 1 MATLAB S-functions do not support any work vectors, and Level 2 MATLAB S-functions support only DWork vectors which cannot store non-numeric values).
Although I hesitate to propose this - if you do not plan to have multiple instances of this S-function running simultaneously - using global variables (as you seem to have already tried) may be the most appropriate solution. Could you provide a brief description of how you tried using a global variable to store the tcpip object and how it failed?
  댓글 수: 1
Aleck Wu
Aleck Wu 2011년 5월 13일
Thank you, I will not be running multiple instances of this S-Function and therefore I can use global variables. The reason why global variables didn't work is purely a silly mistake on my part (as I didn't declare it to be global in every function I used it in, I never had to do this in any other languages). As well I will also look into level-2 S-Functions. But I think with only a superficial 'experience' with matlab, I might not notice a difference.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by