1041 lines
39 KiB
C#
1041 lines
39 KiB
C#
using BasicDemo;
|
||
using Modbus.Device;
|
||
using Sunny.UI;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.ComponentModel;
|
||
using System.Data;
|
||
using System.Diagnostics;
|
||
using System.Drawing;
|
||
using System.Globalization;
|
||
using System.Linq;
|
||
using System.Net.Sockets;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using 外科辅料和患者防护罩激光抗性测试仪.Data;
|
||
using 外科辅料和患者防护罩激光抗性测试仪.Model;
|
||
using 材料热传导系数;
|
||
|
||
namespace 外科辅料和患者防护罩激光抗性测试仪
|
||
{
|
||
public partial class SecondaryIgnitionForm : UIForm
|
||
{
|
||
#region 私有字段
|
||
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
|
||
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
|
||
#endregion
|
||
|
||
Function ma;
|
||
DataChange c = new DataChange();
|
||
|
||
private System.Windows.Forms.Timer _readtimer;
|
||
public bool RunStatus = false;
|
||
|
||
ConductivityRepository conductivityRepository;
|
||
private readonly SemaphoreSlim _asyncLock = new SemaphoreSlim(1, 1);
|
||
public SecondaryIgnitionForm()
|
||
{
|
||
InitializeComponent();
|
||
|
||
conductivityRepository = new ConductivityRepository();
|
||
|
||
InitComboBoxPattern();
|
||
InitComboBox2Pattern();
|
||
InitComboBox3Pattern();
|
||
|
||
InitTimer();
|
||
}
|
||
|
||
private System.Windows.Forms.Timer InitTimer()
|
||
{
|
||
var timer = new System.Windows.Forms.Timer()
|
||
{
|
||
Interval = 500,
|
||
};
|
||
timer.Tick += async (s, e) =>
|
||
{
|
||
if (_modbusMaster != null)
|
||
{
|
||
try
|
||
{
|
||
await ReadDataAsync();
|
||
}
|
||
catch { }
|
||
}
|
||
};
|
||
return timer;
|
||
|
||
}
|
||
|
||
private async System.Threading.Tasks.Task ReadDataAsync()
|
||
{
|
||
try
|
||
{
|
||
if (_modbusMaster == null || _tcpClient == null || !_tcpClient.Connected)
|
||
{
|
||
return;
|
||
}
|
||
var tasks = new List<Task>
|
||
{
|
||
|
||
|
||
ReadFloatAsync(156,2,uiLabel7,"F1",""),//点火时间
|
||
ReadFloatAsync(164,2,uiLabel8,"F1",""),//余焰时间
|
||
ReadFloatAsync(168,2,uiLabel12,"F1",""),//余辉时间
|
||
|
||
ReadFloatAsync(1334,2,uiLabel48,"F1",""),//试验舱温度
|
||
ReadFloatAsync(1384,2,uiLabel40,"F1",""),//试验舱湿度
|
||
ReadFloatAsync(102,2,uiLabel36,"F1",""),//试验舱氧浓度
|
||
|
||
ReadLightStatusAsync(),
|
||
|
||
ReadtimeAsync(),//实时时间
|
||
|
||
ReadStartStatusAsync()
|
||
|
||
};
|
||
await Task.WhenAll(tasks);
|
||
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
//ShowError($"读取数据失败:{ex.Message}");
|
||
}
|
||
}
|
||
|
||
private async Task ReadFloatAsync(int address, int length, Label control, string format, string unit)
|
||
{
|
||
if (control == null) return;
|
||
|
||
try
|
||
{
|
||
var modbusMaster = _modbusMaster;
|
||
if (modbusMaster == null) return;
|
||
|
||
ushort[] registers = await modbusMaster.ReadHoldingRegistersAsync(1, (ushort)address, (ushort)length)
|
||
.ConfigureAwait(false);
|
||
|
||
if (registers == null || registers.Length == 0)
|
||
{
|
||
System.Diagnostics.Debug.WriteLine($"读取地址{address}失败:未获取到有效寄存器数据");
|
||
return;
|
||
}
|
||
|
||
string text;
|
||
if (registers.Length >= 2 && length >= 2)
|
||
{
|
||
float value = c.UshortToFloat(registers[1], registers[0]);
|
||
text = $"{value.ToString(format, CultureInfo.InvariantCulture)}{unit}";
|
||
}
|
||
else if (registers.Length >= 1)
|
||
{
|
||
int value = registers[0];
|
||
text = $"{value.ToString(format, CultureInfo.InvariantCulture)}{unit}";
|
||
}
|
||
else
|
||
{
|
||
return;
|
||
}
|
||
|
||
UpdateControlText(control, text);
|
||
}
|
||
catch (OperationCanceledException)
|
||
{
|
||
return;
|
||
}
|
||
catch (Exception ex) when (ex is TimeoutException ||
|
||
ex is InvalidOperationException ||
|
||
ex.Message.Contains("Modbus", StringComparison.OrdinalIgnoreCase))
|
||
{
|
||
Debug.WriteLine($"Modbus通信错误 - 地址{address}: {ex.Message}");
|
||
UpdateControlText(control, "通信错误");
|
||
}
|
||
catch (ObjectDisposedException)
|
||
{
|
||
return;
|
||
}
|
||
}
|
||
private void UpdateControlText(Label control, string text)
|
||
{
|
||
if (control.IsDisposed) return;
|
||
|
||
if (control.InvokeRequired)
|
||
{
|
||
try
|
||
{
|
||
control.BeginInvoke(new Action(() =>
|
||
{
|
||
if (!control.IsDisposed)
|
||
control.Text = text;
|
||
}));
|
||
}
|
||
catch (ObjectDisposedException)
|
||
{
|
||
}
|
||
}
|
||
else
|
||
{
|
||
control.Text = text;
|
||
}
|
||
}
|
||
|
||
private async Task ReadLightStatusAsync()
|
||
{
|
||
try
|
||
{
|
||
bool[] registers1 = await Task.Run(async () =>
|
||
{
|
||
if (_modbusMaster == null)
|
||
return null;
|
||
return await _modbusMaster?.ReadCoilsAsync(1, 10, 1);
|
||
});
|
||
if (registers1 != null && registers1.Length >= 1)
|
||
{
|
||
bool value = registers1[0];
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
if (value)
|
||
{
|
||
uiLight1.State = UILightState.On;
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
uiLight1.State = UILightState.Off;
|
||
}
|
||
}));
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.WriteLine($"读取状态失败: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
private async Task ReadtimeAsync()
|
||
{
|
||
try
|
||
{
|
||
|
||
string currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
|
||
// 跨线程更新UI(异步方法中仍需检查Invoke,避免跨线程异常)
|
||
if (uiLabel25.InvokeRequired)
|
||
{
|
||
uiLabel25.Invoke(new Action(() =>
|
||
{
|
||
uiLabel25.Text = currentTime;
|
||
}));
|
||
}
|
||
else
|
||
{
|
||
uiLabel25.Text = currentTime;
|
||
}
|
||
|
||
// 等待1秒再更新(异步等待,不阻塞UI线程)
|
||
await Task.Delay(1000);
|
||
|
||
}
|
||
|
||
catch (Exception ex)
|
||
{
|
||
uiLabel25.Text = $"错误:{ex.Message}";
|
||
}
|
||
}
|
||
|
||
private bool _lastRunStatus = false;
|
||
private bool _isReported = false;
|
||
private async Task ReadStartStatusAsync()
|
||
{
|
||
try
|
||
{
|
||
bool[] registers2 = await Task.Run(async () =>
|
||
{
|
||
if (_modbusMaster == null)
|
||
return null;
|
||
return await _modbusMaster?.ReadCoilsAsync(1, 301, 1);
|
||
});
|
||
if (registers2 != null && registers2.Length >= 1)
|
||
{
|
||
bool currentRunStatus = registers2[0];
|
||
RunStatus = currentRunStatus;
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
uiLabel23.Text = currentRunStatus ? "运行中" : "空闲中";
|
||
}));
|
||
// ========== 关键修改1:移除重复的插入逻辑,只保留一段 ==========
|
||
// 仅在「运行→停止」且未上报过的情况下执行一次插入
|
||
if (_lastRunStatus && !currentRunStatus && !_isReported)
|
||
{
|
||
// 添加延迟确保数据稳定
|
||
await Task.Delay(500);
|
||
// 加锁防止异步并发重复执行
|
||
await _asyncLock.WaitAsync();
|
||
try
|
||
{
|
||
// 二次校验,防止锁等待期间状态已变更
|
||
if (!_isReported)
|
||
{
|
||
var model = await InsertReportAsync();
|
||
if (model != null && !string.IsNullOrEmpty(model.Id3))
|
||
{
|
||
conductivityRepository.InsertReportItems(model);
|
||
_isReported = true; // 标记已上报
|
||
Debug.WriteLine($"报表插入成功:样品{model.sampleType3}, 分类{model.result3}");
|
||
}
|
||
}
|
||
}
|
||
finally
|
||
{
|
||
_asyncLock.Release(); // 释放锁
|
||
}
|
||
}
|
||
|
||
_lastRunStatus = currentRunStatus;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.WriteLine($"读取状态失败: {ex.Message}");
|
||
}
|
||
finally { _asyncLock.Release(); }
|
||
}
|
||
|
||
public async Task<bool[]> ReadModbusCoilAsync(ushort registerAddress, ushort readCount)
|
||
{
|
||
if (_modbusMaster == null) return null;
|
||
return await Task.Run(async () =>
|
||
{
|
||
return await _modbusMaster.ReadCoilsAsync(1, registerAddress, readCount);
|
||
});
|
||
}
|
||
|
||
|
||
|
||
List<SecondaryIgnitionModel> reportList = new List<SecondaryIgnitionModel>();
|
||
private string classify = null;
|
||
private float yy = 0;
|
||
private float yh = 0;
|
||
private bool iszixi = false;
|
||
private bool isdianhuo = false;
|
||
|
||
private bool _isCurrentRecordAdded = false;//重复添加标记
|
||
private async Task<SecondaryIgnitionModel> InsertReportAsync()
|
||
{
|
||
if (_isReported) return new SecondaryIgnitionModel();
|
||
if (!_isReported)
|
||
{
|
||
if (uiComboBox2.Text != null && uiComboBox2.Text.Trim() == "样品1")
|
||
{
|
||
bool[] value1 = await ReadModbusCoilAsync(230, 1);
|
||
bool[] value2 = await ReadModbusCoilAsync(231, 1);
|
||
bool[] value3 = await ReadModbusCoilAsync(232, 1);
|
||
bool[] value4 = await ReadModbusCoilAsync(233, 1);
|
||
|
||
if (value1 != null && value1.Length > 0 && value1[0] && value3 != null && value3.Length > 0 && value3[0])
|
||
{
|
||
classify = "SI3";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
|
||
else if (value1 != null && value1.Length > 0 && value1[0] && value2 != null && value2.Length > 0 && value2[0])
|
||
{
|
||
classify = "SI2";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
else if (value4 != null && value4.Length > 0 && value4[0])
|
||
{
|
||
classify = "SI1";//分类
|
||
iszixi = false;
|
||
isdianhuo = false;
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
}
|
||
|
||
}
|
||
|
||
if (uiComboBox2.Text != null && uiComboBox2.Text.Trim() == "样品2")
|
||
{
|
||
|
||
bool[] value1 = await ReadModbusCoilAsync(230, 1);
|
||
bool[] value2 = await ReadModbusCoilAsync(231, 1);
|
||
bool[] value3 = await ReadModbusCoilAsync(232, 1);
|
||
bool[] value4 = await ReadModbusCoilAsync(233, 1);
|
||
|
||
if (value1 != null && value1.Length > 0 && value1[0] && value3 != null && value3.Length > 0 && value3[0])
|
||
{
|
||
classify = "SI3";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
|
||
else if (value1 != null && value1.Length > 0 && value1[0] && value2 != null && value2.Length > 0 && value2[0])
|
||
{
|
||
classify = "SI2";//分类
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
else if (value4 != null && value4.Length > 0 && value4[0])
|
||
{
|
||
classify = "SI1";//分类
|
||
iszixi = false;
|
||
isdianhuo = false;
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
}
|
||
}
|
||
|
||
if (uiComboBox2.Text != null && uiComboBox2.Text.Trim() == "样品3")
|
||
{
|
||
|
||
bool[] value1 = await ReadModbusCoilAsync(230, 1);
|
||
bool[] value2 = await ReadModbusCoilAsync(231, 1);
|
||
bool[] value3 = await ReadModbusCoilAsync(232, 1);
|
||
bool[] value4 = await ReadModbusCoilAsync(233, 1);
|
||
|
||
if (value1 != null && value1.Length > 0 && value1[0] && value3 != null && value3.Length > 0 && value3[0])
|
||
{
|
||
classify = "SI3";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
|
||
else if (value1 != null && value1.Length > 0 && value1[0] && value2 != null && value2.Length > 0 && value2[0])
|
||
{
|
||
classify = "SI2";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
else if (value4 != null && value4.Length > 0 && value4[0])
|
||
{
|
||
classify = "SI1";//分类
|
||
iszixi = false;
|
||
isdianhuo = false;
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
}
|
||
}
|
||
|
||
if (uiComboBox2.Text != null && uiComboBox2.Text.Trim() == "样品4")
|
||
{
|
||
bool[] value1 = await ReadModbusCoilAsync(230, 1);
|
||
bool[] value2 = await ReadModbusCoilAsync(231, 1);
|
||
bool[] value3 = await ReadModbusCoilAsync(232, 1);
|
||
bool[] value4 = await ReadModbusCoilAsync(233, 1);
|
||
|
||
if (value1 != null && value1.Length > 0 && value1[0] && value3 != null && value3.Length > 0 && value3[0])
|
||
{
|
||
classify = "SI3";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
|
||
else if (value1 != null && value1.Length > 0 && value1[0] && value2 != null && value2.Length > 0 && value2[0])
|
||
{
|
||
classify = "SI2";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
else if (value4 != null && value4.Length > 0 && value4[0])
|
||
{
|
||
classify = "SI1";//分类
|
||
iszixi = false;
|
||
isdianhuo = false;
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
}
|
||
}
|
||
|
||
if (uiComboBox2.Text != null && uiComboBox2.Text.Trim() == "样品5")
|
||
{
|
||
bool[] value1 = await ReadModbusCoilAsync(230, 1);
|
||
bool[] value2 = await ReadModbusCoilAsync(231, 1);
|
||
bool[] value3 = await ReadModbusCoilAsync(232, 1);
|
||
bool[] value4 = await ReadModbusCoilAsync(233, 1);
|
||
|
||
if (value1 != null && value1.Length > 0 && value1[0] && value3 != null && value3.Length > 0 && value3[0])
|
||
{
|
||
classify = "SI3";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
|
||
else if (value1 != null && value1.Length > 0 && value1[0] && value2 != null && value2.Length > 0 && value2[0])
|
||
{
|
||
classify = "SI2";//分类
|
||
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
if (value2[0])
|
||
{
|
||
iszixi = true; //自熄
|
||
}
|
||
else if (value3[0])
|
||
{
|
||
iszixi = false;
|
||
}
|
||
if (value1[0])
|
||
{
|
||
isdianhuo = true;//点火
|
||
}
|
||
else if (value4[0])
|
||
{
|
||
isdianhuo = false;
|
||
}
|
||
}
|
||
else if (value4 != null && value4.Length > 0 && value4[0])
|
||
{
|
||
classify = "SI1";//分类
|
||
iszixi = false;
|
||
isdianhuo = false;
|
||
//ushort[] yhlocation = _modbusMaster?.ReadHoldingRegisters(1, 330, 4);
|
||
//if (yhlocation != null && yhlocation.Length >= 3)
|
||
//{
|
||
yy = float.Parse(uiLabel8.Text);//余焰
|
||
|
||
yh = float.Parse(uiLabel12.Text);
|
||
//}
|
||
}
|
||
}
|
||
|
||
|
||
try
|
||
{
|
||
string selectedO2Text = uiComboBox1.Text;
|
||
string id = uiComboBox2.Text;
|
||
var model = new SecondaryIgnitionModel
|
||
{
|
||
Id3 = id,
|
||
Iszixi3 = iszixi,
|
||
Isdianhuo3 = isdianhuo,
|
||
O23 = selectedO2Text,
|
||
result3 = classify,
|
||
sampleType3 = uiComboBox2.Text.Contains("1") ? 1
|
||
: uiComboBox2.Text.Contains("2") ? 2
|
||
: uiComboBox2.Text.Contains("3") ? 3
|
||
: uiComboBox2.Text.Contains("4") ? 4
|
||
: uiComboBox2.Text.Contains("5") ? 5 : 0,
|
||
yuhui3 = yh,
|
||
yuyan3 = yy,
|
||
Testtime3 = DateTime.Now
|
||
};
|
||
reportList.Add(model);
|
||
|
||
_isCurrentRecordAdded = true; // 标记为已添加,避免重复
|
||
|
||
await Task.Delay(100);
|
||
return model;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.WriteLine($"报表插入失败: {ex.Message}");
|
||
return new SecondaryIgnitionModel();
|
||
}
|
||
}
|
||
else
|
||
{
|
||
_isCurrentRecordAdded = false;
|
||
return new SecondaryIgnitionModel();
|
||
}
|
||
}
|
||
|
||
private void SecondaryIgnitionForm_Load(object sender, EventArgs e)
|
||
{
|
||
string plcIp = "192.168.1.10";
|
||
bool initSuccess = Data.ModbusResourceManager.Instance.Init(plcIp, 502);
|
||
if (!initSuccess)
|
||
{
|
||
MessageBox.Show("连接Modbus服务器失败!", "错误");
|
||
this.Close();
|
||
return;
|
||
}
|
||
|
||
// 检查连接状态
|
||
if (_tcpClient == null || !_tcpClient.Connected)
|
||
{
|
||
MessageBox.Show("Modbus连接异常!", "错误");
|
||
this.Close();
|
||
return;
|
||
}
|
||
ma = new Function(_modbusMaster);
|
||
|
||
_readtimer = InitTimer();
|
||
if (_modbusMaster != null)
|
||
{
|
||
_readtimer.Start();
|
||
}
|
||
|
||
this.FormClosed += (s, args) =>
|
||
{
|
||
// 当子窗体关闭时,显示主窗体
|
||
Application.OpenForms["MainForm"]?.Show();
|
||
};
|
||
}
|
||
|
||
private void InitComboBoxPattern()
|
||
{
|
||
// 清空原有选项(可选,避免重复添加)
|
||
//Combox_pattern.Items.Clear();
|
||
|
||
// 定义需要添加的选项数组
|
||
string[] options = {
|
||
"环境空气 (21%)",
|
||
"富氧环境 (60%)",
|
||
"富氧环境 (98%)",
|
||
"无氧环境/灭火 (0%)"
|
||
};
|
||
uiComboBox1.Items.AddRange(options);
|
||
|
||
}
|
||
private void InitComboBox2Pattern()
|
||
{
|
||
// 清空原有选项(可选,避免重复添加)
|
||
//Combox_pattern.Items.Clear();
|
||
|
||
// 定义需要添加的选项数组
|
||
string[] options = {
|
||
"样品1",
|
||
"样品2",
|
||
"样品3",
|
||
"样品4",
|
||
"样品5"
|
||
};
|
||
uiComboBox2.Items.AddRange(options);
|
||
}
|
||
|
||
private void InitComboBox3Pattern()
|
||
{
|
||
// 清空原有选项(可选,避免重复添加)
|
||
//Combox_pattern.Items.Clear();
|
||
|
||
// 定义需要添加的选项数组
|
||
string[] options = {
|
||
"样品1",
|
||
"样品2",
|
||
"样品3",
|
||
"样品4",
|
||
"样品5"
|
||
};
|
||
uiComboBox3.Items.AddRange(options);
|
||
|
||
}
|
||
|
||
private void SecondaryIgnitionForm_FormClosed(object sender, FormClosedEventArgs e)
|
||
{
|
||
foreach (Form form in Application.OpenForms)
|
||
{
|
||
if (form.Name == "MainForm" || form.Text.Contains("主界面"))
|
||
{
|
||
form.Show();
|
||
form.Activate(); // 激活窗体
|
||
break;
|
||
}
|
||
}
|
||
}
|
||
|
||
private void uiButton10_Click(object sender, EventArgs e)
|
||
{
|
||
this.Close();//返回主页面
|
||
}
|
||
|
||
private void uiButton4_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 224);//余辉开始
|
||
}
|
||
|
||
private void uiButton5_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 225);//余辉结束
|
||
}
|
||
|
||
private void uiButton6_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 217);//丝光棉燃尽
|
||
}
|
||
|
||
private void uiButton11_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 227);//未自熄灭
|
||
}
|
||
|
||
private void uiButton12_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 223);//自熄
|
||
}
|
||
|
||
private void uiButton1_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 300);//开始测试
|
||
}
|
||
|
||
private void uiButton2_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 304);//手动停止
|
||
}
|
||
|
||
private void uiButton3_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 99);//灭火
|
||
}
|
||
|
||
private void uiButton7_Click(object sender, EventArgs e)
|
||
{
|
||
string message = "帮助一:请目测判断丝光棉是否燃尽,如燃尽请按下“丝光棉燃尽”按钮;\n帮助二:请目测判断样品是否燃尽,如燃尽请按下“火焰未自熄”按钮,如未燃尽请按下“火焰自熄”按钮;\n帮助三:请目测是否有余辉:有余辉请立即点击“余辉开始”按钮,余辉时间开始计时;余辉结束,请立即点击“余辉结束”按钮,余辉时间计时结束。";
|
||
|
||
Font font = new Font("Microsoft YaHei", 12, FontStyle.Regular);
|
||
|
||
// 创建一个自定义的消息框
|
||
TaskDialogPage page = new TaskDialogPage
|
||
{
|
||
Text = message,
|
||
Caption = "帮助",
|
||
SizeToContent = true
|
||
};
|
||
|
||
TaskDialog.ShowDialog(this, page);
|
||
}
|
||
|
||
private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
ushort selectedIndex = ushort.Parse(uiComboBox1.SelectedIndex.ToString());
|
||
_modbusMaster?.WriteSingleRegister(1, 10, selectedIndex);//下拉框
|
||
}
|
||
|
||
|
||
|
||
private void SecondaryIgnitionForm_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
if (_readtimer != null)
|
||
{
|
||
_readtimer.Stop();
|
||
_readtimer.Dispose();
|
||
}
|
||
}
|
||
|
||
private void uiButton8_Click(object sender, EventArgs e)
|
||
{
|
||
AutoCameraForm cameraForm = new AutoCameraForm(() => RunStatus);
|
||
this.SwitchToForm(() => cameraForm);
|
||
}
|
||
private void SwitchToForm(Func<Form> formCreator)
|
||
{
|
||
using (Form form = formCreator())
|
||
{
|
||
this.Hide();
|
||
form.ShowDialog();
|
||
this.Show();
|
||
this.Activate();
|
||
}
|
||
}
|
||
|
||
private void uiButton10_Click_1(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 305, 1);//清除
|
||
}
|
||
|
||
private void uiButton13_Click(object sender, EventArgs e)
|
||
{
|
||
this.Close();//返回主页面
|
||
}
|
||
|
||
private void uiComboBox3_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
string value = uiComboBox3.SelectedItem.ToString();
|
||
|
||
switch (value)
|
||
{
|
||
case "样品1":
|
||
{
|
||
var data = reportList?.OrderByDescending(s => s.Testtime3).FirstOrDefault(x => x.sampleType3 == 1);
|
||
uiLabel21.Text = data?.O23.ToString() ?? string.Empty;
|
||
uiLabel19.Text = data?.result3.ToString() ?? string.Empty;
|
||
uiLabel27.Text = data?.yuyan3.ToString() ?? string.Empty;
|
||
uiLabel29.Text = data?.yuhui3.ToString() ?? string.Empty;
|
||
uiLabel18.Text = data?.Iszixi3.ToString() ?? string.Empty;
|
||
uiLabel10.Text = data?.Isdianhuo3.ToString() ?? string.Empty;
|
||
uiLabel42.Text = data?.Testtime3.ToString() ?? string.Empty;
|
||
break;
|
||
}
|
||
case "样品2":
|
||
{
|
||
var data = reportList?.OrderByDescending(s => s.Testtime3).FirstOrDefault(x => x.sampleType3 == 2);
|
||
uiLabel21.Text = data?.O23.ToString() ?? string.Empty;
|
||
uiLabel19.Text = data?.result3.ToString() ?? string.Empty;
|
||
uiLabel27.Text = data?.yuyan3.ToString() ?? string.Empty;
|
||
uiLabel29.Text = data?.yuhui3.ToString() ?? string.Empty;
|
||
uiLabel18.Text = data?.Iszixi3.ToString() ?? string.Empty;
|
||
uiLabel10.Text = data?.Isdianhuo3.ToString() ?? string.Empty;
|
||
uiLabel42.Text = data?.Testtime3.ToString() ?? string.Empty;
|
||
break;
|
||
}
|
||
case "样品3":
|
||
{
|
||
var data = reportList?.OrderByDescending(s => s.Testtime3).FirstOrDefault(x => x.sampleType3 == 3);
|
||
uiLabel21.Text = data?.O23.ToString() ?? string.Empty;
|
||
uiLabel19.Text = data?.result3.ToString() ?? string.Empty;
|
||
uiLabel27.Text = data?.yuyan3.ToString() ?? string.Empty;
|
||
uiLabel29.Text = data?.yuhui3.ToString() ?? string.Empty;
|
||
uiLabel18.Text = data?.Iszixi3.ToString() ?? string.Empty;
|
||
uiLabel10.Text = data?.Isdianhuo3.ToString() ?? string.Empty;
|
||
uiLabel42.Text = data?.Testtime3.ToString() ?? string.Empty;
|
||
break;
|
||
}
|
||
case "样品4":
|
||
{
|
||
var data = reportList?.OrderByDescending(s => s.Testtime3).FirstOrDefault(x => x.sampleType3 == 4);
|
||
uiLabel21.Text = data?.O23.ToString() ?? string.Empty;
|
||
uiLabel19.Text = data?.result3.ToString() ?? string.Empty;
|
||
uiLabel27.Text = data?.yuyan3.ToString() ?? string.Empty;
|
||
uiLabel29.Text = data?.yuhui3.ToString() ?? string.Empty;
|
||
uiLabel18.Text = data?.Iszixi3.ToString() ?? string.Empty;
|
||
uiLabel10.Text = data?.Isdianhuo3.ToString() ?? string.Empty;
|
||
uiLabel42.Text = data?.Testtime3.ToString() ?? string.Empty;
|
||
break;
|
||
}
|
||
case "样品5":
|
||
{
|
||
var data = reportList?.OrderByDescending(s => s.Testtime3).FirstOrDefault(x => x.sampleType3 == 5);
|
||
uiLabel21.Text = data?.O23.ToString() ?? string.Empty;
|
||
uiLabel19.Text = data?.result3.ToString() ?? string.Empty;
|
||
uiLabel27.Text = data?.yuyan3.ToString() ?? string.Empty;
|
||
uiLabel29.Text = data?.yuhui3.ToString() ?? string.Empty;
|
||
uiLabel18.Text = data?.Iszixi3.ToString() ?? string.Empty;
|
||
uiLabel10.Text = data?.Isdianhuo3.ToString() ?? string.Empty;
|
||
uiLabel42.Text = data?.Testtime3.ToString() ?? string.Empty;
|
||
break;
|
||
}
|
||
default:
|
||
break;
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
}
|