一、启动ONVIF服务见onvif模拟服务器-CSDN博客二、配置libcurl头文件及库1. libcurl头文件下载1. libcurl头文件下载https://resource-wangsu.helplook.net/docker_production/ktbr83/article/1ig1hhAP/attachments/curlheader.zip2. 设置头文件路径及链接库右键项目-属性2.1模拟运行设置头文件路径链接curl库2.2 HMI屏配置头文件路径链接curl库3.页面代码在页面中放个按钮增加点击事件进入代码编辑页3.1包含头文件#include curl.h3.2响应函数static size_t handleResponse(void *contents, size_t length, size_t nmemb, void *userp) { ((std::string*)userp)-append((char*)contents, length * nmemb); string s *(string*)userp;//返回结果 coutsendl;//打印出来 getRuntime()-getIVarMgr()-setChar(onvif_response, s);//赋给变量 return length * nmemb; }3.3按钮点击事件函数void Frm01::wMButton1_clk_cb(uint16_t code, LvEvent e) { /*wMButton1功能键的点击事件*/ std::string readBuffer; CURL *curl; CURLcode res; //从ONVIF_Device_TestTool中查看你需要的请求内容这里是GetDeviceInformation std::string postField R(?xml version1.0 encodingutf-8? soap:Envelope xmlns:soaphttp://www.w3.org/2003/05/soap-envelope xmlns:tdshttp://www.onvif.org/ver10/device/wsdl xmlns:tthttp://www.onvif.org/ver10/schema soap:Body tds:GetDeviceInformation / /soap:Body /soap:Envelope); curl curl_easy_init(); if (curl) { struct curl_slist *slist NULL; slist curl_slist_append(slist, Accept: */*); slist curl_slist_append(slist, Content-Type: text/xml); std::string content_length Content-Length: std::to_string(postField.size()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_HEADER, 0); curl_easy_setopt(curl, CURLOPT_URL, http://10.14.95.172:8000/onvif/device_service);//onvif服务地址 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postField.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleResponse); curl_easy_setopt(curl, CURLOPT_WRITEDATA, readBuffer); res curl_easy_perform(curl); if (res ! CURLE_OK) { std::cout Failed.res std::endl; } curl_easy_cleanup(curl); curl_slist_free_all(slist); } }三、模拟运行在模拟器运行点击按钮后在控制台可以看到响应内容* Trying 10.14.95.172:8000... * Connected to 10.14.95.172 (10.14.95.172) port 8000 (#0) POST /onvif/device_service HTTP/1.1 Host: 10.14.95.172:8000 Accept: */* Content-Type: text/xml Content-Length: 279 HTTP/1.1 200 OK Server: Happytime onvif server V11.1 Content-Type: text/xml Content-Length: 2680 Connection: close * Closing connection 0 ?xml version1.0 encodingUTF-8? s:Envelope xmlns:shttp://www.w3.org/2003/05/soap-envelope xmlns:ehttp://www.w3.org/2003/05/soap-encoding xmlns:wsahttp://www.w3.org/2005/08/addressing xmlns:xshttp://www.w3.org/2001/XMLSchema xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:wsawhttp://www.w3.org/2006/05/addressing/wsdl xmlns:wsnthttp://docs.oasis-open.org/wsn/b-2 xmlns:wstophttp://docs.oasis-open.org/wsn/t-1 xmlns:wsntwhttp://docs.oasis-open.org/wsn/bw-2 xmlns:wsrf-rwhttp://docs.oasis-open.org/wsrf/rw-2 xmlns:wsrf-rhttp://docs.oasis-open.org/wsrf/r-2 xmlns:wsrf-bfhttp://docs.oasis-open.org/wsrf/bf-2 xmlns:wsdlhttp://schemas.xmlsoap.org/wsdl xmlns:wsoap12http://schemas.xmlsoap.org/wsdl/soap12 xmlns:httphttp://schemas.xmlsoap.org/wsdl/http xmlns:dhttp://schemas.xmlsoap.org/ws/2005/04/discovery xmlns:wsadishttp://schemas.xmlsoap.org/ws/2004/08/addressing xmlns:tthttp://www.onvif.org/ver10/schema xmlns:tns1http://www.onvif.org/ver10/topics xmlns:tdshttp://www.onvif.org/ver10/device/wsdl xmlns:trthttp://www.onvif.org/ver10/media/wsdl xmlns:tevhttp://www.onvif.org/ver10/events/wsdl xmlns:timghttp://www.onvif.org/ver20/imaging/wsdl xmlns:tsthttp://www.onvif.org/ver10/storage/wsdl xmlns:dnhttp://www.onvif.org/ver10/network/wsdl xmlns:pthttp://www.onvif.org/ver10/pacs xmlns:tr2http://www.onvif.org/ver20/media/wsdl xmlns:tptzhttp://www.onvif.org/ver20/ptz/wsdl xmlns:tanhttp://www.onvif.org/ver20/analytics/wsdl xmlns:axthttp://www.onvif.org/ver20/analytics xmlns:trphttp://www.onvif.org/ver10/replay/wsdl xmlns:tsehttp://www.onvif.org/ver10/search/wsdl xmlns:trchttp://www.onvif.org/ver10/recording/wsdl xmlns:tachttp://www.onvif.org/ver10/accesscontrol/wsdl xmlns:tdchttp://www.onvif.org/ver10/doorcontrol/wsdl xmlns:tmdhttp://www.onvif.org/ver10/deviceIO/wsdl xmlns:tthhttp://www.onvif.org/ver10/thermal/wsdl xmlns:tcrhttp://www.onvif.org/ver10/credential/wsdl xmlns:tarhttp://www.onvif.org/ver10/accessrules/wsdl xmlns:tschttp://www.onvif.org/ver10/schedule/wsdl xmlns:trvhttp://www.onvif.org/ver10/receiver/wsdl xmlns:tpvhttp://www.onvif.org/ver10/provisioning/wsdl xmlns:terhttp://www.onvif.org/ver10/error s:Body tds:GetDeviceInformationResponse tds:ManufacturerHappytimesoft/tds:Manufacturer tds:ModelIPCamera/tds:Model tds:FirmwareVersion2.4/tds:FirmwareVersion tds:SerialNumber123456/tds:SerialNumber tds:HardwareId1.0/tds:HardwareId /tds:GetDeviceInformationResponse /s:Body /s:Envelope
鑫通态HMI组态软件APUSIDE案例分享:libcurl+ONVIF协议测试
发布时间:2026/7/14 15:01:54
一、启动ONVIF服务见onvif模拟服务器-CSDN博客二、配置libcurl头文件及库1. libcurl头文件下载1. libcurl头文件下载https://resource-wangsu.helplook.net/docker_production/ktbr83/article/1ig1hhAP/attachments/curlheader.zip2. 设置头文件路径及链接库右键项目-属性2.1模拟运行设置头文件路径链接curl库2.2 HMI屏配置头文件路径链接curl库3.页面代码在页面中放个按钮增加点击事件进入代码编辑页3.1包含头文件#include curl.h3.2响应函数static size_t handleResponse(void *contents, size_t length, size_t nmemb, void *userp) { ((std::string*)userp)-append((char*)contents, length * nmemb); string s *(string*)userp;//返回结果 coutsendl;//打印出来 getRuntime()-getIVarMgr()-setChar(onvif_response, s);//赋给变量 return length * nmemb; }3.3按钮点击事件函数void Frm01::wMButton1_clk_cb(uint16_t code, LvEvent e) { /*wMButton1功能键的点击事件*/ std::string readBuffer; CURL *curl; CURLcode res; //从ONVIF_Device_TestTool中查看你需要的请求内容这里是GetDeviceInformation std::string postField R(?xml version1.0 encodingutf-8? soap:Envelope xmlns:soaphttp://www.w3.org/2003/05/soap-envelope xmlns:tdshttp://www.onvif.org/ver10/device/wsdl xmlns:tthttp://www.onvif.org/ver10/schema soap:Body tds:GetDeviceInformation / /soap:Body /soap:Envelope); curl curl_easy_init(); if (curl) { struct curl_slist *slist NULL; slist curl_slist_append(slist, Accept: */*); slist curl_slist_append(slist, Content-Type: text/xml); std::string content_length Content-Length: std::to_string(postField.size()); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_HEADER, 0); curl_easy_setopt(curl, CURLOPT_URL, http://10.14.95.172:8000/onvif/device_service);//onvif服务地址 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 30); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postField.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, handleResponse); curl_easy_setopt(curl, CURLOPT_WRITEDATA, readBuffer); res curl_easy_perform(curl); if (res ! CURLE_OK) { std::cout Failed.res std::endl; } curl_easy_cleanup(curl); curl_slist_free_all(slist); } }三、模拟运行在模拟器运行点击按钮后在控制台可以看到响应内容* Trying 10.14.95.172:8000... * Connected to 10.14.95.172 (10.14.95.172) port 8000 (#0) POST /onvif/device_service HTTP/1.1 Host: 10.14.95.172:8000 Accept: */* Content-Type: text/xml Content-Length: 279 HTTP/1.1 200 OK Server: Happytime onvif server V11.1 Content-Type: text/xml Content-Length: 2680 Connection: close * Closing connection 0 ?xml version1.0 encodingUTF-8? s:Envelope xmlns:shttp://www.w3.org/2003/05/soap-envelope xmlns:ehttp://www.w3.org/2003/05/soap-encoding xmlns:wsahttp://www.w3.org/2005/08/addressing xmlns:xshttp://www.w3.org/2001/XMLSchema xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:wsawhttp://www.w3.org/2006/05/addressing/wsdl xmlns:wsnthttp://docs.oasis-open.org/wsn/b-2 xmlns:wstophttp://docs.oasis-open.org/wsn/t-1 xmlns:wsntwhttp://docs.oasis-open.org/wsn/bw-2 xmlns:wsrf-rwhttp://docs.oasis-open.org/wsrf/rw-2 xmlns:wsrf-rhttp://docs.oasis-open.org/wsrf/r-2 xmlns:wsrf-bfhttp://docs.oasis-open.org/wsrf/bf-2 xmlns:wsdlhttp://schemas.xmlsoap.org/wsdl xmlns:wsoap12http://schemas.xmlsoap.org/wsdl/soap12 xmlns:httphttp://schemas.xmlsoap.org/wsdl/http xmlns:dhttp://schemas.xmlsoap.org/ws/2005/04/discovery xmlns:wsadishttp://schemas.xmlsoap.org/ws/2004/08/addressing xmlns:tthttp://www.onvif.org/ver10/schema xmlns:tns1http://www.onvif.org/ver10/topics xmlns:tdshttp://www.onvif.org/ver10/device/wsdl xmlns:trthttp://www.onvif.org/ver10/media/wsdl xmlns:tevhttp://www.onvif.org/ver10/events/wsdl xmlns:timghttp://www.onvif.org/ver20/imaging/wsdl xmlns:tsthttp://www.onvif.org/ver10/storage/wsdl xmlns:dnhttp://www.onvif.org/ver10/network/wsdl xmlns:pthttp://www.onvif.org/ver10/pacs xmlns:tr2http://www.onvif.org/ver20/media/wsdl xmlns:tptzhttp://www.onvif.org/ver20/ptz/wsdl xmlns:tanhttp://www.onvif.org/ver20/analytics/wsdl xmlns:axthttp://www.onvif.org/ver20/analytics xmlns:trphttp://www.onvif.org/ver10/replay/wsdl xmlns:tsehttp://www.onvif.org/ver10/search/wsdl xmlns:trchttp://www.onvif.org/ver10/recording/wsdl xmlns:tachttp://www.onvif.org/ver10/accesscontrol/wsdl xmlns:tdchttp://www.onvif.org/ver10/doorcontrol/wsdl xmlns:tmdhttp://www.onvif.org/ver10/deviceIO/wsdl xmlns:tthhttp://www.onvif.org/ver10/thermal/wsdl xmlns:tcrhttp://www.onvif.org/ver10/credential/wsdl xmlns:tarhttp://www.onvif.org/ver10/accessrules/wsdl xmlns:tschttp://www.onvif.org/ver10/schedule/wsdl xmlns:trvhttp://www.onvif.org/ver10/receiver/wsdl xmlns:tpvhttp://www.onvif.org/ver10/provisioning/wsdl xmlns:terhttp://www.onvif.org/ver10/error s:Body tds:GetDeviceInformationResponse tds:ManufacturerHappytimesoft/tds:Manufacturer tds:ModelIPCamera/tds:Model tds:FirmwareVersion2.4/tds:FirmwareVersion tds:SerialNumber123456/tds:SerialNumber tds:HardwareId1.0/tds:HardwareId /tds:GetDeviceInformationResponse /s:Body /s:Envelope