patrick.xu
2021-06-01 aefe9f2572eac7c61f6d2952593ec18a700dfcf0
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
 
 
 
namespace M423project
{
    public partial class FormPLCIO : Form
    {
        private OPC _opc;
        private List<PLCIO> _ioList = new List<PLCIO>();
 
        private BindingList<PLCIO> _ioBindlingList;
 
        #region PLCIO Defination
 
        private PLCIO[] PLCIOArray =
            {   new PLCIO( "M423.Box.Output.T_2_00", "CIO0002.00", "1#工位电池检测"),
                new PLCIO( "M423.Box.Output.T_2_01", "CIO0002.01", "5#工位电池检测"),
                new PLCIO( "M423.Box.Input.T_2_02", "CIO0002.02", "转盘到位"),
                new PLCIO( "M423.Box.Output.T_2_03", "CIO0002.03", "上料盘1接近开关"),
                new PLCIO( "M423.Box.Output.T_2_04", "CIO0002.04", "上料盘2接近开关"),
                new PLCIO( "M423.Box.Output.T_2_05", "CIO0002.05", "NG料盘1接近开关"),
                new PLCIO( "M423.Box.Output.T_2_06", "CIO0002.06", "NG料盘2接近开关"),
                new PLCIO( "M423.Box.Output.T_2_07", "CIO0002.07", "下料盘1接近开关"),
                new PLCIO( "M423.Box.Output.T_2_08", "CIO0002.08", "下料盘2接近开关"),
                new PLCIO( "M423.Box.Output.T_2_09", "CIO0002.09", "安全光栅信号"),
                new PLCIO( "M423.Box.Output.T_3_06", "CIO0003.06", "上料盘气缸上限"),
                new PLCIO( "M423.Box.Output.T_3_07", "CIO0003.07", "上料盘气缸下限"),
                new PLCIO( "M423.Box.Output.T_3_08", "CIO0003.08", "NG料盘气缸上限"),
                new PLCIO( "M423.Box.Output.T_3_09", "CIO0003.09", "NG料盘气缸下限"),
                new PLCIO( "M423.Box.Output.T_3_10", "CIO0003.10", "下料盘气缸上限"),
                new PLCIO( "M423.Box.Output.T_3_11", "CIO0003.11", "下料盘气缸下限"),
                new PLCIO( "M423.Box.Output.T_4_03", "CIO0004.03", "安全门信号"),
                new PLCIO( "M423.Box.Output.T_5_00", "CIO0005.00", "X1轴报警"),
                new PLCIO( "M423.Box.Output.T_5_01", "CIO0005.01", "Y1轴报警"),
                new PLCIO( "M423.Box.Output.T_5_02", "CIO0005.02", "X2轴报警"),
                new PLCIO( "M423.Box.Output.T_5_03", "CIO0005.03", "Y2轴报警"),
                new PLCIO( "M423.Box.Output.T_5_10", "CIO0005.10", "内侧光栅"),
                new PLCIO( "M423.Box.Output.T_101_00", "CIO0101.00", "X1-SON"),
                new PLCIO( "M423.Box.Output.T_101_01", "CIO0101.01", "Y1-SON"),
                new PLCIO( "M423.Box.Output.T_101_02", "CIO0101.02", "X2-SON"),
                new PLCIO( "M423.Box.Output.T_101_03", "CIO0101.03", "Y2-SON"),
                new PLCIO( "M423.Box.Output.T_101_05", "CIO0101.05", "下料盘退出"),
                new PLCIO( "M423.Box.Output.T_102_05", "CIO0102.05", "上料盘气缸进"),
                new PLCIO( "M423.Box.Output.T_102_06", "CIO0102.06", "NG料盘气缸进"),
                new PLCIO( "M423.Box.Output.T_102_07", "CIO0102.07", "下料盘气缸进"),
                new PLCIO( "M423.Box.Output.T_104_07", "CIO0104.07", "上料盘气缸退"),
                new PLCIO( "M423.Box.Output.T_106_04", "CIO0106.04", "NG盘气缸退") };
 
 
        #endregion
 
 
        public FormPLCIO(OPC opc)
        {
            InitializeComponent();
            _opc = opc;
            _ioList.AddRange(PLCIOArray);
 
        }
 
