This commit is contained in:
@@ -43,9 +43,9 @@ namespace PLCDataMonitor
|
||||
private IModbusMaster _modbusMaster;
|
||||
|
||||
|
||||
// PLC 地址(根据文档修正)
|
||||
private const int PauseControlCoil = 25053; // 写入 true=暂停,false=继续(线圈地址)
|
||||
private const int PauseStatusCoil = 25053; // 读取当前暂停状态(同一地址)
|
||||
// 暂停控制(HR500.04)
|
||||
private const int PauseControlCoil = 41204; // 写入 true=暂停,false=继续
|
||||
private const int PauseStatusCoil = 41204; // 读取当前暂停状态(同一地址)
|
||||
|
||||
|
||||
// 记录暂停时间段(用于导出过滤)
|
||||
@@ -55,14 +55,6 @@ namespace PLCDataMonitor
|
||||
|
||||
public List<(DateTime start, DateTime end)> PausePeriods => _pausePeriods;
|
||||
|
||||
|
||||
|
||||
// 报警线圈地址(连续读取)
|
||||
private const int AlarmStartAddr_WR4 = 25065; // WR4.0
|
||||
private const int AlarmCount_WR4 = 5; // WR4.0 ~ WR4.4
|
||||
private const int AlarmStartAddr_WR9 = 25145; // WR9.0
|
||||
private const int AlarmCount_WR9 = 5; // WR9.0 ~ WR9.4
|
||||
|
||||
// 添加清空方法
|
||||
public void ClearPausePeriods()
|
||||
{
|
||||
@@ -268,8 +260,6 @@ namespace PLCDataMonitor
|
||||
|
||||
private void ReadDataLoop()
|
||||
{
|
||||
|
||||
|
||||
//int i = 0;
|
||||
//while (i < 100)
|
||||
//{
|
||||
@@ -317,11 +307,6 @@ namespace PLCDataMonitor
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
while (_keepReading)
|
||||
{
|
||||
try
|
||||
@@ -359,6 +344,15 @@ namespace PLCDataMonitor
|
||||
}
|
||||
}
|
||||
_lastPausedState = isPaused;
|
||||
|
||||
// 【新增】同步更新 HomePage 上的按钮状态
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
_homePage.SetButtonState(isPaused);
|
||||
}), System.Windows.Threading.DispatcherPriority.Background);
|
||||
|
||||
|
||||
|
||||
}
|
||||
// --- 结束记录暂停时间段 ---
|
||||
|
||||
@@ -371,31 +365,38 @@ namespace PLCDataMonitor
|
||||
|
||||
|
||||
|
||||
// 读取报警状态
|
||||
bool[] alarmsWR4 = null;
|
||||
bool[] alarmsWR9 = null;
|
||||
// ----- 读取报警状态(HR504 / HR509)-----
|
||||
bool[] alarmsHR504 = null;
|
||||
bool[] alarmsHR509 = null;
|
||||
try
|
||||
{
|
||||
alarmsWR4 = _modbusMaster?.ReadCoils(0x01, (ushort)AlarmStartAddr_WR4, AlarmCount_WR4);
|
||||
alarmsWR9 = _modbusMaster?.ReadCoils(0x01, (ushort)AlarmStartAddr_WR9, AlarmCount_WR9);
|
||||
// 连续读取 HR504.1 ~ HR504.5(5个线圈,起始 41265)
|
||||
alarmsHR504 = _modbusMaster?.ReadCoils(0x01, 41265, 5);
|
||||
// 连续读取 HR509.0 ~ HR509.3(4个线圈,起始 41345)
|
||||
alarmsHR509 = _modbusMaster?.ReadCoils(0x01, 41344, 4);
|
||||
}
|
||||
catch { } // 读取失败时不更新报警
|
||||
catch { }
|
||||
|
||||
// 构建报警消息
|
||||
string alarmMessage = "";
|
||||
if (alarmsWR4 != null && alarmsWR4.Length >= 5)
|
||||
if (alarmsHR504 != null && alarmsHR504.Length >= 5)
|
||||
{
|
||||
if (alarmsWR4[0]) alarmMessage += "超温95℃ ";
|
||||
if (alarmsWR4[1]) alarmMessage += "超上限水位 ";
|
||||
if (alarmsWR4[2]) alarmMessage += "急停被按下 ";
|
||||
if (alarmsWR4[3]) alarmMessage += "测试完成 ";
|
||||
if (alarmsWR4[4]) alarmMessage += "摩擦过大 ";
|
||||
// 根据实测:索引0=41265=已达上限水位
|
||||
if (alarmsHR504[0]) alarmMessage += "已达上限水位 ";
|
||||
// 索引1=41266=急停被按下
|
||||
if (alarmsHR504[1]) alarmMessage += "急停被按下 ";
|
||||
// 索引2=41267=测试完成(待确认)
|
||||
if (alarmsHR504[2]) alarmMessage += "测试完成 ";
|
||||
// 索引3=41268=摩擦力过大(待确认)
|
||||
if (alarmsHR504[3]) alarmMessage += "摩擦力过大 ";
|
||||
// 索引4=41269=超温95℃(待确认)
|
||||
if (alarmsHR504[4]) alarmMessage += "超温95℃ ";
|
||||
}
|
||||
if (alarmsWR9 != null && alarmsWR9.Length >= 5)
|
||||
if (alarmsHR509 != null && alarmsHR509.Length >= 4)
|
||||
{
|
||||
if (alarmsWR9[0]) alarmMessage += "1工位低位水 ";
|
||||
if (alarmsWR9[2]) alarmMessage += "水泵未运行 "; // 索引 2 对应 WR9.2
|
||||
if (alarmsWR9[3]) alarmMessage += "2工位低位水 ";
|
||||
if (alarmsHR509[0]) alarmMessage += "1工位低水位 ";
|
||||
if (alarmsHR509[2]) alarmMessage += "水泵未运行 ";
|
||||
// 索引3=41348=2工位低水位
|
||||
if (alarmsHR509[3]) alarmMessage += "2工位低水位 ";
|
||||
}
|
||||
if (string.IsNullOrEmpty(alarmMessage))
|
||||
alarmMessage = "无报警";
|
||||
@@ -403,7 +404,6 @@ namespace PLCDataMonitor
|
||||
|
||||
|
||||
|
||||
|
||||
// 更新UI(数据监控页+曲线页)
|
||||
// 使用 BeginInvoke 避免阻塞后台读取线程,减少 UI 卡死风险
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
@@ -424,52 +424,56 @@ namespace PLCDataMonitor
|
||||
// 新增:更新报警显示
|
||||
_homePage.UpdateAlarms(alarmMessage);
|
||||
|
||||
// 关键修改:确保曲线数据正确记录
|
||||
if (data.setCount > 0 && data.actualCount > 0 &&
|
||||
data.setCount > data.actualCount) // 点击了开始,且实际次数小于设定次数,才更新曲线
|
||||
|
||||
if (!isPaused)
|
||||
{
|
||||
// 创建数据点
|
||||
var dataPoint = new Tuple<DateTime, double, double>(
|
||||
DateTime.Now,
|
||||
friction1,
|
||||
friction2
|
||||
);
|
||||
// 关键修改:确保曲线数据正确记录
|
||||
if (data.setCount > 0 && data.actualCount > 0 &&
|
||||
data.setCount > data.actualCount) // 点击了开始,且实际次数小于设定次数,才更新曲线
|
||||
{
|
||||
// 创建数据点
|
||||
var dataPoint = new Tuple<DateTime, double, double>(
|
||||
DateTime.Now,
|
||||
friction1,
|
||||
friction2
|
||||
);
|
||||
|
||||
// 添加到共享队列
|
||||
_sharedCurveDataQueue.Enqueue(dataPoint);
|
||||
// 添加到共享队列
|
||||
_sharedCurveDataQueue.Enqueue(dataPoint);
|
||||
|
||||
// 通知曲线页面更新
|
||||
_curvePage.AddFrictionData(friction1, friction2);
|
||||
// 通知曲线页面更新
|
||||
_curvePage.AddFrictionData(friction1, friction2);
|
||||
|
||||
// 关键修改:只添加一次,不需要重复添加到报表页面
|
||||
// 报表页面的CurveDataQueue和_sharedCurveDataQueue是同一个对象
|
||||
|
||||
}
|
||||
|
||||
if (data.setCount > 0 && data.actualCount > 0 &&
|
||||
data.setCount == data.actualCount && !_hasInsertedReport) // 一次测试执行完成,保存报表
|
||||
{
|
||||
// 创建报表记录(时间+压力+最大摩擦力)
|
||||
var reportRecord = new ReportData(
|
||||
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // 当前时间
|
||||
pressure1.ToString("F1"), // 工位1压力
|
||||
pressure2.ToString("F1"), // 工位2压力
|
||||
maxFriction1.ToString("F1"), // 工位1最大摩擦力
|
||||
maxFriction2.ToString("F1"),
|
||||
data.t1,
|
||||
data.t2,
|
||||
data.D
|
||||
);
|
||||
|
||||
// 插入报表
|
||||
_reportPage.AddReportRecord(reportRecord);
|
||||
// 标记为已插入(避免同一批次重复插入)
|
||||
_hasInsertedReport = true;
|
||||
}
|
||||
|
||||
if (data.actualCount < data.setCount)
|
||||
{
|
||||
_hasInsertedReport = false;
|
||||
}
|
||||
}
|
||||
|
||||
if (data.setCount > 0 && data.actualCount > 0 &&
|
||||
data.setCount == data.actualCount && !_hasInsertedReport) // 一次测试执行完成,保存报表
|
||||
{
|
||||
// 创建报表记录(时间+压力+最大摩擦力)
|
||||
var reportRecord = new ReportData(
|
||||
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), // 当前时间
|
||||
pressure1.ToString("F1"), // 工位1压力
|
||||
pressure2.ToString("F1"), // 工位2压力
|
||||
maxFriction1.ToString("F1"), // 工位1最大摩擦力
|
||||
maxFriction2.ToString("F1"),
|
||||
data.t1,
|
||||
data.t2,
|
||||
data.D
|
||||
);
|
||||
|
||||
// 插入报表
|
||||
_reportPage.AddReportRecord(reportRecord);
|
||||
// 标记为已插入(避免同一批次重复插入)
|
||||
_hasInsertedReport = true;
|
||||
}
|
||||
|
||||
if (data.actualCount < data.setCount)
|
||||
{
|
||||
_hasInsertedReport = false;
|
||||
}
|
||||
}), System.Windows.Threading.DispatcherPriority.Background);
|
||||
}
|
||||
}
|
||||
@@ -594,6 +598,7 @@ namespace PLCDataMonitor
|
||||
if (_isConnected && _modbusMaster != null)
|
||||
{
|
||||
_modbusMaster.WriteSingleCoil(0x01, (ushort)PauseControlCoil, true);
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
@@ -609,6 +614,7 @@ namespace PLCDataMonitor
|
||||
if (_isConnected && _modbusMaster != null)
|
||||
{
|
||||
_modbusMaster.WriteSingleCoil(0x01, (ushort)PauseControlCoil, false);
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
|
||||
Reference in New Issue
Block a user