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

286 lines
9.1 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 TestScreen : UIForm
{
TestScreen testScreen;
private main _main;
private TestScreen _TestScreen;
private DebugScreen _DebugScreen;
private CoefficientSetting _CoefficientSetting;
private WeepingTest _WeepingTest;
private LeakageTest _LeakageTest;
private SeparatTest _SeparartTest;
private UnsrewtorqueTest _UnsrewtorqueTest;
private EasyassemblyTest _EasyassembleTest;
private OverloadresTest _OverloadresTest;
private StresscrackTest _StresscrackTest;
private UncrewseptorqueTest _UncrewseptorqueTest;
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
Function ma;
DataChange c;
Timer timeTimer;
public TestScreen()
{
InitializeComponent();
InitTimer();
InitComboBoxPattern();
}
private void InitTimer()
{
Timer timeTimer = new Timer();
timeTimer.Interval = 300;
timeTimer.Tick += TimeTimer_Tick;
timeTimer.Start();
}
private void TimeTimer_Tick(object sender, EventArgs e)
{
Lab_time.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
private void TestScreen_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();
ushort[] value = _modbusMaster.ReadHoldingRegisters(1, 50, 1);
Combox_pattern.SelectedIndex = value[0];
}
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.停止当前窗口的定时器(不释放资源)
timeTimer?.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_home_Click(object sender, EventArgs e)
{
SwitchWindow(ref _main, () => new main());
}
//测试实验
private void Btn_testscreen_Click(object sender, EventArgs e)
{
}
private void Btn_debug_Click(object sender, EventArgs e)
{
SwitchWindow(ref _DebugScreen, () => new DebugScreen());
}
private void uiButton1_Click(object sender, EventArgs e)
{
SwitchWindow(ref _CoefficientSetting, () => new CoefficientSetting());
}
private void Btn_weepingtest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 30, true);
//ma?.BtnClickFunctionForNew(Function.ButtonType.置位型 ,30);
SwitchWindow(ref _WeepingTest, () => new WeepingTest());
}
private void Btn_leakagetest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 31, true);
SwitchWindow(ref _LeakageTest, () => new LeakageTest());
}
private void Btn_separattest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 32, true);
SwitchWindow(ref _SeparartTest, () => new SeparatTest());
}
private void Btn_unscrewsetorquetest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 33, true);
SwitchWindow(ref _UnsrewtorqueTest, () => new UnsrewtorqueTest());
}
private void Btn_easyassemblytest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 34, true);
SwitchWindow(ref _EasyassembleTest, () => new EasyassemblyTest());
}
private void Btn_overloadrestest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 35, true);
SwitchWindow(ref _OverloadresTest, () => new OverloadresTest());
}
private void Btn_stresscracktest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 36, true);
SwitchWindow(ref _StresscrackTest, () => new StresscrackTest());
}
private void Btn_Uncrewseptorquetest_Click(object sender, EventArgs e)
{
_modbusMaster.WriteSingleCoil(1, 37, true);
SwitchWindow(ref _UncrewseptorqueTest, () => new UncrewseptorqueTest());
}
private void TestScreen_FormClosed(object sender, FormClosedEventArgs e)
{
_TestScreen?.Close();
}
private void TestScreen_FormClosing(object sender, FormClosingEventArgs e)
{
// 停止定时器
timeTimer?.Stop();
timeTimer?.Dispose(); // 释放主定时器
// 释放Modbus资源
ModbusResourceManager.Instance?.Dispose();
// 确保应用程序完全退出
Application.Exit();
}
private void InitComboBoxPattern()
{
// 清空原有选项(可选,避免重复添加)
//Combox_pattern.Items.Clear();
// 定义需要添加的选项数组
string[] options = {
"GB/T 1962.1",
"GB/T 1962.2",
"YY/T 0916.20",
"ISO 80369",
"自定义"
};
Combox_pattern.Items.AddRange(options);
if (Combox_pattern.Items.Count > 0)
{
//Combox_pattern.SelectedIndex = 0;
}
}
private void Combox_pattern_SelectedIndexChanged(object sender, EventArgs e)
{
ushort selectedIndex = ushort.Parse(Combox_pattern.SelectedIndex.ToString());
_modbusMaster?.WriteSingleRegister(1, 50, selectedIndex);
//switch (selectedIndex)
//{
// case 0:
// Combox_pattern.Text = "GB/T 1962.1";
// //break;
// case 1:
// //Combox_pattern.Text = "GB/T 1962.2";
// break;
// case 2:
// //Combox_pattern.Text = "YY/T 0916.20";
// break;
// case 3:
// //Combox_pattern.Text = "ISO 80369";
// break;
// case 4:
// //Combox_pattern.Text = "自定义";
// break;
//}
}
}
}