This commit is contained in:
@@ -47,6 +47,8 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
uiButton1 = new UIButton();
|
||||
uiComboBox1 = new UIComboBox();
|
||||
uiLabel6 = new UILabel();
|
||||
uiTextBox5 = new UITextBox();
|
||||
uiLabel2 = new UILabel();
|
||||
uiTableLayoutPanel1.SuspendLayout();
|
||||
uiPanel1.SuspendLayout();
|
||||
uiGroupBox1.SuspendLayout();
|
||||
@@ -132,6 +134,8 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
//
|
||||
// uiPanel2
|
||||
//
|
||||
uiPanel2.Controls.Add(uiTextBox5);
|
||||
uiPanel2.Controls.Add(uiLabel2);
|
||||
uiPanel2.Controls.Add(uiLabel33);
|
||||
uiPanel2.Controls.Add(uiLabel34);
|
||||
uiPanel2.Controls.Add(uiButton4);
|
||||
@@ -372,6 +376,38 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
uiLabel6.Text = "🌬️ 氧浓度环境:";
|
||||
uiLabel6.TextAlign = ContentAlignment.MiddleLeft;
|
||||
//
|
||||
// uiTextBox5
|
||||
//
|
||||
uiTextBox5.Font = new Font("微软雅黑", 13F, FontStyle.Regular, GraphicsUnit.Point, 134);
|
||||
uiTextBox5.Location = new Point(313, 509);
|
||||
uiTextBox5.Margin = new Padding(4, 5, 4, 5);
|
||||
uiTextBox5.MinimumSize = new Size(1, 16);
|
||||
uiTextBox5.Name = "uiTextBox5";
|
||||
uiTextBox5.Padding = new Padding(10);
|
||||
uiTextBox5.Radius = 6;
|
||||
uiTextBox5.RectColor = Color.FromArgb(220, 223, 230);
|
||||
uiTextBox5.ShowText = false;
|
||||
uiTextBox5.Size = new Size(250, 45);
|
||||
uiTextBox5.Style = UIStyle.Custom;
|
||||
uiTextBox5.StyleCustomMode = true;
|
||||
uiTextBox5.TabIndex = 35;
|
||||
uiTextBox5.Text = "0.00";
|
||||
uiTextBox5.TextAlignment = ContentAlignment.MiddleLeft;
|
||||
uiTextBox5.Watermark = "请输入时间";
|
||||
//
|
||||
// uiLabel2
|
||||
//
|
||||
uiLabel2.Font = new Font("微软雅黑", 13F, FontStyle.Bold, GraphicsUnit.Point, 134);
|
||||
uiLabel2.ForeColor = Color.FromArgb(48, 49, 51);
|
||||
uiLabel2.Location = new Point(100, 509);
|
||||
uiLabel2.Name = "uiLabel2";
|
||||
uiLabel2.Size = new Size(211, 45);
|
||||
uiLabel2.Style = UIStyle.Custom;
|
||||
uiLabel2.StyleCustomMode = true;
|
||||
uiLabel2.TabIndex = 34;
|
||||
uiLabel2.Text = "功率:";
|
||||
uiLabel2.TextAlign = ContentAlignment.MiddleLeft;
|
||||
//
|
||||
// ManualDebugForm
|
||||
//
|
||||
AutoScaleMode = AutoScaleMode.None;
|
||||
@@ -415,5 +451,7 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
private UIButton uiButton5;
|
||||
private UILabel uiLabel33;
|
||||
private UILabel uiLabel34;
|
||||
private UITextBox uiTextBox5;
|
||||
private UILabel uiLabel2;
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,8 @@ using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Globalization;
|
||||
using System.IO.Ports;
|
||||
using System.Linq;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
@@ -73,7 +75,7 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
|
||||
if (Ray != null && Ray.Length >= 2)
|
||||
{
|
||||
float Value = c.UshortToFloat(Ray[1],Ray[0]);
|
||||
float Value = c.UshortToFloat(Ray[1], Ray[0]);
|
||||
this.Invoke(new Action(() => uiTextBox1.Text = Value.ToString("F1")));
|
||||
}
|
||||
}
|
||||
@@ -166,6 +168,8 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
// 当子窗体关闭时,显示主窗体
|
||||
Application.OpenForms["MainForm"]?.Show();
|
||||
};
|
||||
|
||||
initPort();
|
||||
}
|
||||
|
||||
private void InitComboBoxPattern()
|
||||
@@ -195,6 +199,8 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
private void uiButton5_Click(object sender, EventArgs e)
|
||||
@@ -241,5 +247,223 @@ namespace 外科辅料和患者防护罩激光抗性测试仪
|
||||
_readTimer.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private string _serialBuffer = "";
|
||||
SerialPort serialPort = null;
|
||||
private System.Windows.Forms.Timer _powerMeterTimer;
|
||||
private readonly object _serialLock = new object();
|
||||
private bool _isSerialPortAvailable = false;
|
||||
private void initPort()
|
||||
{
|
||||
|
||||
lock (_serialLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 创建串口对象
|
||||
serialPort = new SerialPort();
|
||||
serialPort.PortName = "COM2";
|
||||
serialPort.BaudRate = 38400;
|
||||
serialPort.DataBits = 8;
|
||||
serialPort.Parity = Parity.None;
|
||||
serialPort.StopBits = StopBits.One;
|
||||
serialPort.NewLine = "\r\n";
|
||||
|
||||
// 设置数据接收事件
|
||||
serialPort.DataReceived += SerialPort_DataReceived;
|
||||
|
||||
serialPort.Open();
|
||||
|
||||
_isSerialPortAvailable = true;
|
||||
|
||||
_powerMeterTimer = new System.Windows.Forms.Timer();
|
||||
_powerMeterTimer.Interval = 200; // 100ms读取一次
|
||||
_powerMeterTimer.Tick += PowerMeterTimer_Tick;
|
||||
_powerMeterTimer.Start();
|
||||
|
||||
|
||||
//SendCommand("S01");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"串口连接失败:{ex.Message}");
|
||||
_isSerialPortAvailable = false;
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 串口数据接收事件
|
||||
private void SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (serialPort == null || !serialPort.IsOpen) return;
|
||||
|
||||
string data = serialPort.ReadExisting();
|
||||
_serialBuffer += data; // 添加到缓冲区
|
||||
|
||||
// 检查是否有完整的行(以\r\n结尾)
|
||||
int newLineIndex;
|
||||
while ((newLineIndex = _serialBuffer.IndexOf("\r\n")) >= 0)
|
||||
{
|
||||
// 提取一行完整数据
|
||||
string line = _serialBuffer.Substring(0, newLineIndex);
|
||||
|
||||
// 移除已处理的部分
|
||||
_serialBuffer = _serialBuffer.Substring(newLineIndex + 2);
|
||||
|
||||
if (!string.IsNullOrEmpty(line))
|
||||
{
|
||||
this.Invoke(new Action(() =>
|
||||
{
|
||||
ProcessPowerMeterData(line);
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"串口数据接收错误: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private void ProcessPowerMeterData(string data)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(data)) return;
|
||||
|
||||
string trimmedLine = data.Trim();
|
||||
Debug.WriteLine($"处理完整数据行: '{trimmedLine}'");
|
||||
|
||||
// 处理功率值(浮点数)
|
||||
if (double.TryParse(trimmedLine, NumberStyles.Float, CultureInfo.InvariantCulture, out double powerValue))
|
||||
{
|
||||
if (powerValue < 1000) // 功率值通常小于1000W
|
||||
{
|
||||
uiTextBox5.Text = $"{powerValue:F3} W";
|
||||
_waitingResponse = false; // 收到响应,可以发送下一个命令
|
||||
}
|
||||
}
|
||||
|
||||
// S01命令的响应可能是空的,但我们也需要处理
|
||||
else if (string.IsNullOrEmpty(trimmedLine))
|
||||
{
|
||||
Debug.WriteLine("收到空响应(可能是S01的响应)");
|
||||
_waitingResponse = false; // 收到响应
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"处理功率计数据错误: {ex.Message}");
|
||||
_waitingResponse = false; // 出错也重置标志
|
||||
}
|
||||
}
|
||||
|
||||
private int _commandCounter = 0;
|
||||
private bool _waitingResponse = false; // 添加这个变量
|
||||
|
||||
private void PowerMeterTimer_Tick(object sender, EventArgs e)
|
||||
{
|
||||
if (_isSerialPortAvailable && serialPort != null && serialPort.IsOpen && !_waitingResponse)
|
||||
{
|
||||
if (_commandCounter == 0)
|
||||
{
|
||||
SendCommand("GD"); // 读取功率
|
||||
}
|
||||
else if (_commandCounter == 1)
|
||||
{
|
||||
SendCommand("GSC0"); // 读取校准系数
|
||||
}
|
||||
|
||||
_waitingResponse = true; // 标记为等待响应
|
||||
_commandCounter++;
|
||||
if (_commandCounter >= 2) _commandCounter = 0; // 2个命令循环(去掉了S01)
|
||||
}
|
||||
}
|
||||
// 发送命令到功率计
|
||||
private void SendCommand(string command)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (serialPort == null || !serialPort.IsOpen) return;
|
||||
|
||||
lock (_serialLock)
|
||||
{
|
||||
string fullCommand = command.ToUpper() + "\r\n";
|
||||
serialPort.Write(fullCommand);
|
||||
Debug.WriteLine($"发送命令: {command}");
|
||||
|
||||
// 超时保护(2秒)
|
||||
Task.Delay(2000).ContinueWith(t =>
|
||||
{
|
||||
if (_waitingResponse)
|
||||
{
|
||||
Debug.WriteLine($"命令超时重置: {command}");
|
||||
_waitingResponse = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Debug.WriteLine($"发送命令失败: {ex.Message}");
|
||||
_waitingResponse = false; // 出错时重置
|
||||
}
|
||||
}
|
||||
|
||||
private void Disconnect()
|
||||
{
|
||||
_isSerialPortAvailable = false;
|
||||
|
||||
if (_powerMeterTimer != null)
|
||||
{
|
||||
_powerMeterTimer.Stop();
|
||||
_powerMeterTimer.Dispose();
|
||||
_powerMeterTimer = null;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (serialPort != null && serialPort.IsOpen)
|
||||
{
|
||||
SendCommand("S00"); // 停止连续输出
|
||||
}
|
||||
}
|
||||
catch { }
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
lock (_serialLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (serialPort != null)
|
||||
{
|
||||
if (serialPort.IsOpen)
|
||||
{
|
||||
serialPort.Close();
|
||||
}
|
||||
serialPort.Dispose();
|
||||
serialPort = null;
|
||||
}
|
||||
//WriteToLog("功率计已断开连接");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//WriteToLog($"断开连接异常:{ex.Message}");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user