228 lines
6.8 KiB
C#
228 lines
6.8 KiB
C#
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.Linq;
|
||
using System.Net.Sockets;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows.Forms;
|
||
using 外科辅料和患者防护罩激光抗性测试仪.Data;
|
||
|
||
namespace 外科辅料和患者防护罩激光抗性测试仪
|
||
{
|
||
public partial class ManualDebugForm : 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;
|
||
private bool _isManualInput = false;
|
||
|
||
public ManualDebugForm()
|
||
{
|
||
InitializeComponent();
|
||
|
||
InitComboBoxPattern();
|
||
|
||
InitTimer();
|
||
}
|
||
|
||
private System.Windows.Forms.Timer InitTimer()
|
||
{
|
||
var timer = new System.Windows.Forms.Timer()
|
||
{
|
||
Interval = 1000
|
||
};
|
||
timer.Tick += async (s, e) =>
|
||
{
|
||
if (!_isManualInput && _modbusMaster != null)
|
||
{
|
||
try
|
||
{
|
||
await ReadTestParametersAsync();
|
||
}
|
||
catch { }
|
||
}
|
||
};
|
||
//timer.Start();
|
||
return timer;
|
||
}
|
||
|
||
private async System.Threading.Tasks.Task ReadTestParametersAsync()
|
||
{
|
||
if (_tcpClient == null || !_tcpClient.Connected || _modbusMaster == null)
|
||
{
|
||
return;
|
||
}
|
||
|
||
try
|
||
{
|
||
ushort[] Ray = await System.Threading.Tasks.Task.Run(() =>
|
||
_modbusMaster?.ReadHoldingRegisters(1, 250, 2)
|
||
);
|
||
|
||
if (Ray != null && Ray.Length >= 2)
|
||
{
|
||
float Value = c.UshortToFloat(Ray[1],Ray[0]);
|
||
this.Invoke(new Action(() => uiTextBox1.Text = Value.ToString("F1")));
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
this.Invoke(new Action(() =>
|
||
Debug.WriteLine($"读取系数失败:{ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error)));
|
||
}
|
||
|
||
try
|
||
{
|
||
bool[] registers1 = await Task.Run(async () =>
|
||
{
|
||
if (_modbusMaster == null)
|
||
return null;
|
||
return await _modbusMaster?.ReadCoilsAsync(1, 20, 1);
|
||
});
|
||
if (registers1 != null && registers1.Length >= 1)
|
||
{
|
||
bool value = registers1[0];
|
||
this.Invoke(new Action(() =>
|
||
{
|
||
if (value)
|
||
{
|
||
uiLight3.State = UILightState.On;
|
||
|
||
|
||
}
|
||
else
|
||
{
|
||
uiLight3.State = UILightState.Off;
|
||
}
|
||
}));
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Debug.WriteLine($"读取状态失败: {ex.Message}");
|
||
}
|
||
}
|
||
|
||
|
||
|
||
private void ManualDebugForm_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 ManualDebugForm_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 uiButton5_Click(object sender, EventArgs e)
|
||
{
|
||
this.Close();//返回主页面
|
||
}
|
||
|
||
private void uiButton1_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 101);//混氧开关
|
||
}
|
||
|
||
private void uiButton4_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 250);//光斑开关
|
||
}
|
||
|
||
private void uiButton2_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 11);//灯开关
|
||
}
|
||
|
||
private void uiButton3_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.BtnClickFunctionForNew(Function.ButtonType.切换型, 99);//灭火
|
||
}
|
||
|
||
private void uiComboBox1_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
ushort selectedIndex = ushort.Parse(uiComboBox1.SelectedIndex.ToString());
|
||
_modbusMaster?.WriteSingleRegister(1, 10, selectedIndex);//下拉框
|
||
}
|
||
|
||
private void uiTextBox1_Click(object sender, EventArgs e)
|
||
{
|
||
ma?.WriteToPLCForNew(uiTextBox1.Text.Trim(), 250, Function.DataType.浮点型);
|
||
}
|
||
|
||
private void ManualDebugForm_FormClosing(object sender, FormClosingEventArgs e)
|
||
{
|
||
if (_readTimer != null)
|
||
{
|
||
_readTimer.Stop();
|
||
_readTimer.Dispose();
|
||
}
|
||
}
|
||
}
|
||
}
|