有时仿真模型需要与Excel做数据的交互,readExcelFile, writeExcelFile这两个函数可以作为大量数据的读取和写入,但是这种方法略显笨重,不适合数据的动态读取和写入。利用DDE技术可以实现数据的动态交互。

效果展示:

统计模型Buffer中MU数量随时间的变化,在Excel中动态生成Buffer容量的折线图。

建模过程:

搭建方式:

软件版本为13.1,模型搭建方式如视频中所示,Source产生频率为5分钟,SingleProc的加工时间为0到10分钟的均匀分布随机数。

函数说明:

模型中共有三个Method,分别是 init, SetTime, DDEData。运用SimTalk 2.0编写。

init:负责初始化模型,连接DDE。程序如下:

DDEDisconnectAll
channel:=ddeConnect(“Excel”,”Sheet1″)
if   channel=0
debug
end

SetTime:负责改变SingleProc的加工时间。

DDEData:负责向Excel传输数据。监测Buffer.NumMU值的变化,在Buffer.NumMU数量改变时触发。

param newValue: integer

var Cells:string:=sprint(“R”,Buffer.StatNumIn+2,”C1″)
ddePoke(channel,Cells,sprint(newValue),1000)
if  Buffer.StatNumIn=1000
eventController.stop
end

相关函数介绍:

Plant Simulation 作为服务端:

initDDE(Servername:<string>)  :  建立自定义名称的DDE服务器。

closeDDE  :  关闭自定义的DDE服务器。

作为服务端的其他部分程序,需要客户端软件定义,如利用Excel读取Plant Simulation 数据,可能需要利用VBA编写相应的程序,这里不做详细介绍。

Plant Simulation 作为客户端:

ddeConnect(ServerApplication:<string>, Topic:<string>)

连接到其他程序,返回值为channel编号,无法连接时返回0。Topic参数分为”data”,”system”,”info”,参数详情见软件帮助文档。

示例:var  channel : integer :=ddeConnect (“eM-Plant”,”data”)

ddeDisconnect(NameOfConnection:<integer>)

取消某个channel的连接。

ddeDisconnectAll

取消全部连接。

ddeAdvise(OpenedChannel:<integer>, Item:<string>, ModelPath:<string>)

创建一个热连接,当连接到的值发生改变时会触发相应的Method。返回值为当前的热连接编号。

示例:

channel := ddeConnect(“eM-Plant”,”data”)  //连接到eM-Plant
hotlink := ddeAdvise(channel,”.frame1.Variable”, “.evaluate.method”)  //当.frame1.Variable的值发生变化时,会触发.evaluate.method

ddeUnadvise(NameOfHotlink:<integer>)

取消热连接。

ddeExecute(OpenedChannel:<integer>, Command:<string>, Timeout:<integer>)

通过DDE传递命令到连接的程序去执行。

示例:

channel := ddeConnect(“Excel”,”Sheet1″)  //连接到Excel
DDEExecute(channel,”[App.Maximize]”,1000)  //执行将Excel窗口最大化命令
ddeDisconnect(channel)  //取消连接

ddePoke(OpenedChannel:<integer>, Topic:<string>, Data:<string>, Timeout:<integer>)

将一个值传递到连接的程序中。

示例:

channel := ddeConnect(“eM-Plant”,”data”)  //连接到eM-Plant
ddePoke(channel,”.Frame2.Variable12″,”9″,1000)  //将.Frame2.Variable12的值变为”9″

ddeRequest(OpenedChannel:<integer>, PathToObject:<string>)

查询连接到的程序中的某个值。

示例:

channel := ddeConnect(“eM-Plant”,”data”)  //连接到eM-Plant
print ddeRequest(channel,”.frame1.Variable”)  //查询.frame1.Variable的值

1 对 “动态数据交换技术(DDE)应用:与Excel数据交互”的想法;

  1. 小编好,我最近在学习Plantsim-Excel之间的DDE技术,遇到了门槛。请问方便,把DDE的这个案例分析文件发给我吗?或者一份详细的教程。我的邮箱,zxfei1987@126.com

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注