|

楼主 |
发表于 2023-2-9 08:33:22
|
显示全部楼层
using System;
using System.Windows.Media;
using System.ComponentModel;
namespace WPF_Controlador
{
public class PtcOpc : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public PtcOpc(string groupId, string channelName)
{
GroupId = groupId;
ChannelName = channelName;
switch(groupId)
{
case "01":
GroupName = "主试验台";
break;
case "02":
GroupName = "副试验台";
break;
case "03":
GroupName = "燃油供油系统";
break;
case "04":
GroupName = "介质高低温系统";
break;
case "05":
GroupName = "环境高低温系统";
break;
case "06":
GroupName = "液压泵站";
break;
}
IsShown = true;
MediaColor = Colors.Blue;
RecordIsNeeded = true;
DefaultValue = "";
}
public string GroupId { get; set; }
public string GroupName { get; set; }
public string ChannelName { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string Description { get; set; }
public EnumOpcType DataType { get; set; }
public EnumOpcAccess Access { get; set; }
private string _RawValue;
public string RawValue
{
get { return _RawValue; }
set
{
_RawValue = value;
Notify("RawValue");
switch (DataType)
{
case EnumOpcType.Bool:
if (RawValue == "True")
{
BoolValue = true;
}
else
{
BoolValue = false;
}
break;
case EnumOpcType.Float:
FloatValue = Convert.ToSingle(RawValue);
break;
case EnumOpcType.Int:
IntValue = Convert.ToInt32(RawValue);
break;
case EnumOpcType.Byte:
ByteValue = Convert.ToByte(RawValue);
break;
}
}
}
private bool _BoolValue;
public bool BoolValue
{
get { return _BoolValue; }
set
{
_BoolValue = value;
Notify("BoolValue");
}
}
private float _FloatValue;
public float FloatValue
{
get { return _FloatValue; }
set
{
_FloatValue = value;
Notify("FloatValue");
}
}
private int _IntValue;
public int IntValue
{
get { return _IntValue; }
set
{
_IntValue = value;
Notify("IntValue");
}
}
private byte _ByteValue;
public byte ByteValue
{
get { return _ByteValue; }
set
{
_ByteValue = value;
Notify("ByteValue");
}
}
public string Url
{
get
{
return @"opc://localhost/National Instruments.NIOPCServers.V5/" + ChannelName + ".Device1." + GroupId + "." + Name;
}
}
//------------------------------------------------------------------------------------
private Color _MediaColor;
public Color MediaColor
{
get { return _MediaColor; }
set
{
_MediaColor = value;
Notify("MediaColor");
}
}
private bool _IsShown;
public bool IsShown
{
get { return _IsShown; }
set
{
_IsShown = value;
Notify("IsShown");
}
}
//------------------------------------------------------------------------------------
public int IndexNo { get; set; }
private bool _RecordIsNeeded;
public bool RecordIsNeeded
{
get { return _RecordIsNeeded; }
set
{
_RecordIsNeeded = value;
Notify("RecordIsNeeded");
}
}
private string _DefaultValue;
public string DefaultValue
{
get { return _DefaultValue; }
set
{
_DefaultValue = value;
Notify("DefaultValue");
}
}
public Brush Stroke
{
get
{
var c = new BrushConverter();
return (SolidColorBrush)c.ConvertFromString(_MediaColor.ToString());
}
}
}
}
以上是项目中完整的 PtcOpc 类定义代码,颜色等属性,是为 WPF 界面控件绑定服务的。还有 event ,用户程序可以注册该事件,OPC 变量发生变化时,用户可收到通知。有不清楚的,随时给我信息。谢谢! |
|