libiec61850
https://github.com/mz-automation/libiec61850
v1.6
簡介
libiec61850 是一個開源(GPLv3)的 IEC 61850 客戶端和服務器庫實現,支持 MMS、GOOSE 和 SV 協議。它使用 C 語言(根據 C99 標準)實現,以提供最大的可移植性。它可以用于在運行 Linux、Windows 和 MacOS 的嵌入式系統和 PC 上實現符合 IEC 61850 的客戶端和服務器應用程序。包含一組簡單的示例應用程序,可以作為實現自己的 IEC 61850 兼容設備或與 IEC 61850 設備通信的起點。該庫已在許多商業軟件產品和設備中得到成功應用。
第三方庫
各第三方庫的設置參見libiec61850-1.6\third_party下各說明文件
mbedtls-3.6.0
https://github.com/Mbed-TLS/mbedtls
優先使用mbedtls-3.6.0
Mbed TLS 是一個實現加密原語、X.509 證書操作以及 SSL/TLS 和 DTLS 協議的 C 庫。
其小巧的代碼體積使其適用于嵌入式系統。
sqlite
與寫sqlite日志有關
https://sqlite.org/2025/sqlite-amalgamation-3490100.zip
winpcap 與win下運行有關,必須安裝WinPcap_4_1_3.exe并重啟
https://www.winpcap.org/install/bin/WinPcap_4_1_3.exe
https://www.winpcap.org/install/bin/WpdPack_4_1_2.zip
doxygen
https://www.doxygen.nl/
https://www.doxygen.nl/files/doxygen-1.13.2-setup.exe
linux下編譯
linux make編譯:
make examples
生成
libiec61850.a
運行測試
cd examples/server_example_basic_io
sudo ./server_example_basic_io
客戶端對應程序 iec61850_client_example1
make install //默認安裝到.install
make INSTALL_PREFIX=/usr/local install //指定安裝目錄
安裝目標將 API 頭文件和靜態庫復制到單個目錄中,
用于頭文件(INSTALL_PREFIX/include)
和靜態庫(INSTALL_PREFIX/lib)。
linux cmake編譯:
mkdir build
cd build
cmake …
make
sudo make install
win10下cmake vs編譯
cd C:\work\code\iec61850\libiec61850-1.6\winbuild
cmake -G “Visual Studio 17 2022” … -A x64
用vs2022打開winbuild下的sln文件
子工程介紹
hal
作用:libiec61850 的硬件抽象層(Hardware Abstraction Layer)子項目。
該模塊封裝了與操作系統相關的功能(如線程、套接字、定時器等),
使核心庫可以跨平臺運行。
在 Windows 下,hal 可能實現為:
基于 Win32 API 的線程和網絡接口。
替換 POSIX 兼容層(如 socket() 的 Windows 版本)。
生成路徑:libiec61850-1.6\winbuild\hal\Debug
iec61850
核心的靜態庫
生成路徑:libiec61850-1.6\winbuild\src\Debug
doc doc-net
與Doxygen有關,生成路徑 libiec61850-1.6\winbuild\doxydoc
ZERO_CHECK
作用:CMake 自動生成的“配置檢查”項目。
每次構建時,ZERO_CHECK 會先運行,檢查 CMakeLists.txt 或相關文件是否有變更。
如果有變更(如修改了編譯選項、添加/刪除了源文件),
它會自動重新生成 VS2022 的工程文件(.vcxproj 等)。
ALL_BUILD
作用:CMake 默認生成的“構建所有目標”項目。
RUN_TESTS
作用:運行項目的單元測試(如果項目配置了測試)。
PACKAGE
作用:生成分發包(如 ZIP、NSIS 安裝包等)。
典型輸出:libiec61850-1.6-win64.zip 或安裝程序。
INSTALL
作用:將編譯好的文件安裝到指定目錄(類似 make install)。
r_goose_publisher_example
工程以r_開頭的幾個sv goose示例程序與mbedtls tls加密庫有關
libiec61850-1.6\winbuild\config\stack_config.h 中有提示
sntp_example
SNTP(簡單網絡時間協議)是 NTP(Network Time Protocol) 的簡化版本,用于在計算機網絡中同步設備的系統時間。
輕量級:相比完整的 NTP,SNTP 實現更簡單,適合嵌入式系統或輕量級應用。
基于 UDP:默認使用 UDP 端口 123 進行通信。
時間同步:從 SNTP/NTP 服務器獲取精確的時間戳(通常來自原子鐘或 GPS 時間源)。
sntp_example源碼debug
sntp_client.c 代碼修改
libiec61850-1.6\src\sntp\sntp_client.c
parseResponseMessage中修改如下
if (self->userCallback){if (self->userCallbackParameter) {//add*((uint64_t*)self->userCallbackParameter) = trnsTime + self->lastRequestTimestamp;}self->userCallback(self->userCallbackParameter, true);}
sntp_example.c 代碼修改
libiec61850-1.6\examples\sntp_example\sntp_example.c
#include "sntp_client.h"
#include "hal_thread.h"
#include <signal.h>
#include <stdio.h>
#include <time.h>
#include <windows.h>static bool running = true;
static uint64_t g_nsTime = 0;#ifdef _WINDOWS
static BOOL WINAPI sigint_handler(DWORD signal) {if (signal == CTRL_C_EVENT) {printf("Ctrl+C received, exiting ...\n");running = false;return TRUE; // 表示已處理該事件}return FALSE;
}
#else
void sigint_handler(int signalId) {running = false;
}
#endif // _WINDOWSvoid show_time(nsSinceEpoch nsTime) {// 轉換為 Windows FILETIME (1601-01-01 為起點,100ns 單位)uint64_t t = (nsTime / 100ULL) + 116444736000000000ULL;FILETIME ft;ft.dwLowDateTime = (uint32_t)(t & 0xffffffff);ft.dwHighDateTime = (uint32_t)(t >> 32);SYSTEMTIME st;// 轉換為 SYSTEMTIME (便于讀取年月日時分秒)FileTimeToSystemTime(&ft, &st);SYSTEMTIME localSt;// 轉換為本地時間SystemTimeToTzSpecificLocalTime(NULL, &st, &localSt);printf(" Time: %04d-%02d-%02d %02d:%02d:%02d.%03d (nsTime==%llu)\n",localSt.wYear, localSt.wMonth, localSt.wDay,localSt.wHour, localSt.wMinute, localSt.wSecond,localSt.wMilliseconds, nsTime);fflush(stdout);
}static void sntpUserCallback(void* parameter, bool isSynced) {if (isSynced) {printf("SNTP: Clock synchronized %llu\n", *(uint64_t*)parameter);show_time(g_nsTime);}else {printf("SNTP: Clock not synchronized\n");}
}int main(int argc, char** argv) {SNTPClient client = SNTPClient_create();SNTPClient_addServer(client, "pool.ntp.org", 123);//"192.168.178.74"SNTPClient_setUserCallback(client, sntpUserCallback, (void*)&g_nsTime);SNTPClient_setPollInterval(client, 16);SNTPClient_start(client);
#ifdef _WINDOWSif (!SetConsoleCtrlHandler(sigint_handler, TRUE)) {printf("Error: Failed to set Ctrl+C handler!\n");}
#elsesignal(SIGINT, sigint_handler);
#endif // _WINDOWSwhile (running) {Thread_sleep(100);}SNTPClient_destroy(client);getchar();return 0;
}
SNTP: Failed to bind to port 123 問題處理
查詢123端口被占用問題
libiec61850-1.6\winbuild>netstat -ano | findstr “:123”
UDP 0.0.0.0:123 : 1852
UDP [::]:123 : 1852
libiec61850-1.6\winbuild>tasklist | findstr “1852”
svchost.exe 1852 Services 0 7,204 K
svchost.exe,通常是 w32time 服務。
停用w32time
net stop w32time
sc config w32time start= disabled
恢復w32time
sc config w32time start= auto
net start w32time