libIEC61850:开源IEC 61850协议库使用指南
项目概述
libIEC61850是由MZ Automation开发的开放源码库,主要用于实现IEC 61850标准中的通信协议。该库完全符合IEC 61850标准,支持MMS(制造报文规范)、GOOSE(通用面向对象变电站事件)及SV(采样值)等关键协议。
libIEC61850提供了客户端和服务器端的应用编程接口(API),采用C语言(符合C99标准)开发,确保最大程度的可移植性。它可以在嵌入式系统和运行Linux、Windows、MacOS的PC上使用,已成功应用于多个商业软件产品和设备中。
核心特性
- 协议支持完整:支持MMS客户端/服务器、GOOSE(IEC 61850-8-1)、采样值(SV - IEC 61850-9-2)
- 报告功能强大:支持缓冲和非缓冲报告、在线报告控制块配置
- 数据服务全面:数据访问服务(获取数据、设置数据)、在线数据模型发现和浏览
- 数据集服务:所有数据集服务(获取值、设置值、浏览)、动态数据集服务(创建和删除)
- 日志服务灵活:提供连接自定义数据库的抽象接口,附带sqlite实现
- 文件服务完善:MMS文件服务(浏览、获取文件、设置文件、删除/重命名文件)
- 安全特性:支持TLS加密通信(IEC 62351-3/4),保护数据传输过程
- 多语言支持:提供C和C#/.NET API接口
环境搭建与构建
克隆仓库
git clone https://gitcode.com/gh_mirrors/li/libiec61850.git cd libiec61850 使用CMake构建
mkdir build cd build cmake .. make sudo make install # 安装至系统目录 构建示例程序
在项目根目录下执行:
make examples 构建成功后,可以在项目根目录和build目录中找到二进制文件和静态库文件(libiec61850.a)。
快速入门示例
服务器端示例
以下是一个基本的IEC 61850服务器示例,展示如何创建服务器实例并处理控制命令:
#include "iec61850_server.h" #include "hal_thread.h" #include <signal.h> #include <stdlib.h> #include <stdio.h> #include <math.h> #include "static_model.h" static int running = 0; static IedServer iedServer = NULL; void sigint_handler(int signalId) { running = 0; } static ControlHandlerResult controlHandlerForBinaryOutput(ControlAction action, void* parameter, MmsValue* value, bool test) { if (test) return CONTROL_RESULT_FAILED; if (MmsValue_getType(value) == MMS_BOOLEAN) { printf("received binary control command: "); if (MmsValue_getBoolean(value)) printf("on\n"); else printf("off\n"); } else return CONTROL_RESULT_FAILED; uint64_t timeStamp = Hal_getTimeInMs(); IedServer_updateUTCTimeAttributeValue(iedServer, parameter, timeStamp); IedServer_updateAttributeValue(iedServer, parameter, value); return CONTROL_RESULT_OK; } int main(int argc, char** argv) { int tcpPort = 102; printf("Using libIEC61850 version %s\n", LibIEC61850_getVersionString()); /* 创建服务器配置对象 */ IedServerConfig config = IedServerConfig_create(); /* 设置报告缓冲区大小 */ IedServerConfig_setReportBufferSize(config, 200000); /* 创建IEC 61850服务器实例 */ iedServer = IedServer_createWithConfig(&iedModel, NULL, config); IedServerConfig_destroy(config); /* 设置服务器标识信息 */ IedServer_setServerIdentity(iedServer, "MZ", "basic io", "1.6.0"); /* 安装控制处理器 */ IedServer_setControlHandler(iedServer, IEDMODEL_GenericIO_GGIO1_SPCSO1, (ControlHandler) controlHandlerForBinaryOutput, IEDMODEL_GenericIO_GGIO1_SPCSO1); /* 启动服务器监听 */ IedServer_start(iedServer, tcpPort); running = 1; signal(SIGINT, sigint_handler); while (running) { /* 更新模拟量测量值 */ float value = sinf(Hal_getTimeInMs() / 1000.f); IedServer_updateFloatAttributeValue(iedServer, IEDMODEL_GenericIO_GGIO1_AnIn1_mag_f, value); Thread_sleep(100); } /* 停止服务器并清理资源 */ IedServer_stop(iedServer); IedServer_destroy(iedServer); return 0; } 客户端示例
以下是一个简单的IEC 61850客户端示例,展示如何连接服务器并读取数据:
#include "iec61850_client.h" #include <stdlib.h> #include <stdio.h> #include "hal_thread.h" int main(int argc, char** argv) { char* hostname = argc > 1 ? argv[1] : "localhost"; int tcpPort = argc > 2 ? atoi(argv[2]) : 102; IedClientError error; IedConnection con = IedConnection_create(); printf("Connecting to %s:%i\n", hostname, tcpPort); IedConnection_connect(con, &error, hostname, tcpPort); if (error == IED_ERROR_OK) { printf("Connected successfully\n"); /* 读取模拟量测量值 */ MmsValue* value = IedConnection_readObject(con, &error, "simpleIOGenericIO/GGIO1.AnIn1.mag.f", IEC61850_FC_MX); if (value != NULL && MmsValue_getType(value) == MMS_FLOAT) { float fval = MmsValue_toFloat(value); printf("Read float value: %f\n", fval); MmsValue_delete(value); } IedConnection_close(con); } else { printf("Failed to connect (error code: %i)\n", error); } IedConnection_destroy(con); return 0; } 运行示例程序
构建完成后,可以运行示例程序进行测试:
cd examples/server_example_basic_io sudo ./server_example_basic_io 在另一个终端中运行客户端:
cd examples/iec61850_client_example1 ./client_example1 TLS支持配置
libIEC61850支持TLS加密通信,可以使用mbedTLS库来实现:
使用mbedTLS 2.28
make WITH_MBEDTLS=1 使用mbedTLS 3.6
make WITH_MBEDTLS3=1 使用CMake构建时,如果third_party/mbedtls目录中存在相应的mbedtls版本,会自动启用TLS支持。
最佳实践建议
1. 错误处理
在实际应用中,务必添加完善的错误处理机制。所有libIEC61850 API函数都会返回错误代码,应该检查这些代码以确保操作成功。
2. 资源管理
及时释放分配的资源,使用相应的destroy或delete函数来避免内存泄漏。
3. 线程安全
在多线程环境中使用libIEC61850时,注意线程同步问题。服务器API通常提供数据模型锁定机制来确保线程安全。
4. 性能优化
根据实际应用场景调整配置参数,如报告缓冲区大小、最大连接数等,以达到最佳性能。
5. 安全性考虑
在生产环境中,始终启用TLS加密通信,并妥善管理证书和密钥。
许可证信息
libIEC61850采用GNU General Public License v3.0许可证。对于商业项目,MZ Automation GmbH提供商业许可证和支持选项。
技术支持与贡献
该库由MZ Automation GmbH提供商业支持和技术服务。如果您希望为项目做出贡献,请联系[email protected]获取贡献者许可协议。
通过遵循本指南,您可以快速开始使用libIEC61850库来开发符合IEC 61850标准的电力自动化系统应用程序。库中提供的丰富示例代码是学习和理解API用法的绝佳资源。