C#代码如何实现海康相机数字IO输出
时间: 2024-04-01 07:37:11 浏览: 170
以下是C#代码示例,用于实现海康相机数字IO输出:
```c#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace DigitalIOControlDemo
{
class Program
{
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Init();
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Cleanup();
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_Login_V30(string sDVRIP, ushort wDVRPort, string sUserName, string sPassword, ref NET_DVR_DEVICEINFO_V30 lpDeviceInfo);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_Logout(int lUserID);
[DllImport("HCNetSDK.dll")]
public static extern bool NET_DVR_SetDVRConfig(int lUserID, uint dwCommand, int lChannel, ref NET_DVR_DIGITAL_IO_CONTROL lpInBuffer, uint dwInBufferSize);
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DEVICEINFO_V30
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 48)]
public string sSerialNumber;
public byte byAlarmInPortNum;
public byte byAlarmOutPortNum;
public byte byDiskNum;
public byte byDVRType;
public byte byChanNum;
public byte byStartChan;
public byte byAudioChanNum;
public byte byIPChanNum;
public byte byZeroChanNum;
public byte byMainProto;
public byte bySubProto;
public byte bySupport;
public byte bySupport1;
public byte bySupport2;
public ushort wDevType;
public byte bySupport3;
public byte byMultiStreamProto;
public byte byStartDChan;
public byte byStartDTalkChan;
public byte byHighDChanNum;
public byte bySupport4;
public byte byLanguageType;
public byte byVoiceInChanNum;
public byte byRes3;
public byte byMirrorChanNum;
public ushort wStartMirrorChanNo;
public byte byRes2;
public byte bySupport5;
public int lStartUserRight; // the begin user right
public int lEndUserRight; // the end user right
public byte byMirrorChanNum2;
public byte byStartListenChan;
public byte byReserve3;
public byte bySupport6;
public int lSerialNum; //Serial number
public byte bySupport7;
public byte byDevTypeName;
public byte byRes4;
public byte bySupport8;
public byte bySupport9;
public byte bySupport10;
public byte bySupport11;
public byte bySupport12;
public byte bySupport13;
public byte bySupport14;
public byte bySupport15;
public byte bySupport16;
public byte bySupport17;
public byte bySupport18;
public byte bySupport19;
public byte bySupport20;
public byte bySupport21;
public byte bySupport22;
public byte bySupport23;
public byte bySupport24;
public byte bySupport25;
public byte bySupport26;
public byte bySupport27;
public byte bySupport28;
public byte bySupport29;
public byte bySupport30;
public byte bySupport31;
public byte bySupport32;
public byte bySupport33;
public byte bySupport34;
public byte bySupport35;
public byte bySupport36;
public byte bySupport37;
public byte bySupport38;
public byte bySupport39;
public byte bySupport40;
public byte bySupport41;
public byte bySupport42;
public byte bySupport43;
public byte bySupport44;
public byte bySupport45;
public byte bySupport46;
public byte bySupport47;
public byte bySupport48;
public byte bySupport49;
public byte bySupport50;
public byte bySupport51;
public byte bySupport52;
public byte bySupport53;
public byte bySupport54;
public byte bySupport55;
public byte bySupport56;
public byte bySupport57;
public byte bySupport58;
public byte bySupport59;
public byte bySupport60;
public byte bySupport61;
public byte bySupport62;
public byte bySupport63;
public byte bySupport64;
}
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DIGITAL_IO_CONTROL
{
public uint dwSize;
public uint dwChannel;
public uint dwControlType;
public NET_DVR_DIGITAL_IO struDigitalIO;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 76, ArraySubType = UnmanagedType.I1)]
public byte[] byRes;
}
[StructLayout(LayoutKind.Sequential)]
public struct NET_DVR_DIGITAL_IO
{
public uint dwChannel;
public uint dwWorkType;
public uint dwTrigType;
public byte byDelayTime;
public byte byRes1;
public ushort wRes1;
public uint dwAlarmOutPut;
public uint dwOutputDuration;
public byte byMode;
public byte byPolarity;
public byte byRes2;
public byte bySignalLatch;
public byte byRes3;
public byte bySignalType;
public byte byRes4;
public byte byRes5;
public byte byRes6;
public byte byRes7;
public uint dwSignalInPut;
public uint dwInputLevel;
public byte byRes8;
public byte byRes9;
public ushort wRes2;
public uint dwTrigChannel;
public byte byTrigDelayTime;
public byte byTrigLatch;
public byte byTrigSignalType;
public byte byRes10;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4, ArraySubType = UnmanagedType.I1)]
public byte[] byRes11;
}
static void Main(string[] args)
{
// 初始化 SDK
NET_DVR_Init();
// 登录相机
NET_DVR_DEVICEINFO_V30 deviceInfo = new NET_DVR_DEVICEINFO_V30();
int lUserID = NET_DVR_Login_V30("192.168.1.64", 8000, "admin", "12345", ref deviceInfo);
if (lUserID < 0)
{
Console.WriteLine("登录相机失败,错误码:" + NET_DVR_GetLastError().ToString());
return;
}
// 设置数字 IO 参数
NET_DVR_DIGITAL_IO_CONTROL struIOControl = new NET_DVR_DIGITAL_IO_CONTROL();
struIOControl.dwSize = (uint)Marshal.SizeOf(struIOControl);
struIOControl.dwChannel = 1; // IO 通道号
struIOControl.dwControlType = 0; // 输出模式
NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_IO_CONTROL_CFG, 0, ref struIOControl, (uint)Marshal.SizeOf(struIOControl));
// 启用输出信号
struIOControl.struDigitalIO.dwChannel = 1; // IO 通道号
struIOControl.struDigitalIO.dwWorkType = 1; // 手动模式
struIOControl.struDigitalIO.byMode = 1; // 输出高电平
NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_IO_CONTROL_CFG, 0, ref struIOControl, (uint)Marshal.SizeOf(struIOControl));
// 关闭输出信号
struIOControl.struDigitalIO.byMode = 0; // 输出低电平
NET_DVR_SetDVRConfig(lUserID, NET_DVR_SET_IO_CONTROL_CFG, 0, ref struIOControl, (uint)Marshal.SizeOf(struIOControl));
// 释放资源
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
}
}
}
```
注意:以上代码仅供参考,实际使用时需要根据相机型号和需求进行调整。
阅读全文