2

我目前正在尝试使用名为 open62541 的 OPC UA 实现发布日期时间值和温度值。

为此,我一直在使用来自 open62541 github 存储库的测试示例,因为它已经发布了当前日期时间。我试图为温度值添加另一个 DSF(DataSetField),就像它与日期时间一起显示的那样。但它不会发布它。

以下代码片段显示了我如何定义添加 DataSetFields 的方法:

/**
 * **DataSetField handling**
 *
 * The DataSetField (DSF) is part of the PDS and describes exactly one published
 * field. */
static void
addDataSetField(UA_Server *server) {
    /* Add a field to the previous created PublishedDataSet */
    UA_NodeId dataSetFieldIdent;
    UA_DataSetFieldConfig dataSetFieldConfig;
    memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
    dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
    dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Server localtime");
    dataSetFieldConfig.field.variable.promotedField = UA_FALSE;

    dataSetFieldConfig.field.variable.publishParameters.publishedVariable =
                  UA_NODEID_NUMERIC(0, UA_NS0ID_SERVER_SERVERSTATUS_CURRENTTIME);
    dataSetFieldConfig.field.variable.publishParameters.attributeId = 
                  UA_ATTRIBUTEID_VALUE;
    UA_Server_addDataSetField(server, publishedDataSetIdent,
                          &dataSetFieldConfig, &dataSetFieldIdent);
}

static void
addDataSetFieldTemperature(UA_Server *server) {
    /* Add a field to the previous created PublishedDataSet */
    UA_NodeId dataSetFieldIdent;
    UA_DataSetFieldConfig dataSetFieldConfig;
    memset(&dataSetFieldConfig, 0, sizeof(UA_DataSetFieldConfig));
    dataSetFieldConfig.dataSetFieldType = UA_PUBSUB_DATASETFIELD_VARIABLE;
    dataSetFieldConfig.field.variable.fieldNameAlias = UA_STRING("Temperature");
    dataSetFieldConfig.field.variable.promotedField = UA_FALSE;
    dataSetFieldConfig.field.variable.publishParameters.publishedVariable =
                                      UA_NODEID_NUMERIC(0, 50);
    dataSetFieldConfig.field.variable.publishParameters.attributeId =
                                      UA_ATTRIBUTEID_VALUE;
    UA_Server_addDataSetField(server, publishedDataSetIdent, 
                                      &dataSetFieldConfig,
                                      &dataSetFieldIdent);
}

在主函数中,然后我调用这两个方法将它们添加到 PDS(PublishedDataSet):

addPubSubConnection(server, transportProfile, networkAddressUrl);
addPublishedDataSet(server);
addDataSetFieldTemperature(server);
addDataSetField(server);
addWriterGroup(server);
addDataSetWriter(server);

温度值是一个简单的双精度值,那么我在这里做错了什么?也许错误在订阅的客户端应用程序中?

为了订阅已发布的值,我还使用了 github 上的示例(文件名为 tutorial_pubsub_subscribe.c),我正在尝试按如下方式解析值:

/* Loop over the fields and print well-known content types */
for(int i = 0; i < dsm->data.keyFrameData.fieldCount; i++) {
    const UA_DataType *currentType = dsm->data.keyFrameData.dataSetFields[i].value.type;
    if(currentType == &UA_TYPES[UA_TYPES_BYTE]) {
        UA_Byte value = *(UA_Byte *)dsm->data.keyFrameData.dataSetFields[i].value.data;
        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
                    "Message content: [Byte] \tReceived data: %i", value);
    } else if (currentType == &UA_TYPES[UA_TYPES_DATETIME]) {
        UA_DateTime value = *(UA_DateTime *)dsm->data.keyFrameData.dataSetFields[i].value.data;
        UA_DateTimeStruct receivedTime = UA_DateTime_toStruct(value);
        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
                    "Message content: [DateTime] \t"
                    "Received date: %02i-%02i-%02i Received time: %02i:%02i:%02i",
                    receivedTime.year, receivedTime.month, receivedTime.day,
                    receivedTime.hour, receivedTime.min, receivedTime.sec);
    } else if(currentType == &UA_TYPES[UA_TYPES_UINT16]) {
        UA_UInt16 receivedTemp =
            *(UA_UInt16 *)dsm->data.keyFrameData.dataSetFields[i].value.data;

        UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
                    "Message content: [UA_UInt16] \t"
                    "Received temperature: %i",
                    receivedTemp);
    }

有什么建议么?非常感谢您,如果您需要有关此问题的更多详细信息,请告诉我,我将进一步编辑问题!

编辑:如果您想查看整个示例代码,请查看以下链接:对于发布应用程序: https ://github.com/open62541/open62541/blob/master/examples/pubsub/tutorial_pubsub_publish 。C

对于订阅应用程序:https ://github.com/open62541/open62541/blob/master/examples/pubsub/tutorial_pubsub_subscribe.c

4

1 回答 1

0

您需要先致电addDataSetFieldTemperature查看addPubSubConnection
有关如何更新变量的更多详细信息
https://stackoverflow.com/questions/58855146/opcua-open62541-pubsub-how-to-publish-value-change/58882085#58882085

于 2019-11-15T17:46:54.093 回答