Twincat ADS 事件驱动读取在一段时间后停止工作 (Java)

Twincat ADS event driven reading stops working after a while (Java)

我们开发了一个 Java 应用程序,它使用 TwinCat ADS 库 (DLL) 来读取、写入和处理来自 Beckhoff PLC (CX5120) 的事件。 我们在几台机器上成功 运行 但不幸的是,我们目前遇到事件处理突然停止的问题。 这是我们经历的确切场景:

使用装有 Java 应用程序的另一台 PC 进行测试,同样的问题。所以 PLC 中的某些东西似乎冻结/停止工作。

以下是我们设置事件处理的方式:

// Implementation of the CallbackListenerAdsState interface
public class ADSEventController implements CallbackListenerAdsState {

......

// Register itself as listener for the ADS events (in constructor)
callObject = new AdsCallbackObject();
callObject.addListenerCallbackAdsState(this);

....

// Event handling
public void onEvent(AmsAddr addr, AdsNotificationHeader notification, long user) {
    log.info("Got ADS event for handle[{}] and with raw data[{}]", user, notification.getData());

......

// Registering notification handles for PLC variables
// If we already assigned a notification, delete it first (while reconnecting)
    JNILong notification = new JNILong();
if(var.getNotification() != null) {
    notification = var.getNotification();
    AdsCallDllFunction.adsSyncDelDeviceNotificationReq(addr,notification);
}

// Specify attributes of the notificationRequest
AdsNotificationAttrib attr = new AdsNotificationAttrib();
attr.setCbLength(var.getSize());
attr.setNTransMode(AdsConstants.ADSTRANS_SERVERONCHA);
attr.setDwChangeFilter(1000);   // 0.01 sec
attr.setNMaxDelay(2000);        // 0.02 sec

// Create notificationHandle
long err = AdsCallDllFunction.adsSyncAddDeviceNotificationReq(
    addr,
    AdsCallDllFunction.ADSIGRP_SYM_VALBYHND, // IndexGroup
    var.getHandle(), // IndexOffset
    attr, // The defined AdsNotificationAttrib object
    var.getHandle(), // Choose arbitrary number
    notification);
var.setNotification(notification);

if (err != 0) {
    log.error("Error: Add notification: 0x{} for var[{}]", Long.toHexString(err), var.getId());
}

我们设法找到了原因。 当我们注册一个变量时,我们从 PLC 得到一个句柄(长),在我们的例子中,它在一段时间后意外地开始为负值。 我们还使用这个 long 值作为通知的用户引用,但是,我们发现用户引用是 ADS 库中的无符号长整数。

因此,如果我们设置一个负值,例如-1258290964 作为 adsSyncAddDeviceNotificationReq 调用中的“任意数字”,CallbackListenerAdsState onEvent 方法的参数“user”(长整型)获得了我们有符号长用户引用的无符号长整型表示形式,即 3036676332。 在我们的 Java 应用程序中,我们使用此用户引用通过此句柄将事件与特定 plc 变量相匹配。因为在我们的示例中,我们期望 -1258290964 但得到 3036676332,所以我们从未处理过任何事件。