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)
|
{
|
;
|
}
|
|
|
}
|
|
private void OPC_OnReadItemValueChanged(OPC.Item item, int itemValue)
|
{
|
var x = (from i in _ioBindlingList
|
where i.OPCName == item.name
|
select i).FirstOrDefault();
|
if (x != default(PLCIO))
|
{
|
x.PLCValue = itemValue == 1;
|
dgvPLCIO.Invalidate();
|
}
|
}
|
|
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;
|
_opc.OnReadItemValueChanged += OPC_OnReadItemValueChanged;
|
}
|
|
private void FormPLCIO_FormClosing(object sender, FormClosingEventArgs e)
|
{
|
_opc.OnReadItemValueChanged -= OPC_OnReadItemValueChanged;
|
//_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;
|
}
|
}
|
}
|