<< 点击显示目录 >> 主页 PVI通信 > PVI帮助信息 > PVI Services > 所有列表类型的基础服务 > 用于管理通信对象的列表 > ModuleCollection class |
该类中的一个对象代表一个模块对象的集合。当一个CPU对象被创建时,这个类型的实例会被自动创建,可以使用 CPU对象的 Modules 属性 来访问 。这使得 Upload 函数调用可以从控制器中读取模块信息。
cpu.Modules.Upload();
如果CPU对象(cpu)已经正确连接,那么ModuleCollection对象的 Uploaded 事件报告说,模块信息已经从控制器正确读取。因此,该列表包含了控制器上每个BR模块的PVI服务模块对象。因此,模块对象信息的通信服务可以使用字符串索引轻松、快速地访问。
// 通过列表确定一个B&R模块的内存类型
MemoryType memType = cpu.Modules["myTask"].MemoryType;
此外,还可以从控制器中读取几个BR模块,或者用一个函数调用将几个模块传输到控制器中。额外的事件告知用户单个模块指令的当前进度 ( UploadProgress, DownloadProgress )或整个列表的进度(CollectionUploadProgress, CollectionDownloadProgress)。 这样就可以在应用程序中实现一个进度指示器,从而可以显示单个模块的进度或整个列表的指令。如果用户特定列表中的单个模块对象有不同的CPU对象作为父对象,那么调用一次下载方法就可以将几个模块加载到不同的控制器中。
"Modules" example
下面的例子创建了模块对象,并将它们添加到一个用户专用的ModuleCollection对象中。一旦建立了与控制器上各个模块的连接,就可以读取或下载BR模块。
// Definition of global communication objects
Service service;
Cpu cpu;
ModuleCollection moduleCollection;
/// <summary>
/// Generate and connect service object
/// </summary>
private void btConnectPlc_Click(object sender, System.EventArgs e)
{
if ( service == null )
{
service = new Service("service");
service.Connected+=new PviEventHandler(service_Connected);
}
service.Connect();
}
/// <summary>
/// Connect CPU object if service object connection successful
/// </summary>
private void service_Connected(object sender, PviEventArgs e)
{
if ( cpu == null )
{
// 创建CPU对象并添加事件处理程序
cpu = new Cpu(service,"cpu");
cpu.Connected += new PviEventHandler(cpu_Connected);
// 为一个串行连接设置连接属性
cpu.Connection.DeviceType = DeviceType.Serial;
cpu.Connection.Serial.BaudRate = 57600;
cpu.Connection.Serial.Channel = 1;
}
// Connect CPU
cpu.Connect();
}
/// <summary>
/// Output appropriate message if
/// connection to CPU successful
/// </summary>
private void cpu_Connected(object sender, PviEventArgs e)
{
String strOut = String.Format("\"{0}\" connected. BootMode: {1}\r\n",
((Cpu)sender).Name,
((Cpu)sender).BootMode.ToString());
this.tbStatus.Text += strOut;
this.btAddToUserCollection.Enabled = true;
this.btConnectList.Enabled = true;
}
/// <summary>
/// Add new module object to the user-specific list
/// </summary>
private void btAddToUserCollection_Click(object sender, System.EventArgs e)
{
if ( this.tbModuleName.Text.Length > 0 )
{
this.lbUserModules.Items.Add(this.tbModuleName.Text);
if ( moduleCollection == null )
{
// 创建针对用户的列表
moduleCollection = new ModuleCollection(this.service,"MyModules");
/*********************************/
/* Add handling routines */
/*********************************/
// 处理个别连接的事件
moduleCollection.Connected += new PviEventHandler(ModuleConnected);
// 当整个列表连接成功时进行处理
moduleCollection.CollectionConnected += new CollectionEventHandler(ModuleCollectionConnected);
// 处理个别上传的事件
moduleCollection.ModuleUploaded += new PviEventHandler(ModuleUploaded);
// 当整个列表成功上传时的处理
moduleCollection.CollectionUploaded += new CollectionEventHandler(ModuleCollectionUploaded);
// 处理各个模块的进度指标
moduleCollection.UploadProgress += new ModuleEventHandler(ModuleProgress);
// 处理整个列表的进度指示器
moduleCollection.CollectionUploadProgress += new ModuleCollectionEventHandler(ModuleCollectionProgress);
// 处理个别下载事件
moduleCollection.ModuleDownloaded += new PviEventHandler(ModuleDownloaded);
// 当整个列表下载成功时进行处理
moduleCollection.CollectionDownloaded += new CollectionEventHandler(ModuleCollectionDownloaded);
// 处理各个模块的进度指标
moduleCollection.DownloadProgress += new ModuleEventHandler(ModuleProgress);
// 处理整个列表的进度指示器
moduleCollection.CollectionDownloadProgress += new ModuleCollectionEventHandler(ModuleCollectionProgress);
}
// 添加一个新的模块对象
Module mod = new Module(this.cpu,this.tbModuleName.Text);
// 为以后的下载调用设置文件名
mod.FileName = String.Format("C://Modules//{0}.br%22,this.tbModuleName.Text");
moduleCollection.Add(mod);
}
this.tbModuleName.Text = "";
}
/// <summary>
/// Connect all module objects of the user-specific list
/// </summary>
private void btConnectList_Click(object sender, System.EventArgs e)
{
this.tbStatus.Text += "\r\n";
moduleCollection.Connect();
}
/// <summary>
/// Load all modules to the controller
/// </summary>
private void btDownload_Click(object sender, System.EventArgs e)
{
this.tbStatus.Text += "\r\n";
this.pbModuleCollection.Value = 0;
this.moduleCollection.Download(MemoryType.UserRam,InstallMode.Overload);
}
/// <summary>
/// Read all modules in the user-specific list
/// </summary>
private void btUpload_Click(object sender, System.EventArgs e)
{
this.tbStatus.Text += "\r\n";
moduleCollection.Upload("c://Modules");
}
/// <summary>
/// Handling individual Connected events
/// </summary>
private void ModuleConnected(object sender, PviEventArgs e)
{
String strOut = String.Format("\"{0}\" connected\r\n",((Module)sender).Name);
this.tbStatus.Text += strOut;
}
/// <summary>
/// Handling when the entire list connected successfully
/// </summary>
private void ModuleCollectionConnected(object sender, CollectionEventArgs e)
{
String strOut = String.Format("\"{0}\" connected\r\n",((ModuleCollection)sender).Name);
this.tbStatus.Text += strOut;
this.btUpload.Enabled = true;
}
/// <summary>
/// Handling individual Uploaded events
/// </summary>
private void ModuleUploaded(object sender, PviEventArgs e)
{
String strOut = String.Format("\"{0}\" uploaded\r\n",((Module)sender).Name);
this.tbStatus.Text += strOut;
}
/// <summary>
/// Handling when the entire list Uploaded successfully
/// </summary>
private void ModuleCollectionUploaded(object sender, CollectionEventArgs e)
{
String strOut = String.Format("\"{0}\"uploaded\r\n",((ModuleCollection)sender).Name);
this.tbStatus.Text += strOut;
this.btDownload.Enabled = true;
}
/// <summary>
/// Handling the progress indicator of the individual modules
/// </summary>
private void ModuleProgress(object sender, ModuleEventArgs e)
{
this.lbPbModule.Text = ((Module)sender).Name + " ...";
this.pbModule.Value = e.Percentage;
}
/// <summary>
/// Handling the progress indicator for the entire list
/// </summary>
private void ModuleCollectionProgress(object sender, ModuleCollectionProgressEventArgs e)
{
this.pbModuleCollection.Value = e.Percentage;
}
/// <summary>
/// Handling individual Downloaded events
/// </summary>
private void ModuleDownloaded(object sender, PviEventArgs e)
{
String strOut = String.Format("\"{0}\" downloaded\r\n",((Module)sender).Name);
this.tbStatus.Text += strOut;
}
/// <summary>
/// Handling when the entire list downloaded successfully
/// </summary>
private void ModuleCollectionDownloaded(object sender, CollectionEventArgs e)
{
String strOut = String.Format("\"{0}\" downloaded\r\n",((ModuleCollection)sender).Name);
this.tbStatus.Text += strOut;
}