222
schangxiang@126.com
2025-09-29 f782248da68c035aae12f902f29d828e9867abb0
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
using iWareCc.Properties;
using iWareCommon.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareCc.Util
{
  public  class OPCHelper
    {
 
    //    /// <summary>
    //    /// 读取指定地址的OPC的值
    //    /// </summary>
    //    /// <param name="ItemNames">需要读取的地址列表</param>
    //    /// <returns>输入地址对应的值</returns>
    //    public static List<object> OPCRead(List<string> ItemNames)
    //    {
    //        //输入地址为空时输出空
    //        if (ItemNames == null || ItemNames.Count == 0)
    //        {
    //            return null;
    //        }
 
    //        int count = ItemNames.Count;
    //        List<object> readRtn = new List<object>();//设置返回值数组
    //        try
    //        {
 
    //            using (var srv = new OPCXML_DataAccess())
    //            {
    //                ReadRequestItemList ItemLists = new ReadRequestItemList();
    //                ItemLists.Items = new ReadRequestItem[count];
    //                for (int i = 0; i < count; i++)
    //                {
    //                    ItemLists.Items[i] = new ReadRequestItem();
    //                    ItemLists.Items[i].ItemPath = "";
    //                    ItemLists.Items[i].ItemName = ItemNames[i];
    //                }
    //                RequestOptions opt = new RequestOptions();
 
    //                ReplyItemList ItemValues = new ReplyItemList(); ;
    //                OPCError[] Errors;
    //                opt.ReturnItemTime = true;
    //                srv.Read(opt, ItemLists, out ItemValues, out Errors);
    //                foreach (var p in ItemValues.Items)
    //                {
    //                    readRtn.Add(p.Value);
    //                }
    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", "OPCHelper", "OPCRead", ex.Message);
    //        }
    //        return readRtn;
    //    }
 
    //    /// <summary>
    //    /// 单值写入OPCServer
    //    /// </summary>
    //    /// <param name="ItemName">地址块</param>
    //    /// <param name="value">值</param>
    //    /// <returns>是否写入成功</returns>
    //    public static bool OPCWritePoint(string ItemName, object value)
    //    {
    //        try
    //        {
    //            using (var srv = new OPCXML_DataAccess())
    //            {
    //                WriteRequestItemList ItemLists = new WriteRequestItemList();
    //                ItemLists.Items = new ItemValue[1];
    //                ItemLists.Items[0] = new ItemValue();
    //                ItemLists.Items[0].ItemPath = "";
    //                ItemLists.Items[0].ItemName = ItemName;
    //                ItemLists.Items[0].Value = value;
    //                ItemLists.Items[0].TimestampSpecified = false;
 
    //                RequestOptions opt = new RequestOptions();
 
    //                ReplyItemList ItemValues = new ReplyItemList();
    //                OPCError[] Errors;
    //                opt.ReturnItemTime = true;
    //                ReplyBase replay = srv.Write(opt, ItemLists, true, out ItemValues, out Errors);
 
    //                return !serverState.failed.Equals(replay.ServerState) && !serverState.commFault.Equals(replay.ServerState);
 
    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", "OPCHelper", "OPCWritePoint", ex.Message);
    //            return false;
    //        }
    //    }
 
    //    /// <summary>
    //    /// 写入准入信息
    //    /// </summary>
    //    /// <param name="ssj_Number"></param>
    //    /// <returns></returns>
    //    public static bool SSJLicences(int ssj_Number)
    //    {
    //        string LOCALSERVER = "S7:[S7 connection_1]";
    //        string StackerWrite = "DB540,";
    //        string WRITE_SSJ_1_Allow = LOCALSERVER + StackerWrite + "B17";//1号输送机入库许可
    //        string WRITE_SSJ_2_Allow = LOCALSERVER + StackerWrite + "B18";//2号输送机入库许可
    //        string WRITE_SSJ_3_Allow = LOCALSERVER + StackerWrite + "B19";//3号输送机入库许可
 
    //        if (ssj_Number == 1)
    //        {
    //            return OPCWritePoint(WRITE_SSJ_1_Allow, 1);
    //        }
    //        else if (ssj_Number == 2)
    //        {
    //            return OPCWritePoint(WRITE_SSJ_2_Allow, 1);
    //        }
    //        else
    //        {
    //            return OPCWritePoint(WRITE_SSJ_3_Allow, 1);
    //        }
    //    }
 
    //    /// <summary>
    //    /// 集合值写入OPC服务器
    //    /// </summary>
    //    /// <param name="ItemName">地址块集合</param>
    //    /// <param name="values">地址块对应的值</param>
    //    /// <returns>是否写入成功</returns>
    //    public static bool OPCWriteSerial(List<string> ItemNames, List<object> values)
    //    {
    //        try
    //        {
    //            using (var srv = new OPCXML_DataAccess())
    //            {
    //                int count = ItemNames.Count;
    //                WriteRequestItemList ItemLists = new WriteRequestItemList();
    //                ItemLists.Items = new ItemValue[count];
    //                for (int i = 0; i < count; i++)
    //                {
    //                    ItemLists.Items[i] = new ItemValue();
    //                    ItemLists.Items[i].ItemPath = "";
    //                    ItemLists.Items[i].ItemName = ItemNames[i];
    //                    ItemLists.Items[i].Value = values[i];
    //                    ItemLists.Items[i].TimestampSpecified = false;
    //                }
    //                RequestOptions opt = new RequestOptions();
    //                ReplyItemList ItemValues = new ReplyItemList();
    //                OPCError[] Errors;
    //                opt.ReturnItemTime = true;
    //                ReplyBase replay = srv.Write(opt, ItemLists, true, out ItemValues, out Errors);
 
    //                return !serverState.failed.Equals(replay.ServerState) && !serverState.commFault.Equals(replay.ServerState);
    //            }
    //        }
    //        catch (Exception ex)
    //        {
    //            LogTextHelper.WriteLine(Resources.LogDir, "在类{0}中执行方法{1}时出现异常:{2}", "OPCHelper", "OPCWriteSerial", ex.Message);
    //            return false;
    //        }
    //    }
    }
}