海康相机触发输入输出(含代码)

海康相机触发模式在这里插入图片描述

软件设置:先设置触发模式,在设置触发源。

目的:模拟编码器发送信号触发相机采集,通过一个矩形波信号触发采集图像。

计数器触发

说明书MSV客户端步骤:

在这里插入图片描述在这里插入图片描述
在这里插入图片描述

实验过程

1、编码器周长300mm,转一圈产生300个脉冲信号,编码器每产生一个脉冲信号时,代表皮带向前移动300mm/300=1mm.
2、皮带速度为1500mm/s,相机横向视野为750mm,因此相机没秒会拍摄1500/750=2张图像。
3、没经过500ms或者750/1mm=750个脉冲,拍一张图像。
4、程序上,收到一个编码器的脉冲信号,变量自增1,当变量的值可以被750整除时,进行采集。

实际客户端步骤

在这里插入图片描述
在这里插入图片描述

接线

绿色接地线,黄色接+,灰色接电源地,橙色接电源正极。

程序代码

三部分代码:

//回调函数
void __stdcall ImageCallBackEx(unsigned char * pData, MV_FRAME_OUT_INFO_EX* pFrameInfo, void* pUser)
{

    MVCamera* camera;
    camera = (MVCamera*)pUser;
    // 图像转换信息
    MV_CC_PIXEL_CONVERT_PARAM stConvertParam = {0};
    stConvertParam.nWidth = pFrameInfo->nWidth; // image width
    stConvertParam.nHeight = pFrameInfo->nHeight; // image height
    stConvertParam.pSrcData = pData; // input data buffer
    stConvertParam.nSrcDataLen = pFrameInfo->nFrameLen; // input data size
    stConvertParam.enSrcPixelType = pFrameInfo->enPixelType; // input pixel format
    stConvertParam.enDstPixelType = PixelType_Gvsp_BGR8_Packed; // output pixel format
    stConvertParam.pDstBuffer =  (unsigned char*)malloc(pFrameInfo->nWidth * pFrameInfo->nHeight * 3); // output data buffer
    stConvertParam.nDstBufferSize =  pFrameInfo->nWidth * pFrameInfo->nHeight * 3; // output buffer size
    MV_CC_ConvertPixelType(camera->hDevHandle, &stConvertParam);
    int nRet = camera->convertPix(stConvertParam);

    if (MV_OK != nRet)
    {
        qDebug() << "图像格式转化错误";
    }

    camera->nowImage = cv::Mat(cv::Size(4096, 3000), CV_8UC3, stConvertParam.pDstBuffer).clone();//修改1624*1240
    camera->isHaveImage = true;
//    std::cout << "call back" << std::endl;
}
//设置相机配置
int MVCamera::encoderCountGrbbingSetting()
{
    if (!isConn)
    {
        qDebug() << "相机未连接";
        return -1;
    }

    if (isStreaming)
    {
        qDebug() << "相机正在取流,无法设置参数,请先停止取流";
        return -1;
    }

    //设置触发源为开启状态
    int nRet = MV_CC_SetEnumValueByString(hDevHandle, "TriggerMode", "On");
    if (MV_OK != nRet)
    {
        qDebug() << "设置触发模式失败";
        return -1;
    }

    //设置触发源类型
    nRet = MV_CC_SetEnumValueByString(hDevHandle, "TriggerSource", "Counter0");
    if (MV_OK != nRet)
    {
        qDebug() << "设置触发源类型失败";
        return -1;
    }

    //设置计数器控制中的计数器选择
    nRet = MV_CC_SetEnumValueByString(hDevHandle, "CounterSelector", "Counter0");
    if (MV_OK != nRet)
    {
        qDebug() << "设置计数器选择类型失败";
        return -1;
    }
    //设置计数器控制中的计数器事件源
    nRet = MV_CC_SetEnumValueByString(hDevHandle, "CounterEventSource", "Line0");
    if (MV_OK != nRet)
    {
        qDebug() << "设置计数器事件源选择类型失败";
        return -1;
    }
    nRet = MV_CC_SetEnumValueByString(hDevHandle, "CounterResetSource", "Off");
    if (MV_OK != nRet)
    {
        qDebug() << "设置计数器复位原类型失败";
        return -1;
    }

    nRet = MV_CC_SetIntValue(hDevHandle,"CounterValue",500);
    if (MV_OK != nRet)
    {
        qDebug() << "设置计数器复位原类型失败";
        return -1;
    }

    // 设置回调函数
    nRet = MV_CC_RegisterImageCallBackEx(hDevHandle, ImageCallBackEx, this);
    if (MV_OK != nRet)
    {
        qDebug() << "设置回调函数失败";
        return -1;
    }

    unsigned int nImageNodeNum = 2;
    // 设置缓存图像个数
    nRet = MV_CC_SetImageNodeNum(hDevHandle, nImageNodeNum);
    if (MV_OK != nRet)
    {
        qDebug() << "设置相机缓存成功";
        return -1;
    }

    nRet = MV_CC_SetGrabStrategy(hDevHandle, MV_GrabStrategy_LatestImagesOnly);//仅从输出缓存列表中获取最新的一帧图像,同时清空输出缓存列表
    if (MV_OK != nRet)
    {
        qDebug() << "设置相机取图策略成功";
    }

    qDebug() << "主动取流设置成功";
    return 0;
}
//打开相机
int MVCamera::startStreaming()
{
    if (!isConn)
    {
        qDebug() << "相机未连接";
        return -1;
    }

    int nRet = nRet = MV_CC_StartGrabbing(hDevHandle);

    if (MV_OK != nRet) //判断是否开始采集图像
    {
        qDebug() << "相机开始采集图像失败";
        return -1;
    }

    qDebug() << "相机开始拉流";
    isStreaming = true;

    return 0;
}

