using System.ComponentModel; using System.IO.Ports; using System.Text.Json.Serialization; using AntdUI; using DH.Commons.Enums; // 请确保此命名空间包含EnumPLCType namespace DH.Commons.Base { public class PLCBase : NotifyProperty { // 私有字段 private bool _enable; private bool _connected; private string _plcName; private EnumPLCType _plcType; private string _com = "COM1"; private int _baudRate = 9600; private int _dataBit = 8; private StopBits _stopBit = StopBits.One; private Parity _parity = Parity.None; private string _ip = "192.168.6.61"; private int _port = 502; private AntList _PLCItemList = new AntList(); [Category("设备配置")] [DisplayName("是否启用")] [Description("是否启用")] public bool Enable { get => _enable; set { if (_enable == value) return; _enable = value; OnPropertyChanged(nameof(Enable)); } } [Category("状态监控")] [DisplayName("连接状态")] [Description("PLC连接状态")] public bool Connected { get => _connected; set { if (_connected == value) return; _connected = value; OnPropertyChanged(nameof(Connected)); } } [Category("设备配置")] [DisplayName("PLC名称")] [Description("PLC设备名称")] public string PLCName { get => _plcName; set { if (_plcName == value) return; _plcName = value; OnPropertyChanged(nameof(PLCName)); } } [Category("设备配置")] [DisplayName("PLC类型")] [Description("PLC通信协议类型")] public EnumPLCType PLCType { get => _plcType; set { if (_plcType == value) return; _plcType = value; OnPropertyChanged(nameof(PLCType)); } } [Category("串口配置")] [DisplayName("COM端口")] [Description("串口号,如COM1")] public string COM { get => _com; set { if (_com == value) return; _com = value; OnPropertyChanged(nameof(COM)); } } [Category("串口配置")] [DisplayName("波特率")] [Description("串口通信波特率")] public int BaudRate { get => _baudRate; set { if (_baudRate == value) return; _baudRate = value; OnPropertyChanged(nameof(BaudRate)); } } [Category("串口配置")] [DisplayName("数据位")] [Description("数据位长度(5/6/7/8)")] public int DataBit { get => _dataBit; set { if (_dataBit == value) return; _dataBit = value; OnPropertyChanged(nameof(DataBit)); } } [Category("串口配置")] [DisplayName("停止位")] [Description("停止位设置")] public StopBits StopBit { get => _stopBit; set { if (_stopBit == value) return; _stopBit = value; OnPropertyChanged(nameof(StopBit)); } } [Category("串口配置")] [DisplayName("校验位")] [Description("奇偶校验方式")] public Parity Parity { get => _parity; set { if (_parity == value) return; _parity = value; OnPropertyChanged(nameof(Parity)); } } [Category("网络配置")] [DisplayName("IP地址")] [Description("PLC网络地址")] public string IP { get => _ip; set { if (_ip == value) return; _ip = value; OnPropertyChanged(nameof(IP)); } } [Category("网络配置")] [DisplayName("端口号")] [Description("网络通信端口")] public int Port { get => _port; set { if (_port == value) return; _port = value; OnPropertyChanged(nameof(Port)); } } [Category("点位配置")] [DisplayName("点位配置")] [Description("点位配置")] public AntList PLCItemList { get => _PLCItemList; set { if (_PLCItemList == value) return; _PLCItemList = value; OnPropertyChanged(nameof(PLCItemList)); } } public virtual bool PLCConnect() { Connected = true; return true; } public virtual bool PLCDisConnect() { Connected = false; return true; } public virtual ushort ReadShort(string address) { return 0; } public virtual int ReadInt(string address) { return 0; } public virtual float ReadFloat(string address) { return 0; } public virtual bool ReadBool(string address) { return false; } public virtual bool WriteShort(string address, short value, bool waitForReply = true) { return false; } public virtual bool WriteInt(string address, int value, bool waitForReply = true) { return false; } public virtual bool WriteDInt(string address, int value, bool waitForReply = true) { return false; } public virtual bool WriteFloat(string address, float value, bool waitForReply = true) { return false; } public virtual bool WriteBool(string address, bool value, bool waitForReply = true) { return false; } } public class PLCItem : NotifyProperty { private bool _selected; private string _name = string.Empty; private string _type = string.Empty; private string _value = string.Empty; private bool _startexecute; private bool _endexecute; private int _startindex; private int _endindex; private string _numtype; private string _address; /// /// 是否选中 /// public bool Selected { get => _selected; set { if (_selected != value) { _selected = value; OnPropertyChanged(nameof(Selected)); } } } /// /// 参数名称 /// public string Name { get => _name; set { if (_name != value) { _name = value; OnPropertyChanged(nameof(Name)); } } } public string Type { get => _type; set { if (_type != value) { _type = value; OnPropertyChanged(nameof(Type)); } } } /// /// 参数类型 /// public string Address { get => _address; set { if (_address != value) { _address = value; OnPropertyChanged(nameof(Address)); } } } public string NumTpye { get => _numtype; set { if (_numtype != value) { _numtype = value; OnPropertyChanged(nameof(NumTpye)); } } } /// /// 参数值 /// public string Value { get => _value; set { if (_value != value) { _value = value; OnPropertyChanged(nameof(Value)); } } } /// /// 流程开启执行状态 /// public bool StartExecute { get => _startexecute; set { if (_startexecute != value) { _startexecute = value; OnPropertyChanged(nameof(StartExecute)); } } } /// /// 流程结束执行状态 /// public bool EndExecute { get => _endexecute; set { if (_endexecute != value) { _endexecute = value; OnPropertyChanged(nameof(EndExecute)); } } } /// /// 流程开启顺序 /// public int StartIndex { get => _startindex; set { if (_startindex != value) { _startindex = value; OnPropertyChanged(nameof(StartIndex)); } } } /// /// 流程结束顺序 /// public int EndIndex { get => _endindex; set { if (_endindex != value) { _endindex = value; OnPropertyChanged(nameof(EndIndex)); } } } private CellLink[] cellLinks; [JsonIgnore] public CellLink[] CellLinks { get { return cellLinks; } set { if (cellLinks == value) return; cellLinks = value; OnPropertyChanged(nameof(CellLinks)); } } private CellTag[] cellTags; [JsonIgnore] public CellTag[] CellTags { get { return cellTags; } set { if (cellTags == value) return; cellTags = value; OnPropertyChanged(nameof(CellTags)); } } } }