摘 要: 為了解決Simulink與VC++的數(shù)據(jù)交互問題,提出了采用C++語言S?函數(shù)實現(xiàn)兩種開發(fā)環(huán)境的網(wǎng)絡(luò)通信方法。首先介紹了S?函數(shù)以及基于S?函數(shù)的編程框架,其次介紹了UDP網(wǎng)絡(luò)程序設(shè)計模型。最后結(jié)合實例說明并驗證了該方法的有效性和實用性。
關(guān)鍵詞: S?函數(shù); Simulink; VC++; UDP
中圖分類號: TN711?34 文獻標識碼: A 文章編號: 1004?373X(2013)13?0108?04
Research on network communication between Simulink and VC++ based on S?function
ZHOU Tao
(Xi’an Aircraft Industry (group) Company Ltd., AVIC, Xi’an 710089, China)
Abstract: In order to solve the problem of data interaction between the Simulink and Visual C++, a network communication method utilized the S?function of C++ language to realize the two developing environment. The S?function, the programming framework based on it and the design model of UDP network programming are introduced. the effectiveness and the practicability of this method are proved with examples.
Keywords: S?function; Simulink; VC++; UDP
0 引 言
Simulink是Matlab的重要組件之一,具有強大的系統(tǒng)建模和動態(tài)仿真能力,而VC++是Microsoft推出的可視化編程環(huán)境,具有豐富的Windows API,可以開發(fā)友好的人機界面,而且運行穩(wěn)定高效。實際應(yīng)用中經(jīng)常會結(jié)合兩者的優(yōu)勢,進行聯(lián)合仿真,其中必然存在兩種開發(fā)環(huán)境的網(wǎng)絡(luò)通信問題,本文采用S?函數(shù)(System Function,系統(tǒng)函數(shù))構(gòu)建一個單獨的通信模塊,通過C++的Socket通信來實現(xiàn)Simulink與VC++的數(shù)據(jù)交互,并結(jié)合實例對該方法進行了驗證。
1 S?函數(shù)簡介
S?函數(shù)是Simulink環(huán)境下的功能擴展機制,能有效提高和豐富Simulink的功能。S?函數(shù)提供了一種用Matlab、C、C++或者FORTRAN等多種計算機語言描述Simulink模塊的方法,將上述計算機編寫的S?函數(shù)用MEX工具編譯產(chǎn)生MEX文件。S?函數(shù)中用一種特定的回調(diào)語法,使用戶可以和Simulink Engine交互,此類交互和Simulink內(nèi)置模塊與Engine間的交互極為相似。這種回調(diào)語法稱為S?函數(shù)API。
2 基于S?函數(shù)的編程框架
C++語言S?函數(shù)采用標準C++語言規(guī)范,并且產(chǎn)生可獨立運行的MEX文件。 C++語言的S?函數(shù)的編寫采用回調(diào)函數(shù)的方式, Simulink Engine在不同的仿真階段調(diào)用相應(yīng)的回調(diào)函數(shù),執(zhí)行指定的任務(wù)。Matlab提供了S?函數(shù)的編程模板,指定了標準的回調(diào)方法,只需在其中加入特定的功能代碼即可。其基本框架和對應(yīng)的回調(diào)方法如圖1所示。……