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

636 lines
25 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;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace
{
public partial class ParameterSetting1 : UIForm
{
private TestScreen _TestScreen;
private DebugScreen _DebugScreen;
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 ParameterSetting1()
{
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-400)
ushort[] axialForceRegisters = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 400, 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-402)
ushort[] torqueRegisters = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 402, 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-404) - 整数类型
ushort[] holdTimeRegisters = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 404, 1)
);
if (holdTimeRegisters != null && holdTimeRegisters.Length >= 1)
{
int holdTimeValue = holdTimeRegisters[0];
this.Invoke(new Action(() => uiTextBox4.Text = holdTimeValue.ToString()));
}
// 读取漏液测试水压 (D-406)
ushort[] testPressureRegisters = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 406, 2)
);
if (testPressureRegisters != null && testPressureRegisters.Length >= 2)
{
float testPressureValue = c.UshortToFloat(testPressureRegisters[1], testPressureRegisters[0]);
this.Invoke(new Action(() => uiTextBox5.Text = testPressureValue.ToString("F1")));
}
// 读取漏气装夹轴向力 (D-410)
ushort[] axialForceRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 410, 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-412)
ushort[] torqueRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 412, 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-414) - 整数类型
ushort[] holdTimeRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 414, 1)
);
if (holdTimeRegisters1 != null && holdTimeRegisters1.Length >= 1)
{
int holdTimeValue1 = holdTimeRegisters1[0];
this.Invoke(new Action(() => uiTextBox7.Text = holdTimeValue1.ToString()));
}
// 读取漏气规定压力下限 (D-416)
ushort[] testPressureRegisters1 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 416, 2)
);
if (testPressureRegisters1 != null && testPressureRegisters1.Length >= 2)
{
float testPressureValue1 = c.UshortToFloat(testPressureRegisters1[1], testPressureRegisters1[0]);
this.Invoke(new Action(() => uiTextBox6.Text = testPressureValue1.ToString("F1")));
}
// 读取分离力装夹轴向力 (D-420)
ushort[] axialForceRegisters2 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 420, 2)
);
if (axialForceRegisters2 != null && axialForceRegisters2.Length >= 2)
{
float axialForceValue2 = c.UshortToFloat(axialForceRegisters2[1], axialForceRegisters2[0]);
this.Invoke(new Action(() => uiTextBox15.Text = axialForceValue2.ToString("F2")));
}
// 读取分离力装配扭矩 (D-422)
ushort[] torqueRegisters2 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 422, 2)
);
if (torqueRegisters2 != null && torqueRegisters2.Length >= 2)
{
float torqueValue2 = c.UshortToFloat(torqueRegisters2[1], torqueRegisters2[0]);
this.Invoke(new Action(() => uiTextBox13.Text = torqueValue2.ToString("F2")));
}
// 读取分离力分离轴向力 (D-426)
ushort[] sepaxialforce2 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 426, 2)
);
if (sepaxialforce2 != null && sepaxialforce2.Length >= 2)
{
float forceValue2 = c.UshortToFloat(sepaxialforce2[1], sepaxialforce2[0]);
this.Invoke(new Action(() => uiTextBox12.Text = forceValue2.ToString("F2")));
}
// 读取分离力持续时间设置 (D-424) - 整数类型
ushort[] holdTimeRegisters2 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 424, 1)
);
if (holdTimeRegisters2 != null && holdTimeRegisters2.Length >= 1)
{
int holdTimeValue2 = holdTimeRegisters2[0];
this.Invoke(new Action(() => uiTextBox11.Text = holdTimeValue2.ToString()));
}
// 读取分离力速率设置 (D-364)
ushort[] ratesetting2 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 364, 2)
);
if (ratesetting2 != null && ratesetting2.Length >= 2)
{
float speedValue2 = c.UshortToFloat(ratesetting2[1], ratesetting2[0]);
this.Invoke(new Action(() => uiTextBox16.Text = speedValue2.ToString("F2")));
}
// 读取旋开扭矩装夹轴向力 (D-430)
ushort[] axialForceRegisters3 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 430, 2)
);
if (axialForceRegisters3 != null && axialForceRegisters3.Length >= 2)
{
float axialForceValue3 = c.UshortToFloat(axialForceRegisters3[1], axialForceRegisters3[0]);
this.Invoke(new Action(() => uiTextBox21.Text = axialForceValue3.ToString("F2")));
}
// 读取旋开扭矩装配扭矩 (D-432)
ushort[] torqueRegisters3 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 432, 2)
);
if (torqueRegisters3 != null && torqueRegisters3.Length >= 2)
{
float torqueValue3 = c.UshortToFloat(torqueRegisters3[1], torqueRegisters3[0]);
this.Invoke(new Action(() => uiTextBox19.Text = torqueValue3.ToString("F2")));
}
// 读取旋开扭矩保持时间 (D-434) - 整数类型
ushort[] holdTimeRegisters3 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 434, 1)
);
if (holdTimeRegisters3 != null && holdTimeRegisters3.Length >= 1)
{
int holdTimeValue3 = holdTimeRegisters3[0];
this.Invoke(new Action(() => uiTextBox18.Text = holdTimeValue3.ToString()));
}
// 读取旋开扭矩旋开扭矩 (D-436)
ushort[] testPressureRegisters3 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 436, 2)
);
if (testPressureRegisters3 != null && testPressureRegisters3.Length >= 2)
{
float testPressureValue3 = c.UshortToFloat(testPressureRegisters3[1], testPressureRegisters3[0]);
this.Invoke(new Action(() => uiTextBox17.Text = testPressureValue3.ToString("F2")));
}
// 读取易装配性装夹轴向力 (D-440)
ushort[] axialForceRegisters4 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 440, 2)
);
if (axialForceRegisters4 != null && axialForceRegisters4.Length >= 2)
{
float axialForceValue4 = c.UshortToFloat(axialForceRegisters4[1], axialForceRegisters4[0]);
this.Invoke(new Action(() => uiTextBox26.Text = axialForceValue4.ToString("F2")));
}
// 读取易装配性装配扭矩 (D-442)
ushort[] torqueRegisters4 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 442, 2)
);
if (torqueRegisters4 != null && torqueRegisters4.Length >= 2)
{
float torqueValue4 = c.UshortToFloat(torqueRegisters4[1], torqueRegisters4[0]);
this.Invoke(new Action(() => uiTextBox24.Text = torqueValue4.ToString("F2")));
}
// 读取易装配性保持时间 (D-454) - 整数类型
ushort[] holdTimeRegisters4 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 444, 1)
);
if (holdTimeRegisters4 != null && holdTimeRegisters4.Length >= 1)
{
int holdTimeValue5 = holdTimeRegisters4[0];
this.Invoke(new Action(() => uiTextBox22.Text = holdTimeValue5.ToString()));
}
// 读取抗过载装夹轴向力 (D-450)
ushort[] axialForceRegisters5 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 450, 2)
);
if (axialForceRegisters5 != null && axialForceRegisters5.Length >= 2)
{
float axialForceValue5 = c.UshortToFloat(axialForceRegisters5[1], axialForceRegisters5[0]);
this.Invoke(new Action(() => uiTextBox31.Text = axialForceValue5.ToString("F2")));
}
// 读取抗过载装配扭矩 (D-452)
ushort[] torqueRegisters5 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 452, 2)
);
if (torqueRegisters5 != null && torqueRegisters5.Length >= 2)
{
float torqueValue5 = c.UshortToFloat(torqueRegisters5[1], torqueRegisters5[0]);
this.Invoke(new Action(() => uiTextBox29.Text = torqueValue5.ToString("F2")));
}
// 读取抗过载保持时间 (D-454) - 整数类型
ushort[] holdTimeRegisters5 = await System.Threading.Tasks.Task.Run(() =>
_modbusMaster?.ReadHoldingRegisters(1, 454, 1)
);
if (holdTimeRegisters5 != null && holdTimeRegisters5.Length >= 1)
{
int holdTimeValue5 = holdTimeRegisters5[0];
this.Invoke(new Action(() => uiTextBox28.Text = holdTimeValue5.ToString()));
}
}
catch (Exception ex)
{
this.Invoke(new Action(() =>
{
LogHelper.Info(ex?.Message);
LogHelper.Info(ex?.InnerException?.Message);
LogHelper.Info(ex?.InnerException?.ToString());
LogHelper.Info(ex?.StackTrace);
}
//MessageBox.Show($"自定义系数失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)
));
//MessageBox.Show($"自定义系数失败:{ex?.InnerException}");
//MessageBox.Show($"自定义系数失败:{ex?.StackTrace}");
}
}
private void ParameterSetting1_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 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 ParameterSetting1_FormClosing(object sender, FormClosingEventArgs e)
{
//停止定时器
_readTimer?.Stop();
_readTimer?.Dispose();
// 释放Modbus资源
ModbusResourceManager.Instance?.Dispose();
// 确保应用程序完全退出
Application.Exit();
}
private void ParameterSetting1_FormClosed(object sender, FormClosedEventArgs e)
{
//_ParameterSetting1?.Close();
_ParameterSetting2?.Close();
}
//正压阀、漏液
private void uiButton2_Click(object sender, EventArgs e)
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 4);
}
//正压阀、漏气
private void uiButton3_Click(object sender, EventArgs e)
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 4);
}
//负压阀
private void uiButton5_Click(object sender, EventArgs e)
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 5);
}
//负压泵
private void uiButton6_Click(object sender, EventArgs e)
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 7);
}
//阀
private void uiButton7_Click(object sender, EventArgs e)
{
ma?.BtnClickFunctionForNew(Function.ButtonType., 63);
}
//下一页
private void Btn_nextpage_Click(object sender, EventArgs e)
{
SwitchWindow(ref _ParameterSetting2, () => new ParameterSetting2());
}
//返回
private void Btn_return_Click(object sender, EventArgs e)
{
SwitchWindow(ref _DebugScreen, () => new DebugScreen());
}
private void uiTextBox1_Enter(object sender, EventArgs e)
{
}
private void uiTextBox1_ButtonClick(object sender, EventArgs e)
{
}
private void uiTextBox1_TextChanged(object sender, EventArgs e)
{
}
//漏液:装夹轴向力
private void uiTextBox1_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox1.Text, 400, Function.DataType.);
}
//漏液:装配扭矩
private void uiTextBox3_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox3.Text, 402, Function.DataType.);
}
//漏液:保持时间
private void uiTextBox4_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox4.Text, 404, Function.DataType.);
}
//漏液:测试水压
private void uiTextBox5_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox5.Text, 406, Function.DataType.);
}
//漏气:装夹轴向力
private void uiTextBox10_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox10.Text, 410, Function.DataType.);
}
//漏气:装配扭矩
private void uiTextBox8_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox8.Text, 412, Function.DataType.);
}
//漏气:保持时间
private void uiTextBox7_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox7.Text, 414, Function.DataType.);
}
//漏气:规定压力下限
private void uiTextBox6_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox6.Text, 416, Function.DataType., true, 1000, -100);
}
//分离力:装夹轴向力
private void uiTextBox15_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox15.Text, 420, Function.DataType.);
}
//分离力:装配扭矩
private void uiTextBox13_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox13.Text, 422, Function.DataType.);
}
//分离力:分离轴向力
private void uiTextBox12_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox12.Text, 426, Function.DataType., true, 0, -99);
}
//分离力:持续时间设置
private void uiTextBox11_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox11.Text, 424, Function.DataType.);
}
//分离力:速率设置
private void uiTextBox16_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox16.Text, 364, Function.DataType.);
}
//旋开扭矩:装夹轴向力
private void uiTextBox21_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox21.Text, 430, Function.DataType.);
}
//旋开扭矩:装配扭矩
private void uiTextBox19_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox19.Text, 432, Function.DataType.);
}
//旋开扭矩:持续时间设置
private void uiTextBox18_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox18.Text, 434, Function.DataType.);
}
//旋开扭矩:旋开扭矩
private void uiTextBox17_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox17.Text, 436, Function.DataType., true, 0, -99);
}
//易装配性:装夹轴向力
private void uiTextBox26_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox26.Text, 440, Function.DataType.);
}
//易装配性:装夹轴向力
private void uiTextBox24_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox24.Text, 442, Function.DataType.);
}
//抗过载:装夹轴向力
private void uiTextBox31_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox31.Text, 450, Function.DataType.);
}
//抗过载:装配扭矩
private void uiTextBox29_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox29.Text, 452, Function.DataType.);
}
//抗过载:保持时间
private void uiTextBox28_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox28.Text, 454, Function.DataType.);
}
//易装配性:保持时间
private void uiTextBox22_Click(object sender, EventArgs e)
{
ma?.WriteToPLCForNew(uiTextBox22.Text, 444, Function.DataType.);
}
}
}