305 lines
11 KiB
C#
305 lines
11 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.IO.Ports;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
using AntdUI;
|
|||
|
using DH.Commons.Base;
|
|||
|
using DH.Commons.Enums;
|
|||
|
using DH.Devices.PLC;
|
|||
|
using XKRS.CanFly;
|
|||
|
|
|||
|
namespace DHSoftware.Views
|
|||
|
{
|
|||
|
public partial class MotionControl : UserControl
|
|||
|
{
|
|||
|
Window window;
|
|||
|
PLCBase pLCBase;
|
|||
|
public MotionControl(Window _window,PLCBase _pLCBase)
|
|||
|
{
|
|||
|
window= _window;
|
|||
|
pLCBase= _pLCBase;
|
|||
|
InitializeComponent();
|
|||
|
BindEventHandler();
|
|||
|
InitData();
|
|||
|
SetupDataBindings();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private void BindEventHandler()
|
|||
|
{
|
|||
|
sltTpye.TextChanged += SltTpye_TextChanged;
|
|||
|
btnAdd.Click += BtnAdd_Click;
|
|||
|
btnDelete.Click += BtnDelete_Click;
|
|||
|
PLCItemsTable.CellButtonClick += PLCItemsTable_CellButtonClick; ;
|
|||
|
}
|
|||
|
|
|||
|
private void PLCItemsTable_CellButtonClick(object sender, TableButtonEventArgs e)
|
|||
|
{
|
|||
|
var buttontext = e.Btn.Text;
|
|||
|
|
|||
|
if (e.Record is PLCItem pLCItem)
|
|||
|
{
|
|||
|
switch (buttontext)
|
|||
|
{
|
|||
|
//暂不支持进入整行编辑,只支持指定单元格编辑,推荐使用弹窗或抽屉编辑整行数据
|
|||
|
case "编辑":
|
|||
|
var form = new MotionEdit(window, pLCItem) { Size = new Size(500, 300) };
|
|||
|
AntdUI.Drawer.open(new AntdUI.Drawer.Config(window, form)
|
|||
|
{
|
|||
|
OnLoad = () =>
|
|||
|
{
|
|||
|
AntdUI.Message.info(window, "进入编辑", autoClose: 1);
|
|||
|
},
|
|||
|
OnClose = () =>
|
|||
|
{
|
|||
|
pLCItem.CellTags = new CellTag[] { };
|
|||
|
var tempList = pLCItem.CellTags.ToList();
|
|||
|
|
|||
|
if (pLCItem.StartExecute)
|
|||
|
{
|
|||
|
var newTag = new CellTag($"ON_{pLCItem.StartIndex}", TTypeMini.Primary);
|
|||
|
tempList.Add(newTag);
|
|||
|
}
|
|||
|
if (pLCItem.EndExecute)
|
|||
|
{
|
|||
|
var newTag = new CellTag($"OFF_{pLCItem.EndIndex}", TTypeMini.Warn);
|
|||
|
tempList.Add(newTag);
|
|||
|
}
|
|||
|
|
|||
|
// 重新赋值回数组
|
|||
|
pLCItem.CellTags = tempList.ToArray();
|
|||
|
AntdUI.Message.info(window, "结束编辑", autoClose: 1);
|
|||
|
}
|
|||
|
});
|
|||
|
break;
|
|||
|
case "删除":
|
|||
|
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
|
|||
|
if (result == DialogResult.OK)
|
|||
|
{
|
|||
|
pLCBase.PLCItemList.Remove(pLCItem);
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void BtnDelete_Click(object? sender, EventArgs e)
|
|||
|
{
|
|||
|
if (pLCBase.PLCItemList.Count == 0 || !pLCBase.PLCItemList.Any(x => x.Selected))
|
|||
|
{
|
|||
|
AntdUI.Message.warn(window, "请选择要删除的行!", autoClose: 3);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
var result = Modal.open(window, "删除警告!", "确认要删除选择的数据吗?", TType.Warn);
|
|||
|
if (result == DialogResult.OK)
|
|||
|
{
|
|||
|
// 使用反转for循环删除主列表中选中的项
|
|||
|
for (int i = pLCBase.PLCItemList.Count - 1; i >= 0; i--)
|
|||
|
{
|
|||
|
// 删除选中的主列表项
|
|||
|
if (pLCBase.PLCItemList[i].Selected)
|
|||
|
{
|
|||
|
pLCBase.PLCItemList.RemoveAt(i);
|
|||
|
}
|
|||
|
}
|
|||
|
// 提示删除完成
|
|||
|
AntdUI.Message.success(window, "删除成功!", autoClose: 3);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void BtnAdd_Click(object? sender, EventArgs e)
|
|||
|
{
|
|||
|
PLCItem pLCItem = new PLCItem()
|
|||
|
{
|
|||
|
CellLinks = new CellLink[]
|
|||
|
{
|
|||
|
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary),
|
|||
|
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
var form = new MotionEdit(window, pLCItem) { Size = new Size(450, 550) };
|
|||
|
AntdUI.Modal.open(new AntdUI.Modal.Config(window, "", form, TType.None)
|
|||
|
{
|
|||
|
BtnHeight = 0,
|
|||
|
});
|
|||
|
|
|||
|
if (form.submit)
|
|||
|
{
|
|||
|
pLCItem.CellTags = new CellTag[] { };
|
|||
|
var tempList = pLCItem.CellTags.ToList();
|
|||
|
|
|||
|
if (pLCItem.StartExecute)
|
|||
|
{
|
|||
|
var newTag = new CellTag($"ON_{pLCItem.StartIndex}", TTypeMini.Primary);
|
|||
|
tempList.Add(newTag);
|
|||
|
}
|
|||
|
if (pLCItem.EndExecute)
|
|||
|
{
|
|||
|
var newTag = new CellTag($"OFF_{pLCItem.EndIndex}", TTypeMini.Warn);
|
|||
|
tempList.Add(newTag);
|
|||
|
}
|
|||
|
|
|||
|
// 重新赋值回数组
|
|||
|
pLCItem.CellTags = tempList.ToArray();
|
|||
|
|
|||
|
pLCBase.PLCItemList.Add(pLCItem);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void SltTpye_TextChanged(object? sender, EventArgs e)
|
|||
|
{
|
|||
|
if (sender is Select slt && !string.IsNullOrEmpty(slt.Text))
|
|||
|
{
|
|||
|
// 将文本转换为枚举值
|
|||
|
if (Enum.TryParse<EnumPLCType>(slt.Text, out var plcType))
|
|||
|
{
|
|||
|
switch (plcType)
|
|||
|
{
|
|||
|
case EnumPLCType.信捷XC网口:
|
|||
|
case EnumPLCType.信捷XD网口:
|
|||
|
stpCom.Visible = false;
|
|||
|
stpBaud.Visible = false;
|
|||
|
stpData.Visible = false;
|
|||
|
stpParity.Visible = false;
|
|||
|
stpStop.Visible = false;
|
|||
|
stpIP.Visible = true;
|
|||
|
stpPort.Visible = true;
|
|||
|
break;
|
|||
|
|
|||
|
case EnumPLCType.信捷XD串口:
|
|||
|
case EnumPLCType.信捷XC串口:
|
|||
|
stpCom.Visible = true;
|
|||
|
stpBaud.Visible = true;
|
|||
|
stpData.Visible = true;
|
|||
|
stpParity.Visible = true;
|
|||
|
stpStop.Visible = true;
|
|||
|
stpIP.Visible = false;
|
|||
|
stpPort.Visible = false;
|
|||
|
break;
|
|||
|
|
|||
|
default:
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private void InitData()
|
|||
|
{
|
|||
|
// 获取枚举字段名列表(原描述改为字段名)
|
|||
|
sltTpye.Items.Clear();
|
|||
|
foreach (EnumPLCType value in Enum.GetValues(typeof(EnumPLCType)))
|
|||
|
{
|
|||
|
sltTpye.Items.Add(value.ToString());
|
|||
|
}
|
|||
|
|
|||
|
// 波特率选项(保持不变)
|
|||
|
cmbBaudRate.Items.AddRange(new object[] { 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400, 57600, 115200 });
|
|||
|
|
|||
|
// 数据位选项(保持不变)
|
|||
|
cmbDataBits.Items.AddRange(new object[] { 5, 6, 7, 8 });
|
|||
|
|
|||
|
// 停止位选项改为字段名
|
|||
|
var stopBitsNames = Enum.GetNames(typeof(StopBits));
|
|||
|
foreach (var name in stopBitsNames)
|
|||
|
{
|
|||
|
cmbStopBits.Items.Add(name);
|
|||
|
}
|
|||
|
|
|||
|
// 校验位选项改为字段名
|
|||
|
var parityNames = Enum.GetNames(typeof(Parity));
|
|||
|
foreach (var name in parityNames)
|
|||
|
{
|
|||
|
cmbParity.Items.Add(name);
|
|||
|
}
|
|||
|
|
|||
|
// COM端口列表(保持不变)
|
|||
|
List<string> comList = SerialPort.GetPortNames().ToList();
|
|||
|
foreach (var item in comList)
|
|||
|
{
|
|||
|
cmbCom.Items.Add(item);
|
|||
|
}
|
|||
|
|
|||
|
PLCItemsTable.Columns = new ColumnCollection() {
|
|||
|
new ColumnCheck("Selected"){Fixed = true},
|
|||
|
new Column("Name", "名称", ColumnAlign.Center),
|
|||
|
new Column("Type", "类型",ColumnAlign.Center),
|
|||
|
new Column("Address", "地址",ColumnAlign.Center),
|
|||
|
new Column("Value", "值",ColumnAlign.Center),
|
|||
|
new Column("CellTags", "标签",ColumnAlign.Center),
|
|||
|
new Column("CellLinks", "操作", ColumnAlign.Center)
|
|||
|
};
|
|||
|
if (pLCBase.PLCItemList.Count > 0)
|
|||
|
{
|
|||
|
foreach( var item in pLCBase.PLCItemList)
|
|||
|
{
|
|||
|
item.CellTags = new CellTag[] { };
|
|||
|
var tempList = item.CellTags.ToList();
|
|||
|
|
|||
|
if (item.StartExecute)
|
|||
|
{
|
|||
|
var newTag = new CellTag($"ON_{item.StartIndex}", TTypeMini.Primary);
|
|||
|
tempList.Add(newTag);
|
|||
|
}
|
|||
|
if (item.EndExecute)
|
|||
|
{
|
|||
|
var newTag = new CellTag($"OFF_{item.EndIndex}", TTypeMini.Warn);
|
|||
|
tempList.Add(newTag);
|
|||
|
}
|
|||
|
|
|||
|
// 重新赋值回数组
|
|||
|
item.CellTags = tempList.ToArray();
|
|||
|
|
|||
|
|
|||
|
|
|||
|
item.CellLinks = new CellLink[] {
|
|||
|
new CellButton(Guid.NewGuid().ToString(), "编辑", TTypeMini.Primary) ,
|
|||
|
new CellButton(Guid.NewGuid().ToString(), "删除", TTypeMini.Error)
|
|||
|
};
|
|||
|
}
|
|||
|
}
|
|||
|
PLCItemsTable.Binding(pLCBase.PLCItemList);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
private void SetupDataBindings()
|
|||
|
{
|
|||
|
// 添加双向类型转换
|
|||
|
sltTpye.DataBindings.Add("Text", pLCBase, "PLCType", true, DataSourceUpdateMode.OnPropertyChanged, "");
|
|||
|
cmbCom.DataBindings.Add(nameof(cmbCom.Text), pLCBase, nameof(pLCBase.COM));
|
|||
|
cmbBaudRate.DataBindings.Add(nameof(cmbBaudRate.Text), pLCBase, nameof(pLCBase.BaudRate));
|
|||
|
cmbDataBits.DataBindings.Add(nameof(cmbDataBits.Text), pLCBase, nameof(pLCBase.DataBit));
|
|||
|
cmbParity.DataBindings.Add(nameof(cmbParity.Text), pLCBase, nameof(pLCBase.Parity));
|
|||
|
cmbStopBits.DataBindings.Add(nameof(cmbStopBits.Text), pLCBase, nameof(pLCBase.StopBit));
|
|||
|
|
|||
|
iptIP.DataBindings.Add(nameof(iptIP.Text), pLCBase, nameof(pLCBase.IP));
|
|||
|
iptPort.DataBindings.Add(nameof(iptPort.Text), pLCBase, nameof(pLCBase.Port));
|
|||
|
|
|||
|
swhEnable.DataBindings.Add(nameof(swhEnable.Checked),pLCBase, nameof(pLCBase.Enable));
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
}
|