我当前正在尝试发布带有OPC UA实现的DateTime值和一个称为Open62541的温度值。
为此,我一直在使用Open62541 GitHub存储库中的测试示例,因为它已经发布了当前的日期时间。我试图为温度值添加另一个DSF(DatasetField),如DateTime所示。但它不会发布。
以下代码片段显示了我如何定义添加数据集的方法:
/**
* **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(已发布DataSet):
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/master/examples/pubsub/tutorial_pubsub_subscribe.c
您需要在addPubSubConnection
之前调用addDataSetFieldTemperature
有关如何更新变量
的更多详细信息,请参阅此信息https://stackoverflow.com/questions/58855146/58855146/opcua-open62541-pubsub-how-how-to-publish-value-change/58888882085 #588888882085