Wiki »
设备网络SDK¶
- 环境
1)下载“设备网络SDK”:https://open.hikvision.com/osp#%E8%AE%BE%E5%A4%87%E9%9B%86%E6%88%90SDK 2)拷贝“MakeAll”下所有的文件(包括子文件)到程序编译目录,不管有没有用到,都需要,不然可能会报错:NET_DVR_DVROPRATEFAILED //DVR操作失败
- 代码
查看...查看...
#include <stdio.h> #include <iostream> #include "HCNetSDK.h" #include <time.h> #include <string.h> #include <unistd.h> using namespace std; void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser) { printf("========================\n"); char tempbuf[256] = {0}; switch(dwType) { case EXCEPTION_RECONNECT: //预览时重连 printf("----------reconnect--------%ld\n", time(NULL)); break; default: break; } } void call_back(DWORD dwType, void *lpBuffer, DWORD dwBufLen, void *pUserData) { printf("========================0\n"); if(NET_SDK_CALLBACK_TYPE_DATA == dwType) { printf("========================1\n"); } } int main() { //--------------------------------------- // 初始化 NET_DVR_Init(); //设置连接时间与重连时间 NET_DVR_SetConnectTime(2000, 1); NET_DVR_SetReconnect(10000, true); //--------------------------------------- //设置异常消息回调函数 NET_DVR_SetExceptionCallBack_V30(0, NULL,g_ExceptionCallBack, NULL); //--------------------------------------- // 注册设备 LONG lUserID; //登录参数,包括设备地址、登录用户、密码等 NET_DVR_USER_LOGIN_INFO struLoginInfo = {0}; struLoginInfo.bUseAsynLogin = 0; //同步登录方式 struLoginInfo.wPort = 8000; //设备服务端口 strcpy(struLoginInfo.sDeviceAddress, "192.168.36.140"); //设备IP地址 strcpy(struLoginInfo.sUserName, "admin"); //设备登录用户名 strcpy(struLoginInfo.sPassword, "opendrive1"); //设备登录密码 // strcpy(struLoginInfo.sDeviceAddress, "192.168.120.68"); //设备IP地址 // strcpy(struLoginInfo.sUserName, "admin"); //设备登录用户名 // strcpy(struLoginInfo.sPassword, "opendrive321"); //设备登录密码 //设备信息, 输出参数 NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = {0}; lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40); if (lUserID < 0) { printf("Login failed, error code: %d\n", NET_DVR_GetLastError()); NET_DVR_Cleanup(); return -1; } printf("The max number of analog channels: %d\n", struDeviceInfoV40.struDeviceV30.byChanNum); //模拟通道个数 printf("The max number of IP channels: %d\n", struDeviceInfoV40.struDeviceV30.byIPChanNum);//IP 通道个数 printf("The start number of IP channel: %d\n", struDeviceInfoV40.struDeviceV30.byStartDChan);//IP 通道起始通道号 char buf[2048] = { 0 }; DWORD dwOutBufferSize = 2048; DWORD lpBytesReturned = 0; LONG channel = 1; // 开始远程控制(未测试成功) // NET_DVR_PTZLOCKINFO_COND ptzLock = { 0 }; // ptzLock.dwSize = sizeof(NET_DVR_PTZLOCKINFO_COND); // ptzLock.dwChannel = channel; // char out[2048] = { 0 }; // LONG handle = NET_DVR_StartRemoteConfig(lUserID, NET_DVR_GET_PTZLOCKINFO, &ptzLock, sizeof(NET_DVR_PTZLOCKINFO_COND), call_back, out); // if(handle == -1) // { // printf("NET_DVR_StartRemoteConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // 获取水平参数、垂直参数、变倍参数范围(只有云台通道有效) // if(!NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_PTZSCOPE, channel, buf, dwOutBufferSize, &lpBytesReturned)) // { // printf("NET_DVR_GetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // printf("PRE==============================\n"); // LPNET_DVR_PTZSCOPE pScope = (LPNET_DVR_PTZSCOPE)buf; // printf("wPanPosMin: 0x%04x\n", pScope->wPanPosMin); // printf("wPanPosMax: 0x%04x\n", pScope->wPanPosMax); // printf("wTiltPosMin: 0x%04x\n", pScope->wTiltPosMin); // printf("wTiltPosMax: 0x%04x\n", pScope->wTiltPosMax); // printf("wZoomPosMin: 0x%04x\n", pScope->wZoomPosMin); // printf("wZoomPosMax: 0x%04x\n", pScope->wZoomPosMax); // 获取/设置/获取相机当前水平参数、垂直参数、变倍参数(重要)(只有云台通道有效) // if(!NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_PTZPOS, channel, buf, dwOutBufferSize, &lpBytesReturned)) // { // printf("NET_DVR_GetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // printf("PRE==============================\n"); // LPNET_DVR_PTZPOS pPos = (LPNET_DVR_PTZPOS)buf; // printf("wAction: %d\n", pPos->wAction); // printf("wPanPos: 0x%04x\n", pPos->wPanPos); // printf("wTiltPos: 0x%04x\n", pPos->wTiltPos); // printf("wZoomPos: 0x%04x\n", pPos->wZoomPos); // // pPos->wPanPos = 0x0988; // pPos->wTiltPos = 0x0086; // pPos->wZoomPos = 0x0010; // if(!NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_PTZPOS, channel, pPos, lpBytesReturned)) // { // printf("NET_DVR_SetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // // sleep(2); // // if(!NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_PTZPOS, channel, buf, dwOutBufferSize, &lpBytesReturned)) // { // printf("NET_DVR_GetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // printf("POST==============================\n"); // pPos = (LPNET_DVR_PTZPOS)buf; // printf("wAction: %d\n", pPos->wAction); // printf("wPanPos: 0x%04x\n", pPos->wPanPos); // printf("wTiltPos: 0x%04x\n", pPos->wTiltPos); // printf("wZoomPos: 0x%04x\n", pPos->wZoomPos); // 设置手动跟踪参数(未测试成功) // if(!NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_PTZ_TRACK_PARAM, channel, buf, dwOutBufferSize, &lpBytesReturned)) // { // printf("NET_DVR_GetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // printf("==============================%d\n", lpBytesReturned); // LPNET_DVR_PTZ_TRACK_PARAM ptztrack = (LPNET_DVR_PTZ_TRACK_PARAM)buf; // printf("byTrackMode: %d\n", ptztrack->byTrackMode); // printf("byLinkageTarget: %d\n", ptztrack->byLinkageTarget); // printf("byAutoTrackEnable: %d\n", ptztrack->byAutoTrackEnable); // // ptztrack->byTrackMode = 0; // ptztrack->byAutoTrackEnable = 1; // if(!NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_PTZ_TRACK_PARAM, channel, buf, lpBytesReturned)) // { // printf("NET_DVR_SetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // 设置手动跟踪(未测试成功) // NET_DVR_PTZ_MANUALTRACE manualTrace = { 0 }; // manualTrace.dwSize = sizeof(NET_DVR_PTZ_MANUALTRACE); // manualTrace.dwChannel = channel; // manualTrace.byTrackType = 0; // manualTrace.byLinkageType = 0; // if(!NET_DVR_RemoteControl(lUserID, NET_DVR_CONTROL_PTZ_MANUALTRACE, &manualTrace, sizeof(manualTrace))) // { // printf("NET_DVR_RemoteControl failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // 解锁(未测试成功) // if(!NET_DVR_GetDVRConfig(lUserID, NET_DVR_GET_PTZLOCKCFG, channel, buf, dwOutBufferSize, &lpBytesReturned)) // { // printf("NET_DVR_GetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // LPNET_DVR_PTZ_LOCKCFG pLck = (LPNET_DVR_PTZ_LOCKCFG)buf; // printf("dwSize: 0x%04x\n", pLck->dwSize); // printf("byWorkMode : 0x%04x\n", pLck->byWorkMode); // pLck->byWorkMode = 0; // if(!NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_PTZLOCKCFG, channel, buf, lpBytesReturned)) // { // printf("NET_DVR_SetDVRConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } // 云台控制(只有云台通道有效) DWORD dwPTZCommand = PAN_LEFT; //云台左转 DWORD dwStop = 0; //开始转动 if(!NET_DVR_PTZControl_Other(lUserID, channel, dwPTZCommand, dwStop)) { printf("PAN_LEFT start failed, error code: %d\n", NET_DVR_GetLastError()); NET_DVR_Logout(lUserID); NET_DVR_Cleanup(); return -1; } sleep(2); //云台控制接口下发命令之后是云台持续运动的,持续转动5秒之后再调用接口下发停止转动命令 dwStop = 1; //停止转动 if(!NET_DVR_PTZControl_Other(lUserID, channel, dwPTZCommand, dwStop)) { printf("PAN_LEFT stop failed, error code: %d\n", NET_DVR_GetLastError()); } // 停止远程控制(未测试成功) // if(!NET_DVR_StopRemoteConfig(handle)) // { // printf("NET_DVR_StopRemoteConfig failed, error code: %d\n", NET_DVR_GetLastError()); // NET_DVR_Logout(lUserID); // NET_DVR_Cleanup(); // return -1; // } //注销用户 NET_DVR_Logout(lUserID); //释放SDK资源 NET_DVR_Cleanup(); return 0; }
- 编译
g++ main.cpp -o test -L./ -lhcnetsdk
- 其它
查看...查看...
channel-1云台初始位置 wAction: 1 wPanPos: 0x0988 wTiltPos: 0x0086 wZoomPos: 0x0010 channel-2/channel-3枪机初始位置(枪机使用SDK设置不了,所以获取不准确) wAction: 1 wPanPos: 0x0000 wTiltPos: 0x0000 wZoomPos: 0x0000 1. 能否获取当前云台的水平、垂直位置,如何获取云台变倍值。 如何获取云台当前位置\当前坐标。 如何转到云台到指定的位置。 设备的云台PTZ范围可以通过NET_DVR_GetDVRConfig(命令: NET_DVR_GET_PTZSCOPE)获取,如果最小值大于最大值,说明最小值是负数,需要减去360度。 通过NET_DVR_GetDVRConfig(命令: NET_DVR_GET_PTZPOS)获取的PTZ值。实际显示的PTZ值是获取到的十六进制值的十分之一,如获取的水平参数P的值是0x1750,实际显示的P值为175度;获取到的垂直参数T的值是0x0789,实际显示的T值为78.9度;获取到的变倍参数Z的值是0x1100,实际显示的Z值为110倍。如果大于取值范围,说明是负数,需要减去360度。 通过NET_DVR_SetDVRConfig(命令:NET_DVR_SET_PTZPOS)可以设置云台PTZ坐标,云台直接转动到指定的位置。 2. 如何判断设备是否在线 SDK是主动登录设备的,正常登录说明设备在线,登录设备之后需要判断设备是否掉线,有两种方式: 1)NET_DVR_SetExceptionCallBack_V30设置异常消息回调函数,网络异常连接断开或者恢复重连,都会回调获取消息。 2)调用NET_DVR_RemoteControl(命令: NET_DVR_CHECK_USER_STATUS)手动判断设备是否在线。 3. ISAPI的一些抓包 <?xml version="1.0" encoding="UTF-8"?>\r\n<PTZData><pan>0</pan><tilt>60</tilt></PTZData> <?xml version="1.0" encoding="UTF-8"?>\r\n<ManualTraceEvidenceMode><EvidenceMode>true</EvidenceMode></ManualTraceEvidenceMode>
- 参考
https://open.hikvision.com/docs/docId?productId=5cda567cf47ae80dd41a54b3&version=%2F16e18c75bd644f1dbf9e8301d6fc9b73&tagPath=%E9%80%9A%E7%94%A8%26%E8%A7%86%E9%A2%91-%E4%BA%91%E5%8F%B0%E6%8E%A7%E5%88%B6-%E4%BA%91%E5%8F%B0%E5%9F%BA%E6%9C%AC%E6%8E%A7%E5%88%B6