本分析RTLAB软件自带的异步通信案例中的源代码。
更多内容可参考异步通信流程
该案例在…/Examples/IO/Generic/asyncip中,打开模型后,主要内容如下图所示:
该模型首先将模型内部运行数据,通过端口发送到第三方程序app,app接收到数据后,再将其返回,模型接收到数据后,再将数据显示出来。
下面的代码展示了app中的接收与发送是如何实现的。
注:该程序其实只是常规的socket编程流程,具体可参考socket编程基本流程
#include <sys/types.h>
#include <stdio.h>
#include <conio.h>
#include <winsock.h>
#include <time.h>
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "wsock32.lib")
#pragma comment(lib, "advapi32.lib")
/*-----------------------------------------------------------------
* Displays the packet (array of double) received and sends it back
* to the originator.
*-----------------------------------------------------------------*/
void main(void)
{
struct sockaddr_in server_ad;
struct sockaddr_in client_ad;
int sd,sd2;
int port;
int length;
int n,iter=