|
|
@@ -175,6 +175,10 @@ namespace FileWatcher
|
|
|
|
|
|
private bool canRecordData = true; // 初始为true,允许第一次记录
|
|
|
private bool currentWasZero = true; // 初始状态假设电流为0
|
|
|
+
|
|
|
+ private bool isWaitingForStableData = false;
|
|
|
+ private DateTime currentDetectionTime;
|
|
|
+ private string pendingData = "";
|
|
|
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
|
{
|
|
|
try
|
|
|
@@ -182,61 +186,90 @@ namespace FileWatcher
|
|
|
int len = serialPort1.BytesToRead;
|
|
|
Byte[] readBuffer = new Byte[len];
|
|
|
serialPort1.Read(readBuffer, 0, len);
|
|
|
- data = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
|
|
|
+ string rawData = BitConverter.ToString(readBuffer, 0, readBuffer.Length).Replace("-", "");
|
|
|
|
|
|
- if (data.Length > 48)
|
|
|
- {
|
|
|
- data = data.Substring(0, 48);
|
|
|
- }
|
|
|
+ // 保存原始数据用于后续处理
|
|
|
+ pendingData = rawData.Length > 48 ? rawData.Substring(0, 48) : rawData;
|
|
|
|
|
|
- if (data.Length == 48)
|
|
|
+ if (pendingData.Length == 48)
|
|
|
{
|
|
|
- float 电压数据 = getdata(data.Substring(6, 8));
|
|
|
- float 电流数据 = getdata(data.Substring(14, 8));
|
|
|
- float 功率数据 = getdata(data.Substring(22, 8));
|
|
|
- float 频率数据 = getdata(data.Substring(30, 8));
|
|
|
- float 功率因素 = getdata(data.Substring(38, 8));
|
|
|
-
|
|
|
- // 检查电流状态变化
|
|
|
- if (电流数据 == 0)
|
|
|
+ float 电流数据 = getdata(pendingData.Substring(14, 8));
|
|
|
+
|
|
|
+ // 如果检测到有电流且不在等待状态
|
|
|
+ if (电流数据 != 0 && !isWaitingForStableData)
|
|
|
{
|
|
|
- currentWasZero = true;
|
|
|
- canRecordData = true; // 电流归零后,允许下次记录
|
|
|
- OperateResult.AppendText("电流归零,准备下次记录\n");
|
|
|
+ OperateResult.AppendText($"检测到电流({电流数据}A),等待2秒稳定...\n");
|
|
|
+ isWaitingForStableData = true;
|
|
|
+ currentDetectionTime = DateTime.Now;
|
|
|
+
|
|
|
+ // 启动定时器检查是否已等待足够时间
|
|
|
+ System.Windows.Forms.Timer delayTimer = new System.Windows.Forms.Timer();
|
|
|
+ delayTimer.Interval = 2000; // 2秒
|
|
|
+ delayTimer.Tick += (s, args) =>
|
|
|
+ {
|
|
|
+ delayTimer.Stop();
|
|
|
+ ProcessStableData();
|
|
|
+ };
|
|
|
+ delayTimer.Start();
|
|
|
}
|
|
|
- else if (电流数据 != 0 && currentWasZero && canRecordData)
|
|
|
+ else if (电流数据 == 0)
|
|
|
+ {
|
|
|
+ isWaitingForStableData = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ OperateResult.AppendText($"处理数据时出错: {ex.Message}\n");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void ProcessStableData()
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (pendingData.Length == 48)
|
|
|
+ {
|
|
|
+ float 电压数据 = getdata(pendingData.Substring(6, 8));
|
|
|
+ float 电流数据 = getdata(pendingData.Substring(14, 8));
|
|
|
+ float 功率数据 = getdata(pendingData.Substring(22, 8));
|
|
|
+ float 频率数据 = getdata(pendingData.Substring(30, 8));
|
|
|
+ float 功率因素 = getdata(pendingData.Substring(38, 8));
|
|
|
+
|
|
|
+ if (电流数据 != 0) // 再次确认电流仍然存在
|
|
|
{
|
|
|
- // 电流从0变为非0,且允许记录
|
|
|
double num, num1;
|
|
|
if (double.TryParse(FunctionNum.Text, out num) &&
|
|
|
double.TryParse(FunctionNum1.Text, out num1))
|
|
|
{
|
|
|
if (功率数据 > num && 功率数据 < num1)
|
|
|
{
|
|
|
- OperateResult.AppendText("检测到有效电流,记录数据,功率[" + 功率数据 + "]\n");
|
|
|
+ OperateResult.AppendText($"数据稳定,记录: 电压={电压数据}V, 电流={电流数据}A, 功率={功率数据}W\n");
|
|
|
dh.ExecuteSql("insert into currenttest(CT_ID,ct_linecode, CT_MAKECODE, CT_VOLTAGE, CT_CURRENT, CT_POWER, CT_FREQUENCY, CT_POWER1) " +
|
|
|
- "VALUES(currenttest_SEQ.NEXTVAL,'" + li_code.Text + "','" + ma_code.Text + "','" + 电压数据 + "','" + 电流数据 + "','" + 功率数据 + "','" + 频率数据 + "','" + 功率因素 + "')", "insert");
|
|
|
+ "VALUES(currenttest_SEQ.NEXTVAL,'" + li_code.Text + "','" + ma_code.Text + "','" + 电压数据 + "','" + 电流数据 + "','" + 功率数据 + "','" + 频率数据 + "','" + 功率因素 + "')", "insert");
|
|
|
NowQTY.Text = dh.getRowCount("currenttest", "ct_makecode='" + ma_code.Text + "'").ToString();
|
|
|
-
|
|
|
- // 记录后禁止再次记录,直到电流归零
|
|
|
- canRecordData = false;
|
|
|
- currentWasZero = false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- OperateResult.AppendText("功率数据异常\n");
|
|
|
+ OperateResult.AppendText($"功率数据异常: {功率数据}W (应在{num}-{num1}W之间)\n");
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- OperateResult.AppendText("请填写功率标准值\n");
|
|
|
+ OperateResult.AppendText("请正确设置功率标准值\n");
|
|
|
}
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OperateResult.AppendText("电流已消失,取消记录\n");
|
|
|
+ }
|
|
|
}
|
|
|
+ isWaitingForStableData = false;
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
- OperateResult.AppendText(ex.Message + data + "\n");
|
|
|
+ OperateResult.AppendText($"处理稳定数据时出错: {ex.Message}\n");
|
|
|
+ isWaitingForStableData = false;
|
|
|
}
|
|
|
}
|
|
|
|