1. 添加用户组用户
2. 导入openness.dll文件
- .dll文件位置
C:\Program Files\Siemens\Automation\Portal V19\PublicAPI\V19
- 添加项目引用
- 引用的命名空间
using Siemens.Engineering;
2. TIA Portal Openness的对象模型
- 可以通过索引号像数组一样对列表中的对象进行寻址
- 可以使用Find类函数,比如
Screen screen = screens.Find("myScreen")
- 直接使用符号名称找到对象
3. 找到SofterwareContainer
- 和FB,FC,OB,Variant等等相关的API接口
4. 打开博图
//INIT
TiaPortal my_Ti;
Project my_proj = null;
List<Device> my_devicelist = new List<Device>();
//RUN TIAPORTAL
public int runTiaportal(bool tiaMode)
{
if (tiaMode)
{
my_Ti = new TiaPortal(TiaPortalMode.WithUserInterface);
}
else
{
my_Ti = new TiaPortal(TiaPortalMode.WithoutUserInterface);
}
return my_Ti.GetCurrentProcess().Id;
}
5. 关闭博图
//STOP TIAPORTAL
public void stopTiaportal()
{
Process[] my_process = Process.GetProcesses();
foreach (Process item in my_process)
{
if (item.Id == my_Ti.GetCurrentProcess().Id)
{
item.Kill();
break;
}
}
}
6. 新建项目
//NEW PROJECT
public void newProject(string my_projectname)
{
ProjectComposition proCom = my_Ti.Projects;
FolderBrowserDialog my_browserDialog = new FolderBrowserDialog();
string my_path = string.Empty;
if (my_browserDialog.ShowDialog() == DialogResult.OK)
{
my_path = my_browserDialog.SelectedPath;
DirectoryInfo my_targetDir = new DirectoryInfo(my_path);
my_proj = proCom.Create(my_targetDir, my_projectname);
}
}
7. 运行项目
//RUN PROJECT
public void runProject()
{
ProjectComposition proCom = my_Ti.Projects;
OpenFileDialog my_opendialog = new OpenFileDialog();
my_opendialog.Multiselect = false;
my_opendialog.Title = "choic Tia Project";
my_opendialog.Filter = "All files(*.*)|*.*";
my_opendialog.InitialDirectory = @"D:\";
if (my_opendialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string my_projectfile = my_opendialog.FileName;
FileInfo my_path = new FileInfo(my_projectfile);
my_proj = proCom.Open(my_path);
}
}
8. 设备和网络视图
//DEVICES & NETWORKS
public void openHWView(int choic_params)
{
switch (choic_params)
{
case 0:
my_proj.ShowHwEditor(Siemens.Engineering.HW.View.Network);
break;
case 1:
my_proj.ShowHwEditor(Siemens.Engineering.HW.View.Topology);
break;
case 2:
my_proj.ShowHwEditor(Siemens.Engineering.HW.View.Device);
break;
default:
break;
}
}
9. 创建设备
//CREATE DEVICE
public void createDevice(string deviceitemtypeid, string deviceitemname, string devicename)
{
DeviceComposition my_devicecompos = my_proj.Devices;
Device my_device = my_devicecompos.CreateWithItem(deviceitemtypeid, deviceitemname, devicename);
}
public void createDevice(string devicetypeid, string devicename)
{
//example-->my_devicetypeid:"System:Device.S7300"
//example-->my_devicename:"S7300Device"
DeviceComposition my_devicecompos = my_proj.Devices;
Device my_device = my_devicecompos.Create(devicetypeid, devicename);
}
10. 枚举设备
//ENUM ROOT DEVICE
public List<Device> enumDevice()
{
DeviceComposition my_devicecompos = my_proj.Devices;
foreach (Device dev in my_devicecompos)
{
my_devicelist.Add(dev);
}
return my_devicelist;
}
11. 删除设备
//DELETE DEVICE
public void deleteDevice(string deletedevicename)
{
Device my_deleteDevice = my_proj.UngroupedDevicesGroup.Devices.Find(deletedevicename);
my_deleteDevice.Delete();
}
12. 找到设备
//FIND DEVICES
public Device findDevice(string devicename)
{
//devicename:"S7-1500/ET200MP station_1"
Device my_device = my_proj.Devices.Find("devicename");
return my_device;
}
13. 插入新设备
//PLUG NEW DEVICE
public void plugNewdevice(string typeidentifier, string name, int positionnumber)
{
HardwareObject my_hwobject = my_proj.Devices[0].DeviceItems[1];
if (my_hwobject.CanPlugNew(typeidentifier, name, positionnumber))
{
DeviceItem my_plugnewdeviceitem = my_hwobject.PlugNew(typeidentifier, name, positionnumber);
}
}
14. 关闭和保存项目
//SAVE PROJECT
public void saveProject()
{
my_proj.Save();
}
//CLOSE PROJECT
public void closeProject()
{
my_proj.Close();
}
15. 上载项目
//DEVICE UPLOAD
public void UploadPLCprogram()
{
StationUploadProvider my_canbeuploadproj = my_proj.GetService<StationUploadProvider>();
StationUploadProvider my_upload_proj = my_canbeuploadproj;
ConnectionConfiguration my_conf = my_upload_proj.Configuration;
ConfigurationMode my_confmode = my_conf.Modes.Find("PN/IE");
ConfigurationPcInterface my_pcinterface = my_confmode.PcInterfaces.Find("Intel(R) I210 Gigabit Network Connection <2>", 1);
ConfigurationAddress my_uploadaddress = my_pcinterface.Addresses.Create("192.168.0.1");
Device my_uploadedobj = null;
UploadConfigurationDelegate my_uploaddelegate = PreConfigureUpload;
UploadResult my_uploadresult = my_upload_proj.StationUpload(my_uploadaddress, PreConfigureUpload);
my_uploadedobj = my_uploadresult.UploadedStation;
void PreConfigureUpload(UploadConfiguration my_uploadconf)
{
ModuleReadAccessPassword my_module_readpsw = my_uploadconf as ModuleReadAccessPassword;
if (my_module_readpsw != null)
{
Console.WriteLine("psw error!");
return;
}
ModuleWriteAccessPassword my_module_writepsw = my_uploadconf as ModuleWriteAccessPassword;
if (my_module_writepsw != null)
{
Console.WriteLine("psw error!");
return;
}
throw new NotSupportedException();
}
}
16. 代码整理如下
using Siemens.Engineering;
using Siemens.Engineering.Connection;
using Siemens.Engineering.HW;
using Siemens.Engineering.Upload;
using Siemens.Engineering.Upload.Configurations;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Security.Permissions;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace openness_tt
{
internal class openess_func
{
//INIT
TiaPortal my_Ti;
Project my_proj = null;
List<Device> my_devicelist = new List<Device>();
//RUN TIAPORTAL
public int runTiaportal(bool tiaMode)
{
if (tiaMode)
{
my_Ti = new TiaPortal(TiaPortalMode.WithUserInterface);
}
else
{
my_Ti = new TiaPortal(TiaPortalMode.WithoutUserInterface);
}
return my_Ti.GetCurrentProcess().Id;
}
//STOP TIAPORTAL
public void stopTiaportal()
{
Process[] my_process = Process.GetProcesses();
foreach (Process item in my_process)
{
if (item.Id == my_Ti.GetCurrentProcess().Id)
{
item.Kill();
break;
}
}
}
//NEW PROJECT
public void newProject(string my_projectname)
{
ProjectComposition proCom = my_Ti.Projects;
FolderBrowserDialog my_browserDialog = new FolderBrowserDialog();
string my_path = string.Empty;
if (my_browserDialog.ShowDialog() == DialogResult.OK)
{
my_path = my_browserDialog.SelectedPath;
DirectoryInfo my_targetDir = new DirectoryInfo(my_path);
my_proj = proCom.Create(my_targetDir, my_projectname);
}
}
//RUN PROJECT
public void runProject()
{
ProjectComposition proCom = my_Ti.Projects;
OpenFileDialog my_opendialog = new OpenFileDialog();
my_opendialog.Multiselect = false;
my_opendialog.Title = "choic Tia Project";
my_opendialog.Filter = "All files(*.*)|*.*";
my_opendialog.InitialDirectory = @"D:\";
if (my_opendialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string my_projectfile = my_opendialog.FileName;
FileInfo my_path = new FileInfo(my_projectfile);
my_proj = proCom.Open(my_path);
}
}
//DEVICES & NETWORKS
public void openHWView(int choic_params)
{
switch (choic_params)
{
case 0:
my_proj.ShowHwEditor(Siemens.Engineering.HW.View.Network);
break;
case 1:
my_proj.ShowHwEditor(Siemens.Engineering.HW.View.Topology);
break;
case 2:
my_proj.ShowHwEditor(Siemens.Engineering.HW.View.Device);
break;
default:
break;
}
}
//CREATE DEVICE
public void createDevice(string deviceitemtypeid, string deviceitemname, string devicename)
{
DeviceComposition my_devicecompos = my_proj.Devices;
Device my_device = my_devicecompos.CreateWithItem(deviceitemtypeid, deviceitemname, devicename);
}
public void createDevice(string devicetypeid, string devicename)
{
//example-->my_devicetypeid:"System:Device.S7300"
//example-->my_devicename:"S7300Device"
DeviceComposition my_devicecompos = my_proj.Devices;
Device my_device = my_devicecompos.Create(devicetypeid, devicename);
}
//ENUM ROOT DEVICE
public List<Device> enumDevice()
{
DeviceComposition my_devicecompos = my_proj.Devices;
foreach (Device dev in my_devicecompos)
{
my_devicelist.Add(dev);
}
return my_devicelist;
}
//DELETE DEVICE
public void deleteDevice(string deletedevicename)
{
Device my_deleteDevice = my_proj.UngroupedDevicesGroup.Devices.Find(deletedevicename);
my_deleteDevice.Delete();
}
//FIND DEVICES
public Device findDevice(string devicename)
{
//devicename:"S7-1500/ET200MP station_1"
Device my_device = my_proj.Devices.Find("devicename");
return my_device;
}
//PLUG NEW DEVICE
public void plugNewdevice(string typeidentifier, string name, int positionnumber)
{
HardwareObject my_hwobject = my_proj.Devices[0].DeviceItems[1];
if (my_hwobject.CanPlugNew(typeidentifier, name, positionnumber))
{
DeviceItem my_plugnewdeviceitem = my_hwobject.PlugNew(typeidentifier, name, positionnumber);
}
}
//SAVE PROJECT
public void saveProject()
{
my_proj.Save();
}
//CLOSE PROJECT
public void closeProject()
{
my_proj.Close();
}
//DEVICE UPLOAD
public void UploadPLCprogram()
{
StationUploadProvider my_canbeuploadproj = my_proj.GetService<StationUploadProvider>();
StationUploadProvider my_upload_proj = my_canbeuploadproj;
ConnectionConfiguration my_conf = my_upload_proj.Configuration;
ConfigurationMode my_confmode = my_conf.Modes.Find("PN/IE");
ConfigurationPcInterface my_pcinterface = my_confmode.PcInterfaces.Find("Intel(R) I210 Gigabit Network Connection <2>", 1);
ConfigurationAddress my_uploadaddress = my_pcinterface.Addresses.Create("192.168.0.1");
Device my_uploadedobj = null;
UploadConfigurationDelegate my_uploaddelegate = PreConfigureUpload;
UploadResult my_uploadresult = my_upload_proj.StationUpload(my_uploadaddress, PreConfigureUpload);
my_uploadedobj = my_uploadresult.UploadedStation;
void PreConfigureUpload(UploadConfiguration my_uploadconf)
{
ModuleReadAccessPassword my_module_readpsw = my_uploadconf as ModuleReadAccessPassword;
if (my_module_readpsw != null)
{
Console.WriteLine("psw error!");
return;
}
ModuleWriteAccessPassword my_module_writepsw = my_uploadconf as ModuleWriteAccessPassword;
if (my_module_writepsw != null)
{
Console.WriteLine("psw error!");
return;
}
throw new NotSupportedException();
}
}
}
}
17. Openness参考资料及对象模型工具资料
openness Demo参考
openness 对象模型工具下载
openness 文档下载