|
|
@@ -5,16 +5,10 @@ using System.Data;
|
|
|
using System.IO;
|
|
|
using System.Threading;
|
|
|
using System.Windows.Forms;
|
|
|
-using System.Xml;
|
|
|
using System.Text;
|
|
|
using System.Net;
|
|
|
using System.Net.Sockets;
|
|
|
-using System.Text.RegularExpressions;
|
|
|
using System.IO.Ports;
|
|
|
-using static System.Windows.Forms.VisualStyles.VisualStyleElement;
|
|
|
-using System.Windows.Media.Media3D;
|
|
|
-using System.Runtime.InteropServices;
|
|
|
-using NPOI.POIFS.Storage;
|
|
|
|
|
|
namespace FileWatcher
|
|
|
{
|
|
|
@@ -53,7 +47,6 @@ namespace FileWatcher
|
|
|
ftpOperater ftp = new ftpOperater();
|
|
|
|
|
|
private bool waitingForStableData = false;
|
|
|
- private DateTime lastCurrentReadingTime;
|
|
|
|
|
|
public AutoMakeQTYRZ()
|
|
|
{
|
|
|
@@ -68,8 +61,6 @@ namespace FileWatcher
|
|
|
}
|
|
|
DataTable Dbfind;
|
|
|
|
|
|
- string IPAddress = "";
|
|
|
- bool writedata = true;
|
|
|
private void nr_rule_DBChange(object sender, EventArgs e)
|
|
|
{
|
|
|
Dbfind = li_code.ReturnData;
|
|
|
@@ -83,46 +74,6 @@ namespace FileWatcher
|
|
|
SetLoadingWindow stw = new SetLoadingWindow(InitDB, "正在启动程序");
|
|
|
stw.StartPosition = FormStartPosition.CenterScreen;
|
|
|
stw.ShowDialog();
|
|
|
- //string[] str = new string[] { "AA001097796643D67FE43D4185C74185EB47429A63783F64",
|
|
|
- // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
|
|
|
- // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
|
|
|
- // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
|
|
|
- // "AA0010685B664300000000BE2BC74185EB47423655783F1E",
|
|
|
- // "AA001097796643D67FE43D4185C74185EB47429A63783F64",
|
|
|
- // "AA001097796643D67FE43D4185C74185EB47429A63783F64"};
|
|
|
- //for (int i = 0; i < str.Length; i++)
|
|
|
- //{
|
|
|
- // float 电压数据 = getdata(str[i].Substring(6, 8));
|
|
|
- // float 电流数据 = getdata(str[i].Substring(14, 8));
|
|
|
- // float 功率数据 = getdata(str[i].Substring(22, 8));
|
|
|
- // float 频率数据 = getdata(str[i].Substring(30, 8));
|
|
|
- // float 功率因素 = getdata(str[i].Substring(38, 8));
|
|
|
- // OperateResult.AppendText(str[i]);
|
|
|
- // OperateResult.AppendText(电压数据.ToString() + "\n");
|
|
|
- // OperateResult.AppendText(电流数据.ToString() + "\n");
|
|
|
- // OperateResult.AppendText(功率数据.ToString() + "\n");
|
|
|
- // OperateResult.AppendText(频率数据.ToString() + "\n");
|
|
|
- // OperateResult.AppendText(功率因素.ToString() + "\n");
|
|
|
- // //如果电流是0,则下次重新获取数据
|
|
|
-
|
|
|
- // if (电流数据 == 0)
|
|
|
- // {
|
|
|
- // writedata = false;
|
|
|
- // if (电流数据 == 0 && !writedata) {
|
|
|
- // writedata = true;
|
|
|
- // }
|
|
|
- // }
|
|
|
-
|
|
|
- // if (writedata)
|
|
|
- // {
|
|
|
- // if (电流数据 != 0) {
|
|
|
- // dh.ExecuteSql("insert into currenttest(CT_ID, CT_MAKECODE, CT_VOLTAGE, CT_CURRENT, CT_POWER, CT_FREQUENCY, CT_POWER1)" +
|
|
|
- // "VALUES(currenttest_SEQ.NEXTVAL,'" + ma_code.Text + "','" + 电压数据 + "','" + 电流数据 + "','" + 功率数据 + "','" + 频率数据 + "','" + 功率因素 + "')", "insert");
|
|
|
- // writedata = false;
|
|
|
- // }
|
|
|
- // }
|
|
|
- //}
|
|
|
-
|
|
|
FormBorderStyle = FormBorderStyle.FixedSingle;
|
|
|
serialPort1.DataReceived += SerialPort1_DataReceived;
|
|
|
|
|
|
@@ -131,12 +82,6 @@ namespace FileWatcher
|
|
|
//ATEFile.Changed += new FileSystemEventHandler(XmlWatcher_Created);
|
|
|
|
|
|
List<string> CacheInf = new List<string>();
|
|
|
- IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
|
|
|
- for (int i = 0; i < IpEntry.AddressList.Length; i++)
|
|
|
- {
|
|
|
- if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
|
|
|
- IPAddress = IpEntry.AddressList[i].ToString();
|
|
|
- }
|
|
|
//获取缓存信息
|
|
|
try
|
|
|
{
|
|
|
@@ -177,18 +122,12 @@ namespace FileWatcher
|
|
|
return f2;
|
|
|
}
|
|
|
|
|
|
- string data = "";
|
|
|
bool realwritedata = false;
|
|
|
|
|
|
- private bool canRecordData = true; // 初始为true,允许第一次记录
|
|
|
- private bool currentWasZero = true; // 初始状态假设电流为0
|
|
|
-
|
|
|
private bool isWaitingForStableData = false;
|
|
|
- private DateTime currentDetectionTime;
|
|
|
private bool canRecordNext = true;
|
|
|
private string pendingData = "";
|
|
|
private System.Windows.Forms.Timer stableDataTimer = new System.Windows.Forms.Timer();
|
|
|
- private object dataLock = new object();
|
|
|
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
|
|
|
{
|
|
|
try
|
|
|
@@ -200,28 +139,24 @@ namespace FileWatcher
|
|
|
|
|
|
this.Invoke((MethodInvoker)delegate
|
|
|
{
|
|
|
- lock (dataLock)
|
|
|
- {
|
|
|
- pendingData = rawData.Length >= 48 ? rawData.Substring(0, 48) : rawData;
|
|
|
- //OperateResult.AppendText($"收到数据: {pendingData}\n");
|
|
|
+ pendingData = rawData.Length >= 48 ? rawData.Substring(0, 48) : rawData;
|
|
|
+ //OperateResult.AppendText($"收到数据: {pendingData}\n");
|
|
|
|
|
|
- if (pendingData.Length == 48)
|
|
|
+ if (pendingData.Length == 48)
|
|
|
+ {
|
|
|
+ float current = getdata(pendingData.Substring(14, 8));
|
|
|
+ // 电流归零时重置记录许可
|
|
|
+ if (current == 0)
|
|
|
+ {
|
|
|
+ canRecordNext = true;
|
|
|
+ //OperateResult.AppendText("电流归零,已允许下次记录\n");
|
|
|
+ }
|
|
|
+ // 电流非零且允许记录时触发
|
|
|
+ else if (current != 0 && canRecordNext && !isWaitingForStableData)
|
|
|
{
|
|
|
- float current = getdata(pendingData.Substring(14, 8));
|
|
|
- //OperateResult.AppendText($"当前电流: {current}A (允许记录:{canRecordNext})\n");
|
|
|
- // 电流归零时重置记录许可
|
|
|
- if (current == 0)
|
|
|
- {
|
|
|
- canRecordNext = true;
|
|
|
- //OperateResult.AppendText("电流归零,已允许下次记录\n");
|
|
|
- }
|
|
|
- // 电流非零且允许记录时触发
|
|
|
- else if (current != 0 && canRecordNext && !isWaitingForStableData)
|
|
|
- {
|
|
|
- //OperateResult.AppendText("[开始] 检测到有效电流,启动2秒延迟...\n");
|
|
|
- isWaitingForStableData = true;
|
|
|
- stableDataTimer.Start();
|
|
|
- }
|
|
|
+ //OperateResult.AppendText("[开始] 检测到有效电流,启动2秒延迟...\n");
|
|
|
+ isWaitingForStableData = true;
|
|
|
+ stableDataTimer.Start();
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
@@ -241,59 +176,53 @@ namespace FileWatcher
|
|
|
{
|
|
|
this.Invoke((MethodInvoker)delegate
|
|
|
{
|
|
|
- lock (dataLock)
|
|
|
+ if (string.IsNullOrEmpty(pendingData) || pendingData.Length < 48)
|
|
|
{
|
|
|
- //OperateResult.AppendText("[触发] 处理稳定数据\n");
|
|
|
-
|
|
|
- if (string.IsNullOrEmpty(pendingData) || pendingData.Length < 48)
|
|
|
- {
|
|
|
- //OperateResult.AppendText("错误: 数据长度不足\n");
|
|
|
- isWaitingForStableData = false;
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- float current = getdata(pendingData.Substring(14, 8));
|
|
|
-
|
|
|
- // 延迟后再次检查电流
|
|
|
- if (current == 0)
|
|
|
- {
|
|
|
- //OperateResult.AppendText("警告: 延迟后电流已归零,取消记录\n");
|
|
|
- isWaitingForStableData = false;
|
|
|
- return;
|
|
|
- }
|
|
|
+ //OperateResult.AppendText("错误: 数据长度不足\n");
|
|
|
+ isWaitingForStableData = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- // 解析其他数据
|
|
|
- float voltage = getdata(pendingData.Substring(6, 8));
|
|
|
- float power = getdata(pendingData.Substring(22, 8));
|
|
|
- float frequency = getdata(pendingData.Substring(30, 8));
|
|
|
- float powerFactor = getdata(pendingData.Substring(38, 8));
|
|
|
+ float current = getdata(pendingData.Substring(14, 8));
|
|
|
|
|
|
- // 检查功率范围
|
|
|
- if (double.TryParse(FunctionNum.Text, out double minPower) &&
|
|
|
- double.TryParse(FunctionNum1.Text, out double maxPower))
|
|
|
+ // 延迟后再次检查电流
|
|
|
+ if (current == 0)
|
|
|
+ {
|
|
|
+ //OperateResult.AppendText("警告: 延迟后电流已归零,取消记录\n");
|
|
|
+ isWaitingForStableData = false;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // 解析其他数据
|
|
|
+ float voltage = getdata(pendingData.Substring(6, 8));
|
|
|
+ float power = getdata(pendingData.Substring(22, 8));
|
|
|
+ float frequency = getdata(pendingData.Substring(30, 8));
|
|
|
+ float powerFactor = getdata(pendingData.Substring(38, 8));
|
|
|
+
|
|
|
+ // 检查功率范围
|
|
|
+ if (double.TryParse(FunctionNum.Text, out double minPower) &&
|
|
|
+ double.TryParse(FunctionNum1.Text, out double maxPower))
|
|
|
+ {
|
|
|
+ if (power >= minPower && power <= maxPower)
|
|
|
{
|
|
|
- if (power >= minPower && power <= maxPower)
|
|
|
- {
|
|
|
- // 写入数据库
|
|
|
- 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 + "', " + voltage + ", " + current + ", " + power + ", " + frequency + "" +
|
|
|
- ", " + powerFactor + ")", "insert");
|
|
|
- NowQTY.Text = dh.getRowCount("currenttest", $"ct_makecode='{ma_code.Text}'").ToString();
|
|
|
- OperateResult.AppendText(DateTime.Now.ToString("HH:mm:ss") + $"数据上成功,记录: 电流={current}A 功率={power}W\n");
|
|
|
- // 记录成功后禁止下次记录,直到电流归零
|
|
|
- canRecordNext = false;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- OperateResult.AppendText($"功率超出范围: {power}W (要求: {minPower}-{maxPower}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 + "', " + voltage + ", " + current + ", " + power + ", " + frequency + "" +
|
|
|
+ ", " + powerFactor + ")", "insert");
|
|
|
+ NowQTY.Text = dh.getRowCount("currenttest", $"ct_makecode='{ma_code.Text}'").ToString();
|
|
|
+ OperateResult.AppendText(DateTime.Now.ToString("HH:mm:ss") + $"数据上成功,记录: 电流={current}A 功率={power}W\n");
|
|
|
+ // 记录成功后禁止下次记录,直到电流归零
|
|
|
+ canRecordNext = false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- OperateResult.AppendText("错误: 无效的功率范围设置\n");
|
|
|
+ OperateResult.AppendText($"功率超出范围: {power}W (要求: {minPower}-{maxPower}W)\n");
|
|
|
}
|
|
|
- isWaitingForStableData = false;
|
|
|
}
|
|
|
+ else
|
|
|
+ {
|
|
|
+ OperateResult.AppendText("错误: 无效的功率范围设置\n");
|
|
|
+ }
|
|
|
+ isWaitingForStableData = false;
|
|
|
});
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
@@ -340,16 +269,6 @@ namespace FileWatcher
|
|
|
OperateResult.AppendText("工单号不能为空\n");
|
|
|
return;
|
|
|
}
|
|
|
- //if (!dh.CheckExist("line", "li_code='" + li_code.Text + "'"))
|
|
|
- //{
|
|
|
- // OperateResult.AppendText("线别不能为空\n");
|
|
|
- // return;
|
|
|
- //}
|
|
|
- //string ExitConfirm = MessageBox.Show(this, "确认计数器是否置为0?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question).ToString();
|
|
|
- //if (ExitConfirm != "Yes")
|
|
|
- //{
|
|
|
- // return;
|
|
|
- //}
|
|
|
//设置按钮不可点击
|
|
|
StartWatch.Enabled = false;
|
|
|
ma_code.Enabled = false;
|
|
|
@@ -411,8 +330,6 @@ namespace FileWatcher
|
|
|
e.Cancel = true;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
private void Timer_Tick(object sender, EventArgs e)
|
|
|
{
|
|
|
if (serialPort1.IsOpen)
|
|
|
@@ -450,7 +367,6 @@ namespace FileWatcher
|
|
|
rk.Close();
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
private byte[] HexStringToBytes(string hs)//十六进制字符串转byte
|
|
|
{
|
|
|
string a = hs.Replace(" ", "");
|
|
|
@@ -479,10 +395,5 @@ namespace FileWatcher
|
|
|
//按照指定编码将字节数组变为字符串
|
|
|
return b;
|
|
|
}
|
|
|
-
|
|
|
- private void GetData_Click(object sender, EventArgs e)
|
|
|
- {
|
|
|
-
|
|
|
- }
|
|
|
}
|
|
|
}
|