Files
luer/鲁尔圆锥接头测试仪/ParameterSetting2.cs
2026-02-07 10:58:56 +08:00

297 lines
11 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Modbus.Device;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using .Data;
namespace
{
public partial class ParameterSetting2 : UIForm
{
private ParameterSetting1 _parameterSetting1;
private ParameterSetting2 _parameterSetting2;
DataChange c;
private readonly System.Timers.Timer _readTimer;
private bool _isManualInput = false; // 手动输入标记
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
Function ma;
public ParameterSetting2()
{
InitializeComponent();
InitTimer();
}
private System.Windows.Forms.Timer InitTimer()
{
var timer = new System.Windows.Forms.Timer()
{
Interval =500
};
timer.Tick += async (s, e) =>
{
if (!_isManualInput && _modbusMaster != null)
{
try
{
await ReadLeakTestParametersAsync();
}
catch { }
}
};
timer.Start();
return timer;
}
private async System.Threading.Tasks.Task ReadLeakTestParametersAsync()
{
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null)
{
return;
}
try
{
// 读取应力开裂装夹轴向力 (D-460)
ushort[] axialForceRegisters = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 460, 2)
);
if (axialForceRegisters != null && axialForceRegisters.Length >= 2)
{
float axialForceValue = c.UshortToFloat(axialForceRegisters[1], axialForceRegisters[0]);
this.Invoke(new Action(() => uiTextBox1.Text = axialForceValue.ToString("F2")));
}
// 读取应力开裂装配扭矩 (D-462)
ushort[] torqueRegisters = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 462, 2)
);
if (torqueRegisters != null && torqueRegisters.Length >= 2)
{
float torqueValue = c.UshortToFloat(torqueRegisters[1], torqueRegisters[0]);
this.Invoke(new Action(() => uiTextBox3.Text = torqueValue.ToString("F2")));
}
// 读取应力开裂保持时间 (D-464) - 整数类型
ushort[] holdTimeRegisters = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 464, 1)
);
if (holdTimeRegisters != null && holdTimeRegisters.Length >= 1)
{
int holdTimeValue = holdTimeRegisters[0];
this.Invoke(new Action(() => uiTextBox4.Text = holdTimeValue.ToString()));
}
// 读取漏气旋开分离扭力装夹轴向力 (D-470)
ushort[] axialForceRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 470, 2)
);
if (axialForceRegisters1 != null && axialForceRegisters1.Length >= 2)
{
float axialForceValue1 = c.UshortToFloat(axialForceRegisters1[1], axialForceRegisters1[0]);
this.Invoke(new Action(() => uiTextBox10.Text = axialForceValue1.ToString("F2")));
}
// 读取旋开分离扭力装配扭矩 (D-472)
ushort[] torqueRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 472, 2)
);
if (torqueRegisters1 != null && torqueRegisters1.Length >= 2)
{
float torqueValue1 = c.UshortToFloat(torqueRegisters1[1], torqueRegisters1[0]);
this.Invoke(new Action(() => uiTextBox8.Text = torqueValue1.ToString("F2")));
}
// 读取旋开分离扭力持续时间 (D-474) - 整数类型
ushort[] holdTimeRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 474, 1)
);
if (holdTimeRegisters1 != null && holdTimeRegisters1.Length >= 1)
{
int holdTimeValue1 = holdTimeRegisters1[0];
this.Invoke(new Action(() => uiTextBox7.Text = holdTimeValue1.ToString()));
}
// 读取旋开分离扭力 (D-476)
ushort[] testPressureRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 476, 2)
);
if (testPressureRegisters1 != null && testPressureRegisters1.Length >= 2)
{
float testPressureValue1 = c.UshortToFloat(testPressureRegisters1[1], testPressureRegisters1[0]);
this.Invoke(new Action(() => uiTextBox6.Text = testPressureValue1.ToString("F2")));
}
}
catch (Exception ex)
{
this.Invoke(new Action(() =>
MessageBox.Show($"读取自定义系数2失败{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)));
}
}
private void ParameterSetting2_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);
c = new DataChange();
System.Threading.Tasks.Task.Delay(50).Wait();
//_readTimer.Start();
}
private bool TryReconnect()
{
try
{
string plcIp = "192.168.1.10";
bool initSuccess = Data.ModbusResourceManager.Instance.Init(plcIp, 502);
if (initSuccess)
{
ma = new Function(_modbusMaster);
return true;
}
}
catch (Exception ex)
{
//ShowErrorMsg($"重新连接失败:{ex.Message}");
}
return false;
}
private void ParameterSetting2_FormClosing(object sender, FormClosingEventArgs e)
{
// 停止定时器
_readTimer?.Stop();
_readTimer?.Dispose();
// 释放Modbus资源
ModbusResourceManager.Instance?.Dispose();
// 确保应用程序完全退出
Application.Exit();
}
private void ParameterSetting2_FormClosed(object sender, FormClosedEventArgs e)
{
_parameterSetting1?.Close();
//_parameterSetting2?.Close();
}
private void SwitchWindow<T>(ref T windowInstance, Func<T> createFunc) where T : UIForm
{
//1.停止当前窗口的定时器(不释放资源)
_readTimer?.Stop();
// 2. 检查资源是否可用(添加重连机制)
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null)
{
// 尝试重新连接
bool reconnectSuccess = TryReconnect();
if (!reconnectSuccess)
{
MessageBox.Show("TCP连接已断开请重新连接", "提示");
return;
}
}
// 3. 复用窗口实例:不存在则创建,存在则激活
if (windowInstance == null)
{
windowInstance = createFunc();
// 添加窗口关闭事件处理
windowInstance.Closed += (s, args) =>
{
// 窗口关闭时重新启动定时器并显示当前窗口
//_readTimer?.Start();
//this.Show();
this.Activate();
};
}
else
{
// 激活已存在的窗口(前置显示)
windowInstance.Activate();
return;
}
// 4. 切换窗口:隐藏当前窗口,显示目标窗口(非模态)
this.Hide();
windowInstance.Show(); // 使用 Show() 而不是 ShowDialog()
}
//返回
private void Btn_return_Click(object sender, EventArgs e)
{
SwitchWindow(ref _parameterSetting1, () => new ParameterSetting1());
}
//应力开裂:装夹轴向力
private void uiTextBox1_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox1.Text, 460, Function.DataType.);
}
//应力开裂:装配扭矩
private void uiTextBox3_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox3.Text, 462, Function.DataType.);
}
//应力开裂:保持时间
private void uiTextBox4_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox4.Text, 464, Function.DataType.);
}
//旋开分离扭力:装夹轴向力
private void uiTextBox10_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox10.Text, 470, Function.DataType.);
}
//旋开分离扭力:装配扭矩
private void uiTextBox8_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox8.Text, 472, Function.DataType.);
}
//旋开分离扭力:持续时间设置
private void uiTextBox7_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox7.Text, 474, Function.DataType.);
}
//旋开分离扭力:样品螺距
private void uiTextBox6_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox6.Text, 476, Function.DataType.,true,99,0);
}
}
}