c#海康visionmaster二次开发每隔5秒监控相机是否在线
时间: 2023-08-05 08:10:40 浏览: 177
海康Vision Master SDK 二次开发
您好!要每隔5秒钟监控相机是否在线,您可以使用C#中的定时器Timer,代码如下:
```csharp
using System;
using System.Timers;
using System.Runtime.InteropServices;
using HCNetSDK;
class Program
{
static int loginHandle = -1;
static void Main(string[] args)
{
// 初始化 SDK
HCNetSDK.NET_DVR_Init();
// 每隔5秒钟检查相机是否在线
Timer timer = new Timer();
timer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
timer.Interval = 5000;
timer.Enabled = true;
// 等待用户输入任意字符退出程序
Console.ReadLine();
// 关闭定时器、登出相机并释放资源
timer.Enabled = false;
if (loginHandle >= 0)
{
HCNetSDK.NET_DVR_Logout(loginHandle);
}
HCNetSDK.NET_DVR_Cleanup();
}
static void OnTimedEvent(object source, ElapsedEventArgs e)
{
Console.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
// 如果相机未登录,则登录相机
if (loginHandle < 0)
{
NET_DVR_USER_LOGIN_INFO loginInfo = new NET_DVR_USER_LOGIN_INFO();
loginInfo.sDeviceAddress = "192.168.1.100";
loginInfo.wPort = 8000;
loginInfo.sUserName = "admin";
loginInfo.sPassword = "12345";
loginInfo.bUseAsynLogin = false;
NET_DVR_DEVICEINFO_V40 deviceInfo = new NET_DVR_DEVICEINFO_V40();
loginHandle = HCNetSDK.NET_DVR_Login_V40(ref loginInfo, ref deviceInfo);
if (loginHandle < 0)
{
Console.WriteLine("登录相机失败:{0}", HCNetSDK.NET_DVR_GetLastError());
return;
}
}
// 获取相机状态
NET_DVR_WORK_STATUS workStatus = new NET_DVR_WORK_STATUS();
int bufferSize = Marshal.SizeOf(workStatus);
IntPtr bufferPtr = Marshal.AllocHGlobal(bufferSize);
Marshal.StructureToPtr(workStatus, bufferPtr, true);
int bytesReturned = 0;
bool success = HCNetSDK.NET_DVR_GetDVRConfig(loginHandle, HCNetSDK.NET_DVR_GET_WORK_STATUS, -1, bufferPtr, bufferSize, out bytesReturned);
workStatus = (NET_DVR_WORK_STATUS)Marshal.PtrToStructure(bufferPtr, typeof(NET_DVR_WORK_STATUS));
// 判断相机是否在线
if (workStatus.dwDeviceStatic == 1)
{
Console.WriteLine("相机在线");
}
else
{
Console.WriteLine("相机离线");
// 登出相机、清除登录句柄
HCNetSDK.NET_DVR_Logout(loginHandle);
loginHandle = -1;
}
}
}
```
注意,以上代码仅为示例,仅供参考。具体实现还需根据您的具体需求进行调整。
阅读全文