private void LoadButton_Click(object sender, EventArgs e) { if (xPCTarget.IsConnected == true) { try { application = xPCTarget.Load("C:\\Users\\Closer\\Desktop\\matlab\\xpctank1.dlm"); // 设置hostScope来获取xPCTarget的信号信息 SetAppHostScopes(); xPCScopes scopes = application.Scopes; xPCHostScopeCollection hScopes = scopes.HostScopes; xPCTargetScopeCollection tScopes = scopes.TargetScopes; // 指定参数 xPCParameters param = xPCTarget.Application.Parameters; // 先清空参数 param.Refresh(); // 获取参数的坐标和参数名 xPCParameter p = param["SetPoint", "Value"]; Double[] values = new Double[1]; //values[0] = Double.Parse(ParaText.Text); values[0] = 5.0; // 指定参数值 p.SetParam(values); // 设置采样时间 application.SampleTime = 0.01; // 设置结束时间 单位秒 -1是永不停止 application.StopTime = 360; tScopes.StartAll(); hScopes.StartAll(); Console.WriteLine("load successfully"); } catch (xPCException ex) { Console.WriteLine(ex.Message); MessageBox.Show(ex.Message); } } else { Console.WriteLine("please connect first"); //Call the Disconnect methods to close the communication channel with the target PC. xPCTarget.Disconnect(); } }
void demo() { // xPCTargetPC设置IP和端口 xPCTargetPC xPCTarget = new xPCTargetPC(); xPCTarget.TcpIpTargetAddress = "192.168.88.189"; xPCTarget.TcpIpTargetPort = "22222"; try { // 建立连接 xPCTarget.Connect(); if (xPCTarget.IsConnected == true) { Console.WriteLine(true); // 装载模型,并获取target机的应用 xPCApplication application = xPCTarget.Load("C:\\Users\\Closer\\Desktop\\matlab\\xpctank1.dlm"); xPCTargetScopeCollection tScopes = application.Scopes.TargetScopes; tScopes.Refresh(); // 指定参数 xPCParameters param = xPCTarget.Application.Parameters; // 先清空参数 param.Refresh(); // 获取参数的坐标和参数名 xPCParameter p = param["SetPoint", "Value"]; Double[] values = new Double[] { 5.0 }; // 指定参数值 p.SetParam(values); // 设置采样时间 application.SampleTime = 0.01; // 设置结束时间 单位秒 -1是永不停止 application.StopTime = 60; // target机开始运行 application.Start(); } else { //Call the Disconnect methods to close the communication channel with the target PC. xPCTarget.Disconnect(); } } catch (xPCException ex) { Console.WriteLine(ex.Message); } }