471 lines
17 KiB
C#
471 lines
17 KiB
C#
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 DebugScreen : UIForm
|
||
{
|
||
private main _main;
|
||
private TestScreen _testScreen;
|
||
private DebugScreen _debugScreen;
|
||
private ParameterSetting1 _ParameterSetting1;
|
||
private ParameterSetting2 _ParameterSetting2;
|
||
|
||
DataChange c;
|
||
Timer Timer;
|
||
private readonly System.Timers.Timer _readTimer;
|
||
private bool _isManualInput = false; // 手动输入标记
|
||
|
||
private Timer _longPressTimer;
|
||
private int _currentButtonId = -1;
|
||
// 记录触摸按下的起始时间,用于区分单击和长按(可选)
|
||
private DateTime _touchDownTime;
|
||
// 长按判定阈值(毫秒),可调整
|
||
private const int LongPressThreshold = 200;
|
||
|
||
private TcpClient _tcpClient => ModbusResourceManager.Instance.TcpClient;
|
||
private IModbusMaster _modbusMaster => ModbusResourceManager.Instance.ModbusMaster;
|
||
Function ma;
|
||
|
||
public DebugScreen()
|
||
{
|
||
InitializeComponent();
|
||
InitTimer();
|
||
|
||
_longPressTimer = new Timer();
|
||
_longPressTimer.Interval = 100; // 长按持续触发间隔
|
||
_longPressTimer.Tick += LongPressTimer_Tick;
|
||
|
||
// 绑定适配触摸的按钮事件
|
||
BindTouchCompatibleEvents(uiButton1, 10);
|
||
BindTouchCompatibleEvents(uiButton6, 11);
|
||
BindTouchCompatibleEvents(uiButton3, 12);
|
||
BindTouchCompatibleEvents(uiButton5, 13);
|
||
}
|
||
|
||
private void BindTouchCompatibleEvents(UIButton button, int buttonId)
|
||
{
|
||
// 核心:使用 MouseDown/MouseUp(触摸会自动触发),并优化触摸体验
|
||
button.MouseDown += (sender, e) =>
|
||
{
|
||
if (e.Button == MouseButtons.Left)
|
||
{
|
||
_touchDownTime = DateTime.Now;
|
||
_currentButtonId = buttonId;
|
||
|
||
// 触摸/鼠标按下时,先启动一个短延迟再触发定时器(避免单击误触发长按)
|
||
var delayTimer = new Timer { Interval = LongPressThreshold };
|
||
delayTimer.Tick += (s, args) =>
|
||
{
|
||
delayTimer.Stop();
|
||
delayTimer.Dispose();
|
||
// 判定为长按,启动持续触发
|
||
_longPressTimer.Start();
|
||
};
|
||
delayTimer.Start();
|
||
|
||
}
|
||
};
|
||
|
||
// 触摸抬起/离开均停止触发
|
||
button.MouseUp += (sender, e) => StopLongPress();
|
||
button.MouseLeave += (sender, e) => StopLongPress();
|
||
|
||
// 额外:屏蔽默认的Click事件重复触发(触摸可能导致Click和MouseDown双重触发)
|
||
button.Click += (sender, e) =>
|
||
{
|
||
// 只有按下时间短于长按阈值,才执行Click(避免重复)
|
||
var pressDuration = (DateTime.Now - _touchDownTime).TotalMilliseconds;
|
||
if (pressDuration < LongPressThreshold)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 10);
|
||
}
|
||
};
|
||
}
|
||
|
||
/// <summary>
|
||
/// 停止长按触发的统一方法
|
||
/// </summary>
|
||
private void StopLongPress()
|
||
{
|
||
if (_currentButtonId == 10)
|
||
{
|
||
_modbusMaster?.WriteSingleCoil(1, 10, false);
|
||
}
|
||
if (_currentButtonId == 11)
|
||
{
|
||
_modbusMaster?.WriteSingleCoil(1, 11, false);
|
||
}
|
||
if (_currentButtonId == 12)
|
||
{
|
||
_modbusMaster?.WriteSingleCoil(1, 12, false);
|
||
}
|
||
if (_currentButtonId == 13)
|
||
{
|
||
_modbusMaster?.WriteSingleCoil(1, 13, false);
|
||
}
|
||
_longPressTimer.Stop();
|
||
_currentButtonId = -1;
|
||
}
|
||
|
||
private void LongPressTimer_Tick(object sender, EventArgs e)
|
||
{
|
||
if (_currentButtonId != -1)
|
||
{
|
||
if (_currentButtonId == 10)
|
||
_modbusMaster?.WriteSingleCoil(1, 10, true);
|
||
if (_currentButtonId == 11)
|
||
_modbusMaster?.WriteSingleCoil(1, 11, true);
|
||
if (_currentButtonId == 12)
|
||
_modbusMaster?.WriteSingleCoil(1, 12, true);
|
||
if (_currentButtonId == 13)
|
||
_modbusMaster?.WriteSingleCoil(1, 13, true);
|
||
|
||
}
|
||
}
|
||
|
||
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
|
||
{
|
||
//时间显示
|
||
Lab_time.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
||
//装夹轴向力D
|
||
ushort[] force = _modbusMaster?.ReadHoldingRegisters(1, 1130, 2);
|
||
var value = c.UshortToFloat(force[1], force[0]);
|
||
lb_axclampforce.Text = value.ToString("F2");
|
||
//测试水压
|
||
ushort[] testwatpressure = _modbusMaster?.ReadHoldingRegisters(1, 1230, 2);
|
||
var value1 = c.UshortToFloat(testwatpressure[1], testwatpressure[0]);
|
||
lb_testwaterpressure.Text = value1.ToString("F1");
|
||
//装配扭矩
|
||
ushort[] assemblytorque = _modbusMaster?.ReadHoldingRegisters(1, 1180, 2);
|
||
var value2 = c.UshortToFloat(assemblytorque[1], assemblytorque[0]);
|
||
lb_assemblytorque.Text = value2.ToString("F2");
|
||
//当前位置
|
||
ushort[] currentlocation1 = _modbusMaster?.ReadHoldingRegisters(1, 42, 2);
|
||
var value3 = c.UshortToFloat(currentlocation1[1], currentlocation1[0]);
|
||
lb_currentlocation1.Text = value3.ToString("F2");
|
||
ushort[] currentlocation2 = _modbusMaster?.ReadHoldingRegisters(1, 46, 2);
|
||
var value4 = c.UshortToFloat(currentlocation2[1], currentlocation2[0]);
|
||
lb_currentlocation2.Text = value4.ToString("F1");
|
||
|
||
//复位灯
|
||
bool[] resetStatus = _modbusMaster?.ReadCoils(1, 92, 1);
|
||
if (resetStatus[0])
|
||
{
|
||
uiLight1.OnColor = System.Drawing.Color.Green;
|
||
}
|
||
|
||
else
|
||
{
|
||
uiLight1.OnColor = System.Drawing.Color.Red;
|
||
}
|
||
|
||
|
||
|
||
// 读取位置设置 (D-300)
|
||
ushort[] axialForceRegisters = await System.Threading.Tasks.Task.Run(() =>
|
||
_modbusMaster?.ReadHoldingRegisters(1, 300, 2)
|
||
);
|
||
|
||
if (axialForceRegisters != null && axialForceRegisters.Length >= 2)
|
||
{
|
||
float axialForceValue = c.UshortToFloat(axialForceRegisters[1], axialForceRegisters[0]);
|
||
this.Invoke(new Action(() => tb_locationset.Text = axialForceValue.ToString("F3")));
|
||
}
|
||
|
||
// 读取速度设置 (D-302)
|
||
ushort[] torqueRegisters = await System.Threading.Tasks.Task.Run(() =>
|
||
_modbusMaster?.ReadHoldingRegisters(1, 302, 2)
|
||
);
|
||
|
||
if (torqueRegisters != null && torqueRegisters.Length >= 2)
|
||
{
|
||
float torqueValue = c.UshortToFloat(torqueRegisters[1], torqueRegisters[0]);
|
||
this.Invoke(new Action(() => tb_speedset.Text = torqueValue.ToString("F3")));
|
||
}
|
||
|
||
// 读取滑台参数1 (D-310)
|
||
ushort[] holdTimeRegisters = await System.Threading.Tasks.Task.Run(() =>
|
||
_modbusMaster?.ReadHoldingRegisters(1, 310, 2)
|
||
);
|
||
|
||
if (holdTimeRegisters != null && holdTimeRegisters.Length >= 1)
|
||
{
|
||
float holdTimeValue = c.UshortToFloat(holdTimeRegisters[1], holdTimeRegisters[0]);
|
||
this.Invoke(new Action(() => tb_slipwaypara1.Text = holdTimeValue.ToString("F1")));
|
||
}
|
||
|
||
// 读取滑台参数2 (D-312)
|
||
ushort[] testPressureRegisters = await System.Threading.Tasks.Task.Run(() =>
|
||
_modbusMaster?.ReadHoldingRegisters(1, 312, 2)
|
||
);
|
||
|
||
if (testPressureRegisters != null && testPressureRegisters.Length >= 2)
|
||
{
|
||
float testPressureValue = c.UshortToFloat(testPressureRegisters[1], testPressureRegisters[0]);
|
||
this.Invoke(new Action(() => tb_slipwaypara2.Text = testPressureValue.ToString("F3")));
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
MessageBox.Show($"读取调试参数失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
||
}
|
||
}
|
||
|
||
private void uiTableLayoutPanel2_Paint(object sender, PaintEventArgs e)
|
||
{
|
||
|
||
}
|
||
|
||
private void DebugScreen_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 DebugScreen_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
// 停止定时器
|
||
_readTimer?.Stop();
|
||
_readTimer?.Dispose();
|
||
_longPressTimer?.Stop();
|
||
_longPressTimer?.Dispose(); // 释放长按定时器
|
||
|
||
// 释放Modbus资源
|
||
ModbusResourceManager.Instance?.Dispose();
|
||
|
||
// 确保应用程序完全退出
|
||
Application.Exit();
|
||
}
|
||
|
||
private void DebugScreen_FormClosed(object sender, FormClosedEventArgs e)
|
||
{
|
||
_main?.Close();
|
||
_testScreen?.Close();
|
||
//_debugScreen?.Close();
|
||
_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 uiButton1_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 10);
|
||
}
|
||
|
||
//反转
|
||
private void uiButton6_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 11);
|
||
}
|
||
|
||
//前进
|
||
private void uiButton3_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 12);
|
||
}
|
||
|
||
//后退
|
||
private void uiButton5_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 13);
|
||
}
|
||
|
||
//复位
|
||
private void uiButton2_Click(object sender, EventArgs e)
|
||
{
|
||
ma.BtnClickFunctionForNew(Function.ButtonType.切换型, 95);
|
||
bool result = UIMessageBox.ShowAsk("确保夹具已松开!", true, UIMessageDialogButtons.Ok);
|
||
if (result)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 90);
|
||
}
|
||
else
|
||
{
|
||
ma.BtnClickFunctionForNew(Function.ButtonType.切换型, 95);
|
||
}
|
||
}
|
||
|
||
//自定义测试参数
|
||
private void uiButton4_Click(object sender, EventArgs e)
|
||
{
|
||
SwitchWindow(ref _ParameterSetting1, () => new ParameterSetting1());
|
||
}
|
||
|
||
//轴向力零点标定
|
||
private void uiButton7_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 1100);
|
||
}
|
||
|
||
//压力零点标定
|
||
private void uiButton8_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 1102);
|
||
}
|
||
|
||
//扭矩零点标定
|
||
private void uiButton9_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.复归型, 1101);
|
||
}
|
||
|
||
//主页
|
||
private void Btn_home_Click(object sender, EventArgs e)
|
||
{
|
||
SwitchWindow(ref _main, () => new main());
|
||
}
|
||
|
||
//测试
|
||
private void Btn_testscreen_Click(object sender, EventArgs e)
|
||
{
|
||
SwitchWindow(ref _testScreen, () => new TestScreen());
|
||
}
|
||
|
||
//调试
|
||
private void Btn_debug_Click(object sender, EventArgs e)
|
||
{
|
||
SwitchWindow(ref _debugScreen, () => new DebugScreen());
|
||
}
|
||
|
||
//位置设置
|
||
private void tb_locationset_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.WriteToPLCForNew(tb_locationset.Text, 300, Function.DataType.浮点型,3);
|
||
}
|
||
//速度设置
|
||
private void tb_speedset_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.WriteToPLCForNew(tb_speedset.Text, 302, Function.DataType.浮点型,3);
|
||
}
|
||
//滑台设置1
|
||
private void tb_slipwaypara1_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.WriteToPLCForNew(tb_slipwaypara1.Text, 310, Function.DataType.浮点型);
|
||
}
|
||
//滑台设置2
|
||
private void tb_slipwaypara2_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.WriteToPLCForNew(tb_slipwaypara2.Text, 312, Function.DataType.浮点型,3);
|
||
}
|
||
}
|
||
}
|