协议61850学习

https://libiec61850.com/libiec61850/documentation/iec-61850-client-tutorial/(学习网站)

从站读取数据

 MmsValue* dataSetValue123= IedConnection_readObject(con, &error, "simpleIOGenericIO/GGIO1.AnIn2.mag.f", IEC61850_FC_MX);
               if(dataSetValue123 != NULL) {
                   //int v = MmsValue_toInt32(dataSetValue123);
                   float vv=     MmsValue_toFloat(dataSetValue123);
                   printf("read int32 value: %.3f, type=%d\n", vv);
                   MmsValue_delete(dataSetValue123);
               }

以下是完整的

/*
 * client_example1.c
 *
 * This example is intended to be used with server_example3 or server_example_goose.
 */

#include "iec61850_client.h"

#include <stdlib.h>
#include <stdio.h>

#include "hal_thread.h"

void
reportCallbackFunction(void* parameter, ClientReport report)
{
    MmsValue* dataSetValues = ClientReport_getDataSetValues(report);

    printf("received report for %s\n", ClientReport_getRcbReference(report));

    int i;
    for (i = 0; i < 2; i++) {
        ReasonForInclusion reason = ClientReport_getReasonForInclusion(report, i);

        if (reason != IEC61850_REASON_NOT_INCLUDED) {
            printf("  GGIO1.SPCSO%i.stVal: %i (included for reason %i)\n", i,
                    MmsValue_getBoolean(MmsValue_getElement(dataSetValues, i)), reason);
        float sqtouch=    MmsValue_toFloat(MmsValue_getElement(dataSetValues, i));
        printf("%.3f",sqtouch);
        }
    }
}

int main(int argc, char** argv) {

    char* hostname;
    int tcpPort = 102;

    if (argc > 1)
        hostname = argv[1];
    else
        hostname = "localhost";

    if (argc > 2)
        tcpPort = atoi(argv[2]);

    IedClientError error;

    IedConnection con = IedConnection_create();

    IedConnection_connect(con, &error, hostname, tcpPort);

    if (error == IED_ERROR_OK) {

        IedConnection_getServerDirectory(con, &error, false);

        /* read an analog measurement value from server */
        MmsValue* value = IedConnection_readObject(con, &error, "simpleIOGenericIO/GGIO1.AnIn1.mag.f", IEC61850_FC_MX);

        if (value != NULL) {
            float fval = MmsValue_toFloat(value);
            printf("read float value: %f\n", fval);
            MmsValue_delete(value);
        }

        /* write a variable to the server */
        value = MmsValue_newVisibleString("libiec61850.com");
        IedConnection_writeObject(con, &error, "simpleIOGenericIO/GGIO1.NamPlt.vendor", IEC61850_FC_DC, value);

        if (error != IED_ERROR_OK)
            printf("failed to write simpleIOGenericIO/GGIO1.NamPlt.vendor!\n");

        MmsValue_delete(value);


        /* read data set */
        //ClientDataSet clientDataSet = IedConnection_readDataSetValues(con, &error, "simpleIOGenericIO/LLN0.Events", NULL);
         ClientDataSet clientDataSet = IedConnection_readDataSetValues(con, &error, "simpleIOGenericIO/GGI01.Events", NULL);
        if (clientDataSet == NULL)
            printf("failed to read dataset\n");

        /* Read RCB values */
       ClientReportControlBlock rcb =
                IedConnection_getRCBValues(con, &error, "simpleIOGenericIO/GGI01.MX.AnIn3", NULL);
        
               MmsValue* dataSetValue123= IedConnection_readObject(con, &error, "simpleIOGenericIO/GGIO1.AnIn2.mag.f", IEC61850_FC_MX);
               if(dataSetValue123 != NULL) {
                   //int v = MmsValue_toInt32(dataSetValue123);
                   float vv=     MmsValue_toFloat(dataSetValue123);
                   printf("read int32 value: %.3f, type=%d\n", vv);
                   MmsValue_delete(dataSetValue123);
               }
                
        
        bool rptEna = ClientReportControlBlock_getRptEna(rcb);//暂时屏蔽

        printf("RptEna = %i\n", rptEna);

        /* Install handler for reports */
       // IedConnection_installReportHandler(con, "simpleIOGenericIO/LLN0.RP.EventsRCB01",
        //        ClientReportControlBlock_getRptId(rcb), reportCallbackFunction, NULL);
        /                         simpleIOGenericIO/LLN0.RP.EventsRCB01
        IedConnection_installReportHandler(con, "simpleIOGenericIO/GGI01.MX.AnIn1.mag.f",
            ClientReportControlBlock_getRptId(rcb), reportCallbackFunction, NULL);
        /

        /* Set trigger options and enable report */
        ClientReportControlBlock_setTrgOps(rcb, TRG_OPT_DATA_UPDATE | TRG_OPT_INTEGRITY | TRG_OPT_GI);
        ClientReportControlBlock_setRptEna(rcb, true);
        ClientReportControlBlock_setIntgPd(rcb, 5000);
        IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_RPT_ENA | RCB_ELEMENT_TRG_OPS | RCB_ELEMENT_INTG_PD, true);

        if (error != IED_ERROR_OK)
            printf("report activation failed (code: %i)\n", error);

        Thread_sleep(1000);

        /* trigger GI report */
        ClientReportControlBlock_setGI(rcb, true);
        IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_GI, true);

        if (error != IED_ERROR_OK)
            printf("Error triggering a GI report (code: %i)\n", error);

        Thread_sleep(60000);

        /* disable reporting */
        ClientReportControlBlock_setRptEna(rcb, false);
        IedConnection_setRCBValues(con, &error, rcb, RCB_ELEMENT_RPT_ENA, true);

        if (error != IED_ERROR_OK)
            printf("disable reporting failed (code: %i)\n", error);

        ClientDataSet_destroy(clientDataSet);

        ClientReportControlBlock_destroy(rcb);

        close_connection:

        IedConnection_close(con);
    }
    else {
        printf("Failed to connect to %s:%i\n", hostname, tcpPort);
        getchar();
    }

    IedConnection_destroy(con);
}

 

