| | |
| | | using LB_SmartVisionCommon; |
| | | using LB_VisionControls; |
| | | using LB_VisionProcesses.Cameras; |
| | | using LB_VisionProcesses.Communicators; |
| | | using LB_VisionProcesses.Communicators.SiemensS7; |
| | | using OpenVinoSharp.Extensions.model; |
| | | using System; |
| | | using System.Collections.Concurrent; |
| | | using System.Collections.Generic; |
| | |
| | | private BindingSource _bindingSource; |
| | | |
| | | |
| | | private PlcConfig config; |
| | | private DataTable signalTable; |
| | | private BindingSource bindingSource; |
| | | private string Name; |
| | | |
| | | |
| | | |
| | | UserPictureBox onlinePictureBox { get; set; } |
| | | string communicatorsName { get; set; } |
| | | public MotionControlFormRead() |
| | |
| | | } |
| | | string cameraSN = string.Empty; |
| | | |
| | | public MotionControlFormRead(List<string> strCameraSN) |
| | | public MotionControlFormRead(string strCameraSN) |
| | | { |
| | | InitializeComponent(); |
| | | |
| | | if (strCameraSN == null || strCameraSN.Count == 0) |
| | | if (string.IsNullOrEmpty(strCameraSN)) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | //this.camera = camera; |
| | | //this.Text = camera.SN; |
| | | this.cmbSN.Items.Clear(); |
| | | foreach (string strSN in strCameraSN) |
| | | { |
| | | this.cmbSN.Items.Add(strSN); |
| | | } |
| | | //cameraSN = camera.SN; |
| | | this.cmbSN.Items.Add(strCameraSN); |
| | | onlinePictureBox = new UserPictureBox(uiGroupBoxImage); |
| | | this.uiGroupBoxImage.Controls.Clear(); |
| | | this.uiGroupBoxImage.Controls.Add(onlinePictureBox); |
| | | onlinePictureBox.Dock = DockStyle.Fill; |
| | | dataGridView1.Columns["SN"].HeaderText = "序列号"; |
| | | dataGridView1.Columns["SN"].ReadOnly = true; |
| | | dataGridView1.Columns["ReadAddress"].HeaderText = "读取PLC地址"; |
| | | //dataGridView1.Columns["ReadAddress"].ReadOnly = true; |
| | | dataGridView1.Columns["WriteAddress"].HeaderText = "写入PLC地址"; |
| | | //dataGridView1.Columns["WriteAddress"].ReadOnly = true; |
| | | dataGridView1.Columns["Value"].HeaderText = "数值"; |
| | | dataGridView1.Columns["Description"].HeaderText = "描述"; |
| | | dataGridView1.Columns["Value"].DefaultCellStyle.Format = "F2"; // 保留两位小数 |
| | | |
| | | |
| | | LoadConfigAndSetupGrid(); |
| | | } |
| | | Dictionary<string, RecordMotionControlData> concurrentDictionary = new Dictionary<string, RecordMotionControlData>(); |
| | | private void CameraForm_FormClosing(object sender, FormClosingEventArgs e) |
| | | { |
| | | if (GlobalVar.dicCameras[this.cmbSN.Text] == null) |
| | | { |
| | | return; |
| | | } |
| | | |
| | | GlobalVar.dicCameras[this.cmbSN.Text].StopGrabbing(); |
| | | |
| | | GlobalVar.dicCameras[this.cmbSN.Text].StartGrabbing(); |
| | | if (GlobalVar.dicMotionControlData.Keys.Contains(GlobalVar.strProductName)) |
| | | { |
| | | concurrentDictionary = GlobalVar.dicMotionControlData[GlobalVar.strProductName]; |
| | | double xAxisDetectLocationValue, xAxisOriginalLocationValue, yAxisDetectLocationValue, yAxisOriginalLocationValue, zAxisDetectLocationValue, zAxisOriginalLocationValue, wAxisDetectLocationValue, wAxisOriginalLocationValue, pulseMotionSpeedValue, angularVelocityValue; |
| | | //xAxisDetectLocationValue = double.Parse(this.textBoxXDetectLocation.Text); |
| | | //xAxisOriginalLocationValue = double.Parse(this.textBoxXOriginalLocation.Text); |
| | | //yAxisDetectLocationValue = double.Parse(this.textBoxYDetectLocation.Text); |
| | | //yAxisOriginalLocationValue = double.Parse(this.textBoxYOriginalLocation.Text); |
| | | //zAxisDetectLocationValue = double.Parse(this.textBoxZDetectLocation.Text); |
| | | //zAxisOriginalLocationValue = double.Parse(this.textBoxZOriginalLocation.Text); |
| | | //wAxisDetectLocationValue = double.Parse(this.textBoxWDetectLocation.Text); |
| | | //wAxisOriginalLocationValue = double.Parse(this.textBoxWOriginalLocation.Text); |
| | | //pulseMotionSpeedValue = double.Parse(this.textBoxPointSpeed.Text); |
| | | //angularVelocityValue = double.Parse(this.textBoxPointSpeed.Text); |
| | | if (concurrentDictionary.Keys.Contains(cameraSN)) |
| | | { |
| | | //RecordMotionControlData recordMotionControlData = new RecordMotionControlData |
| | | //{ |
| | | // ProductName = GlobalVar.strProductName, |
| | | // CommunicatorsName = communicatorsName, |
| | | // CameraSN = cameraSN, |
| | | // //XAxisAddress = this.textBoxXVarAddress.Text, |
| | | // //XAxisDetectLocationValue = xAxisDetectLocationValue, |
| | | // //XAxisOriginalLocationValue = xAxisOriginalLocationValue, |
| | | // //YAxisAddress = this.textBoxYVarAddress.Text, |
| | | // //YAxisDetectLocationValue = yAxisDetectLocationValue, |
| | | // //YAxisOriginalLocationValue = yAxisOriginalLocationValue, |
| | | // //ZAxisAddress = this.textBoxZVarAddress.Text, |
| | | // //ZAxisDetectLocationValue = zAxisDetectLocationValue, |
| | | // //ZAxisOriginalLocationValue = zAxisOriginalLocationValue, |
| | | // //WAxisAddress = this.textBoxWVarAddress.Text, |
| | | // //WAxisDetectLocationValue = wAxisDetectLocationValue, |
| | | // //WAxisOriginalLocationValue = wAxisOriginalLocationValue, |
| | | // //PulseMotionSpeedAddress = this.textBoxPulseMotionSpeedAddress.Text, |
| | | // //PulseMotionSpeedValue = pulseMotionSpeedValue, |
| | | // //AngularVelocityAddress = this.textBoxAngularVelocityAddress.Text, |
| | | // //AngularVelocityValue = angularVelocityValue |
| | | //}; |
| | | //concurrentDictionary.TryAdd(cameraSN, recordMotionControlData); |
| | | } |
| | | else |
| | | { |
| | | //RecordMotionControlData recordMotionControlData = new RecordMotionControlData |
| | | //{ |
| | | // ProductName = GlobalVar.strProductName, |
| | | // CommunicatorsName = communicatorsName, |
| | | // CameraSN = cameraSN, |
| | | // //XAxisAddress = this.textBoxXVarAddress.Text, |
| | | // //XAxisDetectLocationValue = xAxisDetectLocationValue, |
| | | // //XAxisOriginalLocationValue = xAxisOriginalLocationValue, |
| | | // //YAxisAddress = this.textBoxYVarAddress.Text, |
| | | // //YAxisDetectLocationValue = yAxisDetectLocationValue, |
| | | // //YAxisOriginalLocationValue = yAxisOriginalLocationValue, |
| | | // //ZAxisAddress = this.textBoxZVarAddress.Text, |
| | | // //ZAxisDetectLocationValue = zAxisDetectLocationValue, |
| | | // //ZAxisOriginalLocationValue = zAxisOriginalLocationValue, |
| | | // //WAxisAddress = this.textBoxWVarAddress.Text, |
| | | // //WAxisDetectLocationValue = wAxisDetectLocationValue, |
| | | // //WAxisOriginalLocationValue = wAxisOriginalLocationValue, |
| | | // //PulseMotionSpeedAddress = this.textBoxPulseMotionSpeedAddress.Text, |
| | | // //PulseMotionSpeedValue = pulseMotionSpeedValue, |
| | | // //AngularVelocityAddress = this.textBoxAngularVelocityAddress.Text, |
| | | // //AngularVelocityValue = angularVelocityValue |
| | | //}; |
| | | //concurrentDictionary.TryAdd(cameraSN, recordMotionControlData); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | double xAxisDetectLocationValue, xAxisOriginalLocationValue, yAxisDetectLocationValue, yAxisOriginalLocationValue, zAxisDetectLocationValue, zAxisOriginalLocationValue, wAxisDetectLocationValue, wAxisOriginalLocationValue, pulseMotionSpeedValue, angularVelocityValue; |
| | | //xAxisDetectLocationValue = double.Parse(this.textBoxXDetectLocation.Text); |
| | | //xAxisOriginalLocationValue = double.Parse(this.textBoxXOriginalLocation.Text); |
| | | //yAxisDetectLocationValue = double.Parse(this.textBoxYDetectLocation.Text); |
| | | //yAxisOriginalLocationValue = double.Parse(this.textBoxYOriginalLocation.Text); |
| | | //zAxisDetectLocationValue = double.Parse(this.textBoxZDetectLocation.Text); |
| | | //zAxisOriginalLocationValue = double.Parse(this.textBoxZOriginalLocation.Text); |
| | | //wAxisDetectLocationValue = double.Parse(this.textBoxWDetectLocation.Text); |
| | | //wAxisOriginalLocationValue = double.Parse(this.textBoxWOriginalLocation.Text); |
| | | //pulseMotionSpeedValue = double.Parse(this.textBoxPointSpeed.Text); |
| | | //angularVelocityValue = double.Parse(this.textBoxPointSpeed.Text); |
| | | if (concurrentDictionary.Keys.Contains(cameraSN)) |
| | | { |
| | | //RecordMotionControlData recordMotionControlData = new RecordMotionControlData |
| | | //{ |
| | | // ProductName = GlobalVar.strProductName, |
| | | // CommunicatorsName = communicatorsName, |
| | | // CameraSN = cameraSN, |
| | | // //XAxisAddress = this.textBoxXVarAddress.Text, |
| | | // //XAxisDetectLocationValue = xAxisDetectLocationValue, |
| | | // //XAxisOriginalLocationValue = xAxisOriginalLocationValue, |
| | | // //YAxisAddress = this.textBoxYVarAddress.Text, |
| | | // //YAxisDetectLocationValue = yAxisDetectLocationValue, |
| | | // //YAxisOriginalLocationValue = yAxisOriginalLocationValue, |
| | | // //ZAxisAddress = this.textBoxZVarAddress.Text, |
| | | // //ZAxisDetectLocationValue = zAxisDetectLocationValue, |
| | | // //ZAxisOriginalLocationValue = zAxisOriginalLocationValue, |
| | | // //WAxisAddress = this.textBoxWVarAddress.Text, |
| | | // //WAxisDetectLocationValue = wAxisDetectLocationValue, |
| | | // //WAxisOriginalLocationValue = wAxisOriginalLocationValue, |
| | | // //PulseMotionSpeedAddress = this.textBoxPulseMotionSpeedAddress.Text, |
| | | // //PulseMotionSpeedValue = pulseMotionSpeedValue, |
| | | // //AngularVelocityAddress = this.textBoxAngularVelocityAddress.Text, |
| | | // //AngularVelocityValue = angularVelocityValue |
| | | //}; |
| | | //concurrentDictionary.TryAdd(cameraSN, recordMotionControlData); |
| | | } |
| | | else |
| | | { |
| | | //RecordMotionControlData recordMotionControlData = new RecordMotionControlData |
| | | //{ |
| | | // ProductName = GlobalVar.strProductName, |
| | | // CommunicatorsName = communicatorsName, |
| | | // CameraSN = cameraSN, |
| | | // //XAxisAddress = this.textBoxXVarAddress.Text, |
| | | // //XAxisDetectLocationValue = xAxisDetectLocationValue, |
| | | // //XAxisOriginalLocationValue = xAxisOriginalLocationValue, |
| | | // //YAxisAddress = this.textBoxYVarAddress.Text, |
| | | // //YAxisDetectLocationValue = yAxisDetectLocationValue, |
| | | // //YAxisOriginalLocationValue = yAxisOriginalLocationValue, |
| | | // //ZAxisAddress = this.textBoxZVarAddress.Text, |
| | | // //ZAxisDetectLocationValue = zAxisDetectLocationValue, |
| | | // //ZAxisOriginalLocationValue = zAxisOriginalLocationValue, |
| | | // //WAxisAddress = this.textBoxWVarAddress.Text, |
| | | // //WAxisDetectLocationValue = wAxisDetectLocationValue, |
| | | // //WAxisOriginalLocationValue = wAxisOriginalLocationValue, |
| | | // //PulseMotionSpeedAddress = this.textBoxPulseMotionSpeedAddress.Text, |
| | | // //PulseMotionSpeedValue = pulseMotionSpeedValue, |
| | | // //AngularVelocityAddress = this.textBoxAngularVelocityAddress.Text, |
| | | // //AngularVelocityValue = angularVelocityValue |
| | | //}; |
| | | //concurrentDictionary.TryAdd(cameraSN, recordMotionControlData); |
| | | } |
| | | } |
| | | GlobalVar.dicMotionControlData.TryAdd(GlobalVar.strProductName, concurrentDictionary); |
| | | ConfigManager<Dictionary<string, Dictionary<string, RecordMotionControlData>>>.SaveConfig<Dictionary<string, Dictionary<string, RecordMotionControlData>>>(GlobalVar.dicMotionControlData, GlobalVar.allMotionControlDataPath); |
| | | Unsubscribe(); |
| | | } |
| | | Dictionary<string, PlcConfig> concurrentDictionary = new Dictionary<string, PlcConfig>(); |
| | | |
| | | public void Unsubscribe() |
| | | { |
| | |
| | | |
| | | private void btnAdd_Click(object sender, EventArgs e) |
| | | { |
| | | // 创建新增数据的对话框(这里简化为直接创建默认数据) |
| | | PlcDataItem newData = new PlcDataItem |
| | | { |
| | | SN = (GlobalVar.dicMPlcData.Count()).ToString(), |
| | | ReadAddress = "DB1003.DBD1", |
| | | WriteAddress = "DB1003.DBD51", |
| | | Value = 0.0F, |
| | | Description = "请修改对应描述" |
| | | }; |
| | | |
| | | // 添加到嵌套字典 |
| | | if (!GlobalVar.dicMPlcData.ContainsKey(newData.ReadAddress)) |
| | | using (var configForm = new ConfigForm(config, GlobalVar.allPlcSettingPath + "\\" + this.Name + ".json")) |
| | | { |
| | | GlobalVar.dicMPlcData.TryAdd(newData.ReadAddress, new Dictionary<string, PlcDataItem> { [newData.ReadAddress] = newData }); |
| | | } |
| | | else |
| | | { |
| | | newData.ReadAddress = newData.ReadAddress + GlobalVar.dicMPlcData.Count; |
| | | GlobalVar.dicMPlcData.TryAdd(newData.ReadAddress, new Dictionary<string, PlcDataItem> { [newData.ReadAddress] = newData }); |
| | | } |
| | | |
| | | // 更新DataGridView数据源 |
| | | UpdateDataSource(); |
| | | |
| | | // 选中新增的行 |
| | | dataGridView1.ClearSelection(); |
| | | var newRow = dataGridView1.Rows[dataGridView1.Rows.Count - 1]; |
| | | newRow.Selected = true; |
| | | dataGridView1.FirstDisplayedScrollingRowIndex = newRow.Index; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// 将嵌套字典转换为视图模型并更新数据源 |
| | | /// </summary> |
| | | private void UpdateDataSource() |
| | | { |
| | | // 转换嵌套字典为扁平的视图模型列表 |
| | | List<PlcDataItem> viewModelList = new List<PlcDataItem>(); |
| | | foreach (var group in GlobalVar.dicMPlcData) |
| | | { |
| | | foreach (var device in group.Value) |
| | | if (configForm.ShowDialog() == DialogResult.OK) |
| | | { |
| | | var data = device.Value; |
| | | viewModelList.Add(new PlcDataItem |
| | | { |
| | | SN = (GlobalVar.dicMPlcData.Count()).ToString(), |
| | | ReadAddress = data.ReadAddress, |
| | | WriteAddress = data.WriteAddress, |
| | | Value = data.Value, |
| | | Description = data.Description |
| | | }); |
| | | config = S7ConfigService.LoadConfig(GlobalVar.allPlcSettingPath); |
| | | LoadConfigAndSetupGrid(); |
| | | UpdateConnectionStatus(); |
| | | } |
| | | } |
| | | _bindingSource.DataSource = viewModelList; |
| | | } |
| | | |
| | | |
| | | private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) |
| | | // 数据类型转换辅助方法 |
| | | private object ConvertValue(string dataType, string valueStr) |
| | | { |
| | | if (e.RowIndex < 0 || e.ColumnIndex < 0) |
| | | switch (dataType) |
| | | { |
| | | return; |
| | | case "Bool": |
| | | { |
| | | if (bool.TryParse(valueStr, out bool b)) return b; |
| | | if (valueStr == "1") return true; |
| | | if (valueStr == "0") return false; |
| | | throw new Exception("无效的布尔值"); |
| | | } |
| | | case "Byte": |
| | | { |
| | | return byte.Parse(valueStr); |
| | | } |
| | | case "Int": |
| | | { |
| | | return short.Parse(valueStr); |
| | | } |
| | | case "DInt": |
| | | { |
| | | return int.Parse(valueStr); |
| | | } |
| | | case "Real": |
| | | { |
| | | return float.Parse(valueStr); |
| | | } |
| | | case "String": |
| | | { |
| | | return valueStr; |
| | | } |
| | | default: |
| | | { |
| | | throw new NotSupportedException($"不支持的类型: {dataType}"); |
| | | } |
| | | } |
| | | |
| | | // 获取修改后的视图模型 |
| | | var modifiedModel = (PlcDataItem)_bindingSource.List[e.RowIndex]; |
| | | |
| | | // 更新原始字典数据 |
| | | if (GlobalVar.dicMPlcData.ContainsKey(modifiedModel.SN)) |
| | | { |
| | | var originalData = GlobalVar.dicMPlcData[modifiedModel.SN][GlobalVar.dicMPlcData[modifiedModel.SN].Keys.ToList()[0]]; |
| | | |
| | | // 同步修改后的值 |
| | | originalData.SN = modifiedModel.SN; |
| | | originalData.ReadAddress = modifiedModel.ReadAddress; |
| | | originalData.WriteAddress = modifiedModel.WriteAddress; |
| | | originalData.Value = modifiedModel.Value; |
| | | originalData.Description = modifiedModel.Description; |
| | | GlobalVar.dicMPlcData[modifiedModel.SN] = new Dictionary<string, PlcDataItem> { [originalData.SN] = originalData }; |
| | | } |
| | | |
| | | } |
| | | |
| | | private void btnRemove_Click(object sender, EventArgs e) |
| | | private void LoadConfigAndSetupGrid() |
| | | { |
| | | if (dataGridView1.SelectedRows.Count == 0) |
| | | config = S7ConfigService.LoadConfig(GlobalVar.allPlcSettingPath + "\\" + this.Name + ".json"); |
| | | |
| | | if (GlobalVar.allPlcSettings.Keys.Contains(Name)) |
| | | { |
| | | System.Windows.Forms.MessageBox.Show("请先选中要删除的行!"); |
| | | return; |
| | | } |
| | | |
| | | // 获取选中行的视图模型 |
| | | var selectedModel = (PlcDataItem)dataGridView1.SelectedRows[0].DataBoundItem; |
| | | |
| | | // 从嵌套字典中删除对应数据 |
| | | if (GlobalVar.dicMPlcData.ContainsKey(selectedModel.SN)) |
| | | { |
| | | GlobalVar.dicMPlcData.Remove(selectedModel.SN); |
| | | |
| | | // 更新DataGridView数据源 |
| | | UpdateDataSource(); |
| | | System.Windows.Forms.MessageBox.Show("删除成功!"); |
| | | GlobalVar.allPlcSettings[Name] = config; |
| | | } |
| | | else |
| | | { |
| | | System.Windows.MessageBox.Show("未找到对应数据,删除失败!"); |
| | | GlobalVar.allPlcSettings.TryAdd(Name, config); |
| | | } |
| | | ConfigManager<Dictionary<string, Dictionary<string, PlcConfig>>>.SaveConfig<Dictionary<string, Dictionary<string, PlcConfig>>>(GlobalVar.dicMPlcData, GlobalVar.allMPlcDataPath); |
| | | // 构建 DataTable 用于展示信号(支持编辑值列) |
| | | signalTable = new DataTable(); |
| | | signalTable.Columns.Add("SignalName", typeof(string)); |
| | | signalTable.Columns.Add("Address", typeof(string)); |
| | | signalTable.Columns.Add("Value", typeof(string)); |
| | | |
| | | foreach (var signal in config.Signals) |
| | | { |
| | | string address = $"{signal.Area}"; |
| | | if (signal.Area == "DB") |
| | | address += $"{signal.DbNumber}.DB"; |
| | | address += $"{signal.ByteOffset}.{signal.BitOffset}"; |
| | | |
| | | signalTable.Rows.Add(signal.SignalName, address, ""); |
| | | } |
| | | |
| | | bindingSource = new BindingSource(); |
| | | bindingSource.DataSource = signalTable; |
| | | dgvSignals.DataSource = bindingSource; |
| | | |
| | | dgvSignals.Columns["SignalName"].HeaderText = "信号名称"; |
| | | dgvSignals.Columns["SignalName"].ReadOnly = true; |
| | | dgvSignals.Columns["Address"].HeaderText = "PLC 地址"; |
| | | dgvSignals.Columns["Address"].ReadOnly = true; |
| | | dgvSignals.Columns["Value"].HeaderText = "当前值"; |
| | | dgvSignals.Columns["Value"].ReadOnly = true; // 允许用户编辑 |
| | | |
| | | UpdateConnectionStatus(); |
| | | dgvSignals.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill; |
| | | dgvSignals.ForeColor = Color.Black; |
| | | } |
| | | |
| | | private void UpdateConnectionStatus() |
| | | { |
| | | if (((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]) != null && ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).bConnected) |
| | | { |
| | | lblStatus.Text = $"已连接: {config.PlcIpAddress}"; |
| | | lblStatus.ForeColor = Color.Green; |
| | | } |
| | | else |
| | | { |
| | | lblStatus.Text = "未连接"; |
| | | lblStatus.ForeColor = Color.Red; |
| | | } |
| | | } |
| | | |
| | | private void btnOneClickRead_Click(object sender, EventArgs e) |
| | | { |
| | | if (((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]) == null || !((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).bConnected) |
| | | { |
| | | System.Windows.Forms.MessageBox.Show("请先连接 PLC", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | for (int i = 0; i < config.Signals.Count; i++) |
| | | { |
| | | var signal = config.Signals[i]; |
| | | object value = ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).ReadSignal(signal); |
| | | config.Signals[i].Value = value?.ToString() ?? ""; |
| | | signalTable.Rows[i]["Value"] = config.Signals[i].Value; |
| | | //ShowLogMsg(signalTable.Rows[i]["Value"] + ":" + value?.ToString() ?? ""); |
| | | } |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | System.Windows.Forms.MessageBox.Show($"批量读取失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| | | } |
| | | } |
| | | |
| | | private void btnOneClickSetting_Click(object sender, EventArgs e) |
| | | { |
| | | if (((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]) == null || !((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).bConnected) |
| | | { |
| | | System.Windows.Forms.MessageBox.Show("请先连接 PLC", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); |
| | | return; |
| | | } |
| | | |
| | | try |
| | | { |
| | | int successCount = 0; |
| | | for (int i = 0; i < config.Signals.Count; i++) |
| | | { |
| | | var signal = config.Signals[i]; |
| | | string newValueStr = signalTable.Rows[i]["Value"]?.ToString(); |
| | | if (!string.IsNullOrEmpty(newValueStr)) |
| | | { |
| | | object convertedValue = ConvertValue(signal.DataType, newValueStr); |
| | | ((SiemensLBS7)GlobalVar.dicCommunicators[GlobalVar.currentCommunicators]).WriteSignal(signal, convertedValue); |
| | | successCount++; |
| | | } |
| | | //ShowSendMsg(signalTable.Rows[i]["Value"] + ":" + newValueStr?.ToString() ?? ""); |
| | | } |
| | | System.Windows.Forms.MessageBox.Show($"批量写入完成,成功写入 {successCount} 个信号。", "写入结果", MessageBoxButtons.OK, MessageBoxIcon.Information); |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | System.Windows.Forms.MessageBox.Show($"批量写入失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error); |
| | | } |
| | | } |
| | | |
| | | private void btnSave_Click(object sender, EventArgs e) |
| | | { |
| | | LoadConfigAndSetupGrid(); |
| | | } |
| | | } |
| | | } |