跳到主要内容

诊断

通过 device.Diagnostics() 读诊断与报警(DiagSnapshot() / GetAlarms() 仍可用)。

配合事件使用

建议通过 事件 驱动异常处理,而非自行轮询。 直接读取诊断属性适用于 UI 显示等场景。

运行态(Connected() / Status() / ErrorCode())见 属性与状态机。过程数据读写见 过程数据

功能概览

功能说明
诊断快照链路、帧计数、看门狗、抖动、报警观测
现场问题对照按症状选字段
报警活动列表 / 历史 / 确认

诊断快照

类别属性类型访问说明
可用性okbool只读诊断是否可用。false = 设备未启动,读 message
shared_memory_mappedbool只读过程数据是否已就绪
messagestring只读不可用时的原因;可用时为空
链路statestring只读运行状态文案
link_upbool只读网卡链路是否 Up
connectedbool只读周期数据是否正常
active_iocr_countuint32_t只读已激活的 IO 连接数
iocr_activeuint32_t只读IO 连接激活位图
iocr_healthyuint32_t只读IO 连接健康位图
计数heartbeatuint64_t只读心跳计数
cycle_counteruint64_t只读周期循环计数
last_erroruint64_t只读最近错误码(0 = 无)
rx_frames / tx_framesuint64_t只读收 / 发总帧数
rx_rt_frames / tx_rt_framesuint64_t只读收 / 发 RT 周期帧
dropped_framesuint64_t只读被丢弃的帧
invalid_framesuint64_t只读非法帧
watchdog_tripsuint64_t只读看门狗超时次数
last_rx_tsc / last_tx_tscuint64_t只读最近收 / 发帧 TSC
过程区input_area_lengthuint32_t只读输入区长度(字节)
output_area_lengthuint32_t只读输出区长度(字节)
周期stack_cyclesuint64_t只读周期计数
stack_frames_rx / stack_frames_txuint64_t只读收 / 发帧
stack_missed_ticksuint64_t只读错过的节拍
stack_max_jitter_usuint64_t只读最大抖动(微秒)
报警观测stack_alarm_rx_countuint64_t只读收到控制器报警总数
stack_alarm_tx_ack_okuint64_t只读本机报警被确认次数
stack_alarm_tx_ack_failuint64_t只读本机报警确认失败次数
stack_alarm_ack_pendinguint64_t只读在途报警(0/1)
stack_alarm_maint_stateuint64_t只读维护请求状态
stack_alarm_last_arepuint32_t只读最近控制器报警:Arep
stack_alarm_last_slotuint32_t只读最近控制器报警:槽位
stack_alarm_last_subslotuint32_t只读最近控制器报警:子槽位
stack_alarm_last_typeuint32_t只读最近控制器报警:类型
stack_alarm_last_sequint32_t只读最近控制器报警:序列号
stack_alarm_last_usiuint32_t只读最近控制器报警:USI
stack_alarm_last_lenuint32_t只读最近控制器报警:负载长度
错误error_codeint只读错误码原值(0 = 无)

DiagSnapshot()

PnSServiceDiagSnapshot DiagSnapshot();
PnSServiceDiagSnapshot GetDiag(); // 同一实现

一次取回当前诊断快照,适合界面刷新和日志。设备尚未启动时 ok == false,读 message,不视为异常。区长度看 input_area_length / output_area_length,不要写死。

返回值:

  • PnSServiceDiagSnapshot — 诊断数据快照

未连上服务抛 PnSException

示例:

auto snap = device.DiagSnapshot();
if (!snap.ok)
{
printf("%s\n", snap.message.c_str());
return;
}

printf(snap.link_up ? "Up\n" : "Down\n");
printf(snap.connected ? "周期交换正常\n" : "未建立\n");
printf("帧: %llu / %llu\n",
static_cast<unsigned long long>(snap.rx_frames),
static_cast<unsigned long long>(snap.tx_frames));
printf("看门狗: %llu\n",
static_cast<unsigned long long>(snap.watchdog_trips));

现场问题对照

现场症状看哪些字段
网线没插好DiagSnapshot().link_up
周期数据通不通DiagSnapshot().connected
看门狗反复跳DiagSnapshot().watchdog_trips
帧在丢dropped_frames + invalid_frames
周期稳不稳stack_max_jitter_us + stack_missed_ticks
控制器报警没消化stack_alarm_ack_pending / stack_alarm_tx_ack_fail
本机有没有活动报警GetAlarms() / AlarmCount()

报警

类别属性类型访问说明
计数AlarmCount()int只读活动报警总数。与 GetAlarms 同源
CriticalAlarmCount()int只读Critical 报警数
列表GetAlarms()vector<PnSServiceAlarmItem>只读当前活动报警。空列表 = 无活动报警,不是失败
GetAlarmHistory()vector<PnSServiceAlarmItem>只读历史。空列表 = 无历史

失败抛 PnSException

GetAlarms()

std::vector<PnSServiceAlarmItem> GetAlarms();

当前活动报警。无活动报警返回空列表,不是失败。

示例:

for (const auto& a : device.GetAlarms())
printf("%s %s %s\n", a.id.c_str(), a.severity.c_str(), a.message.c_str());

GetAlarmHistory()

std::vector<PnSServiceAlarmItem> GetAlarmHistory();

报警历史。无历史返回空列表,不是失败。

示例:

for (const auto& a : device.GetAlarmHistory())
printf("%s %s %s\n", a.occurred_at.c_str(), a.severity.c_str(), a.message.c_str());

AcknowledgeAlarm()

void AcknowledgeAlarm(const std::string& id);

确认一条活动报警。ID 取自 GetAlarms()

参数:

  • id (string) — 报警 ID。空串抛 PnSException;无此 ID 抛 PnSException

示例:

device.AcknowledgeAlarm("PnS.LinkDown");

AcknowledgeAllAlarms()

int AcknowledgeAllAlarms();

确认全部活动报警。

返回值:

  • int — 被确认条数(0 = 无活动报警)

示例:

int n = device.AcknowledgeAllAlarms();
PnSServiceAlarmItem
属性类型说明
idstring报警 ID(如 PnS.LinkDown
severitystring严重度:Info / Warning / Error / Critical
messagestring报警消息
occurred_atstring最近一次发生时间(ISO-8601 文本)
countint发生次数(≥1)
sourcestring来源

I&M 标识

ImData() / GetIm()

PnSServiceImInfo ImData();
PnSServiceImInfo GetIm(); // 同一实现

I&M1–I&M3 已由原生栈启用并如实回读,I&M4 未启用。缺字段为空 / 0,不编。im14_supported 为 true 表示 I&M1–I&M4 这次读回成功(从站未启动读不到为 false、字段为空串);主站没写过时读到的是栈的出厂占位值。说明在 im_note

示例:

auto im = device.ImData();
printf("%s vendor=0x%04X device=0x%04X\n",
im.station_name.c_str(), im.vendor_id, im.device_id);

应用记录

GetRecords()

PnSUserRecords GetRecords();

空列表不是失败。

示例:

auto recs = device.GetRecords();
printf("count=%d\n", recs.count);

复位

ResetDevice()

void ResetDevice(const std::string& mode);
mode行为
communication软停再启,记录保留
factory清空后全新启动

其它 modePnSException。本调用不改 IP。

示例:

device.ResetDevice("communication");