        private void FetchPLCIOValue()
        {
            object o = new object();
            try
            {
                foreach (var x in _ioList)
                {
                    x.PLCValue = _opc.ReadOneItem_Wrapper(x.OPCName, ref o) ? (bool)o : false;
                    Thread.Sleep(20);
                }
            }
            catch (Exception)
            {
                ;
            }
 
 
        }
 
 
        public void OPCEventHandler(object sender, EventTool.ListenEventArgs e)
        {
            OPC.Item item = (OPC.Item)e.IData.Data;
            var x = (from i in _ioBindlingList
                     where i.OPCName == item.name
                     select i).FirstOrDefault();
            if (x != default(PLCIO))
            {
                x.PLCValue = (bool)item.value;
                dgvPLCIO.Invalidate();
            }
        }
 
        private void FormPLCIO_Load(object sender, EventArgs e)
        {
            FetchPLCIOValue();
            //_ioBindlingList = new BindingList<PLCIO>(_ioList);
            //dgvPLCIO.DataSource = _ioBindlingList;
            //dgvPLCIO.Invalidate();
 
            for (int i = 0; i < _ioList.Count; i++)
            {
                dgvPLCIO.Rows.Add();
                dgvPLCIO.Rows[i].Cells["PLCAddr"].Value = _ioList[i].PLCAddr;
                dgvPLCIO.Rows[i].Cells["OPCName"].Value = _ioList[i].OPCName;
                dgvPLCIO.Rows[i].Cells["OPCDesc"].Value = _ioList[i].OPCDesc;
                dgvPLCIO.Rows[i].Cells["PLCValue"].Style.BackColor = _ioList[i].PLCValue ? Color.Green : Color.White;
            }
 
            _opc.listenEventCreater.ListenEvent += this.OPCEventHandler;
        }
 
        private void FormPLCIO_FormClosing(object sender, FormClosingEventArgs e)
        {
 
            _opc.listenEventCreater.ListenEvent -= this.OPCEventHandler;
 
        }
 
        private void btnWriteValue_Click(object sender, EventArgs e)
        {
            if (dgvPLCIO.CurrentRow != null)
            {
                PLCIO io = _ioList[dgvPLCIO.CurrentRow.Index];
                try
                {
                    _opc.Write(io.OPCName, io.PLCValue);
                }
                catch (Exception)
                {
                    ;
                }
            }
        }
 
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }
 
        private void cbPLCValue_Click(object sender, EventArgs e)
        {
            //if (dgvPLCIO.CurrentRow != null)
            //{
            //    _ioBindlingList[dgvPLCIO.CurrentRow.Index].PLCValue = cbPLCValue.Checked;
            //    dgvPLCIO.Invalidate();
            //}
        }
 
        private void dgvPLCIO_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvPLCIO.CurrentRow.Index > 0)
            {
                tbPLCAddr.Text = dgvPLCIO.CurrentRow.Cells["PLCAddr"].Value.ToString();
                tbOPCName.Text = dgvPLCIO.CurrentRow.Cells["OPCName"].Value.ToString();
                tbOPCDesc.Text = dgvPLCIO.CurrentRow.Cells["OPCDesc"].Value.ToString();
            }
        }
    }
 
    public class PLCIO
    {
        public string OPCName { get; set; }
        public string PLCAddr { get; set; }
        public string OPCDesc { get; set; }
        public bool PLCValue { get; set; }
        public PLCIO(string opcName, string plcAddr, string opcDesc)
        {
            OPCName = opcName;
            PLCAddr = plcAddr;
            OPCDesc = opcDesc;
            PLCValue = false;
        }
    }
}