using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hanyu.Basic;
using OPCAutomation;
using System.ComponentModel;
namespace Hanyu.Control
{
public class HOPCButton2:Button
{
[Browsable(true), DefaultValue(typeof(Color), "Color.GreenYellow"), Description("开启状态颜色")]
public Color OPCno { get; set; }
[Browsable(true), DefaultValue(typeof(Color), "Color.Transparent"), Description("关闭状态颜色")]
public Color OPCnc{ get; set; }
[Browsable(true), DefaultValue(-1), Description("OPC模式,0 禁用,1 监听,2 点动,3 保持 ")]
public int OPCmode { get; set; }
[Browsable(true), DefaultValue(-1), Description("OPC类型 0 限位系列,2 开系列 ,3 关系列")]
public int OPCtype { get; set; }
[Browsable(true), DefaultValue(0), Description("OPC点位")]
public int OPCpoint { get; set; }
[Browsable(true), DefaultValue(0), Description("OPC状态")]
public int OPCstats { get; set; }
[Browsable(true), DefaultValue(0), Description("OPC按钮状态")]
private bool OPCstat { get; set; }
/// <summary>
/// 单位
/// </summary>
public string OPCunit { get; set; }
[Browsable(true), DefaultValue(0), Description("OPC 点位名称")]
public string OPCName { get; set; }
/// <summary>
/// 事件说明
/// </summary>
public string[] OPCActionCaption { get; set; }
/// <summary>
/// OPC 程序
/// </summary>
public HOPCclient opc;
[Browsable(true), DefaultValue(0), Description("OPC事件")]
public event EventHandler Action;
public string OPCmessage;
public int OPCactionCode = 0;
public HOPCButton2()
{
OPCnc = Color.Transparent;
OPCno = Color.GreenYellow;
this.OPCstats = 0;
Click += new EventHandler(OnClick);
MouseDown += new System.Windows.Forms.MouseEventHandler(OnMouseDown);
MouseUp += new System.Windows.Forms.MouseEventHandler(OnMouseUp);
}
public void Open_OPC(HOPCclient _opc){opc = _opc;}
private void OnClick(object sender, EventArgs e)
{
HOPCButton2 _obj = sender as HOPCButton2;
_obj.OPCstat = getByBit(_obj.OPCstats, _obj.OPCtype);
if (3 == _obj.OPCmode && opc != null)
{
OPCmessage = "人工点击触发"; OPCactionCode = 1;
opc.Items.Item(_obj.OPCpoint).Write(SetBitValue(_obj.OPCstats, _obj.OPCtype, !_obj.OPCstat));
if (Action != null) Action(this, null);
}
}
private void OnMouseDown(object sender, MouseEventArgs e)
{
HOPCButton2 _obj = sender as HOPCButton2;
if (2 == _obj.OPCmode && opc != null)
{
OPCmessage = "人工点击触发"; OPCactionCode = 1;
int value = _obj.OPCstats;
value =SetBitValue(_obj.OPCstats,_obj.OPCtype,true);
opc.Items.Item(_obj.OPCpoint).Write(value);
if (Action != null) Action(this, null);
}
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
HOPCButton2 _obj = sender as HOPCButton2;
if (2 == _obj.OPCmode && opc != null)
{
OPCmessage = "人工点击触发"; OPCactionCode = 1;
if (Action != null) Action(this,null);
opc.Items.Item(_obj.OPCpoint).Write(SetBitValue(_obj.OPCstats, _obj.OPCtype,false));
}
}
public void setBackColors(Color color)
{
this.Invoke((EventHandler)(delegate {this.BackColor = color; }));
}
public void setTexts(string text)
{
this.Invoke((EventHandler)(delegate { this.Text = text; }));
}
/// <summary>
/// 按位取值
/// </summary>
/// <param name="data">要取值的数据Byte类型</param>
/// <param name="index">要取第几位( 0-7 位 如果大于7或小于0返回False)</param>
/// <returns>等于1 返回True等于0返回False</returns>
public bool getByBit(int data, int index)
{
if (index > 7 || index < 0) { return false; }
return ((data >> index) & 1) == 1;
}
/// <summary>
/// 设定Int数据中某一位的值
/// </summary>
/// <param name="value">位设定前的值</param>
/// <param name="index">32位数据的从右向左的偏移位索引(0~31)</param>
/// <param name="bitValue">true设该位为1,false设为0</param>
/// <returns>返回位设定后的值</returns>
public int SetBitValue(int value, int index, bool bitValue)
{
if (index > 7 || index < 0) { return value; }
var val = 1 << index;
return bitValue ? (value | val) : (value & ~val);
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Hanyu.Basic;
using OPCAutomation;
namespace Hanyu.Control
{
public partial class HOPCLED2 : UserControl
{
public event EventHandler Action;
public HOPCLED2()
{
InitializeComponent();
}
public void Open_OPC(string[] _item, HOPCclient _opc, int mode=0)
{
this.Button.Margin = new System.Windows.Forms.Padding(0);
this.Button.Text = null;
this.Button.OPCName =this.label1.Text = _item[0];
this.Button.OPCmode =Convert.ToInt16(_item[1]);
this.Button.OPCtype = Convert.ToInt16(_item[2]);
this.Button.OPCpoint = Convert.ToInt16(_item[3]);
this.Button.OPCunit = _item[9];
this.Button.OPCActionCaption = new string[10];
for (int i = 0; i < 10; i++)
{
this.Button.OPCActionCaption[i] = _item[10 + i];
}
if (this.Button.OPCmode >= 1)
{
_opc.Group.DataChange -= new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
_opc.Group.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
this.Button.Open_OPC(_opc);
}
this.Visible=(this.Button.OPCmode>=1) ?true :false;
this.Button.Action -= OPC_OnAction;
this.Button.Action += OPC_OnAction;
}
private void OPC_OnAction(object sender, EventArgs e)
{
try
{
string str = "";
if (Button.OPCactionCode == 1)
{
Button.OPCactionCode = 0;
if (Action != null) Action(Button.OPCName + "-" + Button.OPCmessage, null);
Button.OPCmessage = "";
return;
}
switch (Button.OPCtype)
{
case 0:
if (Button.getByBit(Button.OPCstats, 0))
{
if (Button.OPCActionCaption[6] == "") break;
str = Button.OPCName + "-" + Button.OPCActionCaption[6];
setTexts(str);
if (Action != null) Action(str,null);
}
else
{
if (Button.OPCActionCaption[7] == "") break;
str = Button.OPCName + "-" + Button.OPCActionCaption[7];
setTexts(Button.OPCName);
if (Action != null) Action(str, null);
};
break;
case 2:
onOPCLEDevent(0);
break;
case 3:
break;
default:
break;
}
}
catch (Exception ex)
{
}
}
public void onOPCLEDevent( int offset)
{
string str = "";
string tmp = "";
for (int i = offset; i <=2+offset; i+=2)
{
if (Button.getByBit(Button.OPCstats, i))
{
if (Button.OPCActionCaption[i] == "") break;
str = Button.OPCName + "-" + Button.OPCActionCaption[i];
//setTexts(str);
if (Action != null) Action(str,null);
}
else
{
if (Button.OPCActionCaption[i+1] == "") break;
str = Button.OPCName + "-" + Button.OPCActionCaption[i+1];
//setTexts(Button.OPCName);
if (Action != null) Action(str, null);
};
}
for (int i = offset+4; i <= 4 + offset; i += 2)
{
if (Button.getByBit(Button.OPCstats, i))
{
if (Button.OPCActionCaption[i] == "") break;
str = Button.OPCName + Button.OPCActionCaption[i];
setTexts(str);
tmp = label1.Text;
Button.setBackColors(Button.OPCno);
if (Action != null) Action(str, null);
}
else
{
if (Button.OPCActionCaption[i+1] == "") break;
str = Button.OPCName + Button.OPCActionCaption[i + 1];
setTexts(Button.OPCName);
tmp = label1.Text;
Button.setBackColors(Button.OPCnc);
if (Action != null) Action(str, null);
};
}
for (int i = offset + 6; i <= 6 + offset; i += 2)
{
if (Button.getByBit(Button.OPCstats, i))
{
if (Button.OPCActionCaption[i] == "") break;
str = label1.Text + "-" + Button.OPCActionCaption[i];
setTexts(str);
if (Action != null) Action(str, null);
}
else
{
if (Button.OPCActionCaption[i + 1] == "") break;
str = tmp + "-" + Button.OPCActionCaption[i + 1];
setTexts(tmp);
if (Action != null) Action(str, null);
};
}
}
public void setTexts(string content)
{
this.Invoke((EventHandler)(delegate{this.label1.Text=content;}));
}
private void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
{
try
{
if (this.Button.OPCmode >= 1)
{
int index;
for (int i = 1; i <= NumItems; i++)
{
index = (int)ClientHandles.GetValue(i);
if (index == this.Button.OPCpoint)
{
this.Button.OPCstats = Convert.ToInt32 (ItemValues.GetValue(i));
this.Button.OPCmessage = ""; this.Button.OPCactionCode = 0;
// if (Action != null) Action(this, null);
OPC_OnAction(this, null);
}
}//endfor
}
}
catch(Exception ex) {
}
}
}
}
public static string[] ReadCsv(string fileName )
{
List<string>data = new List<string>();
try
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("GB2312")))
{
string str = "";
while ((str = sr.ReadLine()) != null) { data.Add(str); }
}
}
return data.ToArray();
}
catch (Exception ex)
{
return null;
}
}