OPCHelp.cs
5.68 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OPCAutomation;
namespace OPCHelper
{
public class OPCHelp
{
string PCIP;
public OPCServer s7 = new OPCServer();
public OPCGroup s7Group;
/// <summary>
/// PC站IP地址
/// </summary>
/// <param name="OPCIP"></param>
public OPCHelp(string OPCIP)
{
PCIP = OPCIP;
}
/// <summary>
/// 打开OPC连接
/// </summary>
/// <returns></returns>
public bool OpenConn()
{
try
{
s7.Connect("OPC.SimaticNET", PCIP);
if (s7.ServerState == (int)OPCServerState.OPCRunning)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 关闭连接
/// </summary>
/// <returns></returns>
public bool CloseConn()
{
try
{
s7.Disconnect();
if (s7.ServerState == (int)OPCServerState.OPCDisconnected)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
/// <summary>
/// 获取OPCserver当前状态,TRUE:正在运行,false:连接关闭
/// </summary>
/// <returns></returns>
public bool GetConnStatus()
{
return s7.ServerState ==(int) OPCServerState.OPCRunning ? true : false;
}
/// <summary>
/// 创建组
/// </summary>
public bool CreateGroup(string name)
{
try
{
s7Group = s7.OPCGroups.Add(name);
SetGroupProperty();
}
catch (Exception)
{
//MessageBox.Show("创建组出现错误:" + err.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
/// <summary>
/// 设置组属性
/// </summary>
private void SetGroupProperty()
{
s7.OPCGroups.DefaultGroupIsActive = Convert.ToBoolean(true);
s7.OPCGroups.DefaultGroupDeadband = Convert.ToInt32(0);
s7Group.UpdateRate = Convert.ToInt32(250);
s7Group.IsActive = Convert.ToBoolean(true);
s7Group.IsSubscribed = Convert.ToBoolean(false);
}
/// <summary>
/// 添加项目地址
/// </summary>
public int AddAddr(string Addr, int ClientHandle)
{
int returnvalue = 0;
if (!string.IsNullOrEmpty(Addr))
{
try
{
OPCItem tempItem = s7Group.OPCItems.AddItem(Addr, ClientHandle);
//ServerHandle,这是一个重要的东西,个人理解为组中的项的索引,读取程序根据这个索引找到db块并进行读写操作。
//程序中应对ServerHandle与实际的变量地址已经这个变量地址所代表的意义进行一个映射。
returnvalue = tempItem.ServerHandle;
}
catch (Exception err)
{
}
}
return returnvalue;
}
/// <summary>
/// 读取数据,成功返回值,失败返回null
/// 按照 OPC 规范,数组始终以索引 1 开始。
/// </summary>
public Array ReadData(int[] handle)
{
try
{
int count = handle.Length;
int[] temp = new int[count + 1];
temp[0] = 0;
for (int i = 1; i < temp.Length; i++)
{
temp[i] = handle[i - 1];
}
Array serverHandles = (Array)temp;
Array values;
Array Errors;
object Qualities;
object TimeStamps;
//OPCAutomation.OPCDataSource.OPCCache;
s7Group.SyncRead(1, count, ref serverHandles, out values, out Errors, out Qualities, out TimeStamps);
return values;
}
catch (Exception err)
{
return null;
}
}
/// <summary>
/// 写入数据,handle,索引值数组,value对应值数组
/// 按照 OPC 规范,数组始终以索引 1 开始。
/// </summary>
public bool WriteData(int[] handle, object[] value)
{
try
{
int[] temp = new int[handle.Length + 1];
temp[0] = 0;
object[] temp1 = new object[handle.Length + 1];
temp1[0] = "";
for (int i = 1; i < temp.Length; i++)
{
temp[i] = handle[i - 1];
temp1[i] = value[i - 1];
}
Array serverHandles = (Array)temp;
Array values = (Array)temp1;
Array Errors;
//OPCAutomation.OPCDataSource.OPCCache;
s7Group.SyncWrite(handle.Length, ref serverHandles, ref values, out Errors);
return true;
}
catch (Exception)
{
return false;
}
}
}
}