导入文件到相机以及导出相机文件

#include <stdio.h>
#include <Windows.h>
#include <process.h>
#include <conio.h>
#include "MvCameraControl.h"

unsigned int g_nMode = 0;
int g_nRet = MV_OK;
// Wait for key press
void WaitForKeyPress(void)
{
    while(!_kbhit())
    {
        Sleep(10);
    }
    _getch();
}

bool PrintDeviceInfo(MV_CC_DEVICE_INFO* pstMVDevInfo)
{
    if (NULL == pstMVDevInfo)
    {
        printf("The Pointer of pstMVDevInfo is NULL!\n");
        return false;
    }
    if (pstMVDevInfo->nTLayerType == MV_GIGE_DEVICE)
    {
        int nIp1 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0xff000000) >> 24);
        int nIp2 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x00ff0000) >> 16);
        int nIp3 = ((pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x0000ff00) >> 8);
        int nIp4 = (pstMVDevInfo->SpecialInfo.stGigEInfo.nCurrentIp & 0x000000ff);

        // print current ip and user defined name
        printf("CurrentIp: %d.%d.%d.%d\n" , nIp1, nIp2, nIp3, nIp4);
        printf("UserDefinedName: %s\n\n" , pstMVDevInfo->SpecialInfo.stGigEInfo.chUserDefinedName);
    }
    else if (pstMVDevInfo->nTLayerType == MV_USB_DEVICE)
    {
        printf("UserDefinedName: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chUserDefinedName);
        printf("Serial Number: %s\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.chSerialNumber);
        printf("Device Number: %d\n\n", pstMVDevInfo->SpecialInfo.stUsb3VInfo.nDeviceNumber);
    }
    else
    {
        printf("Not support.\n");
    }

    return true;
}

static  unsigned int __stdcall ProgressThread(void* pUser)
{
    int nRet = MV_OK;
    MV_CC_FILE_ACCESS_PROGRESS stFileAccessProgress = {0};

    while(1)
    {
        // Get progress of file access
        nRet = MV_CC_GetFileAccessProgress(pUser, &stFileAccessProgress);
        printf("State = 0x%x,Completed = %I64d,Total = %I64d\r\n",nRet,stFileAccessProgress.nCompleted,stFileAccessProgress.nTotal);
        if (nRet != MV_OK || (stFileAccessProgress.nCompleted != 0 && stFileAccessProgress.nCompleted == stFileAccessProgress.nTotal))
        {
            break;
        }
        
        Sleep(50);
    }

    return 0;
}

static  unsigned int __stdcall FileAccessThread(void* pUser)
{
    MV_CC_FILE_ACCESS stFileAccess = {0};

    stFileAccess.pUserFileName = "UserSet1.bin";
    stFileAccess.pDevFileName = "UserSet1";
    if (1 == g_nMode)
    {
        // Read mode
        g_nRet = MV_CC_FileAccessRead(pUser, &stFileAccess);
        if (MV_OK != g_nRet)
        {
            printf("File Access Read fail! nRet [0x%x]\n", g_nRet);
        }
    }
    else if (2 == g_nMode)
    {
        // Write mode
        g_nRet = MV_CC_FileAccessWrite(pUser, &stFileAccess);
        if (MV_OK != g_nRet)
        {
            printf("File Access Write fail! nRet [0x%x]\n", g_nRet);
        }
    }

    return 0;
}

int main()
{
    int nRet = MV_OK;
    void* handle = NULL;

    do 
    {
        // Enum device
        MV_CC_DEVICE_INFO_LIST stDeviceList;
        memset(&stDeviceList, 0, sizeof(MV_CC_DEVICE_INFO_LIST));
        nRet = MV_CC_EnumDevices(MV_GIGE_DEVICE | MV_USB_DEVICE, &stDeviceList);
        if (MV_OK != nRet)
        {
            printf("Enum Devices fail! nRet [0x%x]\n", nRet);
            break;
        }

        if (stDeviceList.nDeviceNum > 0)
        {
            for (unsigned int i = 0; i < stDeviceList.nDeviceNum; i++)
            {
                printf("[device %d]:\n", i);
                MV_CC_DEVICE_INFO* pDeviceInfo = stDeviceList.pDeviceInfo[i];
                if (NULL == pDeviceInfo)
                {
                    break;
                } 
                PrintDeviceInfo(pDeviceInfo);            
            }  
        } 
        else
        {
            printf("Find No Devices!\n");
            break;
        }

        printf("Please Input camera index:");
        unsigned int nIndex = 0;
        scanf_s("%d", &nIndex);

        if (nIndex >= stDeviceList.nDeviceNum)
        {
            printf("Input error!\n");
            break;
        }

        // Select device and create handle
        nRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);
        if (MV_OK != nRet)
        {
            printf("Create Handle fail! nRet [0x%x]\n", nRet);
            break;
        }

        // Open device
        nRet = MV_CC_OpenDevice(handle);
        if (MV_OK != nRet)
        {
            printf("Open Device fail! nRet [0x%x]\n", nRet);
            break;
        }

        // Read mode
        g_nMode = 1;
        printf("Read to file.\n");

        unsigned int nThreadID = 0;
        void* hReadHandle = (void*) _beginthreadex( NULL , 0 , FileAccessThread , handle, 0 , &nThreadID );
        if (NULL == hReadHandle)
        {
            break;
        }

        Sleep(5);

        nThreadID = 0;
        void* hReadProgressHandle = (void*) _beginthreadex( NULL , 0 , ProgressThread , handle, 0 , &nThreadID );
        if (NULL == hReadProgressHandle)
        {
            break;
        }

        WaitForMultipleObjects(1, &hReadHandle, TRUE, INFINITE);
        WaitForMultipleObjects(1, &hReadProgressHandle, TRUE, INFINITE);
        if (MV_OK == g_nRet)
        {
            printf("File Access Read Success!\n");
        }
        printf("\n");

        // Write mode
        g_nMode = 2;
        printf("Write from file.\n");

        nThreadID = 0;
        void* hWriteHandle = (void*) _beginthreadex( NULL , 0 , FileAccessThread , handle, 0 , &nThreadID );
        if (NULL == hWriteHandle)
        {
            break;
        }

        Sleep(5);

        nThreadID = 0;
        void* hWriteProgressHandle = (void*) _beginthreadex( NULL , 0 , ProgressThread , handle, 0 , &nThreadID );
        if (NULL == hWriteProgressHandle)
        {
            break;
        }

        WaitForMultipleObjects(1, &hWriteHandle, TRUE, INFINITE);
        WaitForMultipleObjects(1, &hWriteProgressHandle, TRUE, INFINITE);
        if (MV_OK == g_nRet)
        {
            printf("File Access Write Success!\n");
        }

        // Close device
        nRet = MV_CC_CloseDevice(handle);
        if (MV_OK != nRet)
        {
            printf("ClosDevice fail! nRet [0x%x]\n", nRet);
            break;
        }

        // Destroy handle
        nRet = MV_CC_DestroyHandle(handle);
        if (MV_OK != nRet)
        {
            printf("Destroy Handle fail! nRet [0x%x]\n", nRet);
            break;
        }
    } while (0);

    if (nRet != MV_OK)
    {
        if (handle != NULL)
        {
            MV_CC_DestroyHandle(handle);
            handle = NULL;
        }
    }

    printf("Press a key to exit.\n");
    WaitForKeyPress();

    return 0;
}

以下是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(); } } } ``` 注意:以上代码仅供参考,实际使用时需要根据相机型号和需求进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值