openness的应用

1. 添加用户组用户

2. 导入openness.dll文件

  • .dll文件位置C:\Program Files\Siemens\Automation\Portal V19\PublicAPI\V19

  • 添加项目引用

  • 引用的命名空间
    1
    using Siemens.Engineering;

2. TIA Portal Openness的对象模型

  • 可以通过索引号像数组一样对列表中的对象进行寻址
  • 可以使用Find类函数,比如Screen screen = screens.Find("myScreen")
  • 直接使用符号名称找到对象

3. 找到SofterwareContainer

  • 和FB,FC,OB,Variant等等相关的API接口

4. 打开博图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//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. 关闭博图

1
2
3
4
5
6
7
8
9
10
11
12
13
//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. 新建项目

1
2
3
4
5
6
7
8
9
10
11
12
13
//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. 运行项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//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. 设备和网络视图

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//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. 创建设备

1
2
3
4
5
6
7
8
9
10
11
12
13
//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. 枚举设备

1
2
3
4
5
6
7
8
9
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. 删除设备

1
2
3
4
5
6
//DELETE DEVICE
public void deleteDevice(string deletedevicename)
{
Device my_deleteDevice = my_proj.UngroupedDevicesGroup.Devices.Find(deletedevicename);
my_deleteDevice.Delete();
}

12. 找到设备

1
2
3
4
5
6
7
//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. 插入新设备

1
2
3
4
5
6
7
8
9
//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. 关闭和保存项目

1
2
3
4
5
6
7
8
9
10
11
//SAVE PROJECT
public void saveProject()
{
my_proj.Save();
}

//CLOSE PROJECT
public void closeProject()
{
my_proj.Close();
}

15. 上载项目

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//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. 代码整理如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
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 文档下载


openness的应用
http://example.com/2024/07/21/openness的应用/
作者
xiao cuncun
发布于
2024年7月21日
许可协议