以下是服务端

/*
 *  server_example_config_file.c
 *
 *  This example shows how to use dynamic server data model with a configuration file.
 *
 *  - How to open and parse the model configuration file
 *  - How to access data attributes by object reference strings
 *  - How to access data attributes by short addresses
 *
 *  Note: If building with cmake the vmd-filestore folder containing the configuration file
 *  (model.cfg) has to be copied to the folder where the example is executed!
 *  The configuration file can be created from the SCL(ICD) file with the Java tool genconfig.jar
 *  that is included in the source distribution of libiec61580.
 *
 */

#include "iec61850_server.h"
#include "hal_thread.h"
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>

#include "hal_filesystem.h"
#include "iec61850_config_file_parser.h"
#include "script.h"

static int running = 0;

void sigint_handler(int signalId)
{
    running = 0;
}

int
main(int argc, char** argv)
{
    //QRamRT* ramrt;
    int tcpPort = 102;

    if (argc > 1) {
        tcpPort = atoi(argv[1]);
    }

    /* open configuration file */
    FileHandle configFile = FileSystem_openFile("model.cfg", false);

    if (configFile == NULL) {
        printf("Error opening config file!\n");
        return 1;
    }
    if(!OpenRamRt())
    {
        printf("open ramrt \n");
        return 0 ;
    }
    for(int k=0;k<300;k++)
     SetItemValue(0,k,k);
    /* parse the configuration file and create the data model */
    IedModel* model = ConfigFileParser_createModelFromConfigFile(configFile);

    FileSystem_closeFile(configFile);

    if (model == NULL) {
        printf("Error parsing config file!\n");
        return 1;
    }

    IedServer iedServer = IedServer_create(model);

    /* Access to data attributes by object reference */

   
    DataAttribute* anIn1_t = (DataAttribute*)
            IedModel_getModelNodeByObjectReference(model, "simpleIOGenericIO/GGIO1.AnIn1.t");

    
    /* Access to data attributes by short address */
    DataAttribute* anIn2_mag = (DataAttribute*)
            IedModel_getModelNodeByShortAddress(model, 101);

    DataAttribute* anIn2_t = (DataAttribute*)
            IedModel_getModelNodeByShortAddress(model, 102);

    DataAttribute* anIn2_mag_f = NULL;

    if (anIn2_mag == NULL)
        printf("Error getting AnIn2.mag data attribute!\n");
    else
        anIn2_mag_f = (DataAttribute*) ModelNode_getChild((ModelNode*) anIn2_mag, "f");

    IedServer_start(iedServer, tcpPort);

    if (!IedServer_isRunning(iedServer)) {
        printf("Starting server failed! Exit.\n");
        IedServer_destroy(iedServer);
        exit(-1);
    }

    running = 1;

    signal(SIGINT, sigint_handler);

    float val = 0.f;

    MmsValue* floatValue = MmsValue_newFloat(val);
    DataAttribute* anIn1_mag_f;
    while (running) {
        for(int i=0;i<100;i++)
         {  
             val =GetItemValue(0,i);
             char tagname[200];
             char tagname1[200];
             sprintf(tagname,"simpleIOGenericIO/GGIO1.AnIn%d.mag.f",i+1);
             sprintf(tagname1,"simpleIOGenericIO/GGIO1.AnIn%d.mag.f\n",i+1);
             printf(tagname1);
             anIn1_mag_f= (DataAttribute*)
            IedModel_getModelNodeByObjectReference(model, tagname);
            
           if (anIn1_mag_f != NULL) {
            

            MmsValue_setFloat(floatValue, val);

            IedServer_lockDataModel(iedServer);

            MmsValue_setUtcTimeMs(anIn1_t->mmsValue, Hal_getTimeInMs());
            IedServer_updateAttributeValue(iedServer, anIn1_mag_f, floatValue);//赋值

            IedServer_unlockDataModel(iedServer);
           }
           
        }       

        val += 0.01f;

        Thread_sleep(200);
    }

    MmsValue_delete(floatValue);

    IedServer_stop(iedServer);

    IedServer_destroy(iedServer);

    IedModel_destroy(model);
} /* main() */

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值