using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
|
namespace LB_VisionProcesses.Communicators.SiemensS7
|
{
|
public partial class ConfigForm : Form
|
{
|
private PlcConfig config;
|
private BindingSource bindingSource;
|
private string strPath = string.Empty;
|
public ConfigForm(PlcConfig existingConfig, string strPath)
|
{
|
InitializeComponent();
|
config = existingConfig;
|
SetupBindings();
|
dgvSignals.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
|
dgvSignals.ForeColor = Color.Black;
|
this.strPath = strPath;
|
//dgvSignals.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
|
}
|
|
private void SetupBindings()
|
{
|
// PLC 连接参数绑定
|
txtIpAddress.DataBindings.Add("Text", config, nameof(config.PlcIpAddress));
|
nudRack.DataBindings.Add("Value", config, nameof(config.Rack));
|
nudSlot.DataBindings.Add("Value", config, nameof(config.Slot));
|
|
// 手动配置 DataGridView 列(下拉框列)
|
dgvSignals.AutoGenerateColumns = false;
|
dgvSignals.Columns.Clear();
|
|
var colName = new DataGridViewTextBoxColumn();
|
colName.DataPropertyName = "SignalName";
|
colName.HeaderText = "信号名称";
|
colName.Width = 120;
|
dgvSignals.Columns.Add(colName);
|
|
var colArea = new DataGridViewComboBoxColumn();
|
colArea.DataPropertyName = "Area";
|
colArea.HeaderText = "存储区";
|
colArea.Items.AddRange("DB", "I", "Q", "M");
|
colArea.Width = 60;
|
dgvSignals.Columns.Add(colArea);
|
|
var colDbNum = new DataGridViewTextBoxColumn();
|
colDbNum.DataPropertyName = "DbNumber";
|
colDbNum.HeaderText = "DB号";
|
colDbNum.Width = 60;
|
dgvSignals.Columns.Add(colDbNum);
|
|
var colByte = new DataGridViewTextBoxColumn();
|
colByte.DataPropertyName = "ByteOffset";
|
colByte.HeaderText = "字节偏移";
|
colByte.Width = 80;
|
dgvSignals.Columns.Add(colByte);
|
|
var colBit = new DataGridViewTextBoxColumn();
|
colBit.DataPropertyName = "BitOffset";
|
colBit.HeaderText = "位偏移";
|
colBit.Width = 80;
|
dgvSignals.Columns.Add(colBit);
|
|
var colType = new DataGridViewComboBoxColumn();
|
colType.DataPropertyName = "DataType";
|
colType.HeaderText = "数据类型";
|
colType.Items.AddRange("Bool", "Byte", "Int", "DInt", "Real","String");
|
colType.Width = 80;
|
dgvSignals.Columns.Add(colType);
|
|
// 绑定数据源
|
bindingSource = new BindingSource();
|
bindingSource.DataSource = config.Signals;
|
dgvSignals.DataSource = bindingSource;
|
|
// 添加校验事件(可选)
|
dgvSignals.CellValueChanged += DgvSignals_CellValueChanged;
|
}
|
|
private void DgvSignals_CellValueChanged(object sender, DataGridViewCellEventArgs e)
|
{
|
if (e.RowIndex < 0) return;
|
var row = dgvSignals.Rows[e.RowIndex];
|
var signal = row.DataBoundItem as SignalConfig;
|
if (signal == null) return;
|
|
// 若 Area 不是 DB,自动将 DbNumber 置 0
|
if (!signal.Area.Equals("DB", StringComparison.OrdinalIgnoreCase))
|
{
|
signal.DbNumber = 0;
|
dgvSignals.InvalidateRow(e.RowIndex);
|
}
|
}
|
|
private void btnAdd_Click(object sender, EventArgs e)
|
{
|
config.Signals.Add(new SignalConfig());
|
bindingSource.ResetBindings(false);
|
}
|
|
private void btnDelete_Click(object sender, EventArgs e)
|
{
|
if (dgvSignals.CurrentRow != null)
|
{
|
var signal = dgvSignals.CurrentRow.DataBoundItem as SignalConfig;
|
if (signal != null)
|
{
|
config.Signals.Remove(signal);
|
bindingSource.ResetBindings(false);
|
}
|
}
|
}
|
|
private void btnSave_Click(object sender, EventArgs e)
|
{
|
S7ConfigService.SaveConfig(config, this.strPath);
|
this.DialogResult = DialogResult.OK;
|
this.Close();
|
}
|
|
private void btnCancel_Click(object sender, EventArgs e)
|
{
|
this.DialogResult = DialogResult.Cancel;
|
this.Close();
|
}
|
}
|
|
}
|