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

news2024/9/20 22:54:35

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

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

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

计数器触发

说明书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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2058848.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

[Leetcode 61][Medium]-旋转链表

目录 一、题目描述 二、整体思路 三、代码 一、题目描述 原题链接 二、整体思路 首先发现这样的规律&#xff1a;当k大于等于链表中节点总数n时&#xff0c;会发现此时旋转后的链表和kk%n时的旋转后的链表一样。同时对于特殊情况n0和n1时&#xff0c;无论k的值为多少都可以…

Unity--XLua调用C#

Unity–XLua调用C# 由于Unity/C# 和lua是两种语言&#xff0c;两种语言的特性不一样&#xff0c;因此&#xff0c;如果要互相调用的话&#xff0c;需要第三方作桥梁. 因此&#xff0c;为了在Unity中/C#中使用lua的特性&#xff0c;需要在Unity中安装插件&#xff0c;Xlua/toLu…

数据结构—— 再探二叉树

1. TOP-K问题 TOP-K问题&#xff1a;求数据结合中前K个最大或者最小的数据 比如&#xff1a;专业前10名、世界500强、富豪榜、游戏中前100的活跃玩家等 思路&#xff1a; 1. 用数据集合中前K个数据来建堆&#xff1a; …

easypoi模板导出word并且合并行

导出流程 引入依赖制作模板合并导出 引入依赖 <dependency><groupId>cn.afterturn</groupId><artifactId>easypoi-base</artifactId><version>4.1.2</version> </dependency>制作模板 合并行是备注那一列&#xff0c;这一列…

UCIE-state machion

1.retrain/linkerror/active状态 状态转变都是从下往上。 &#xff08;1&#xff09;Retrain&#xff1a;multistack和raw model相互违背&#xff1b; &#xff08;2&#xff09;Linkerror&#xff1a;如果一个stack需要进入linkerror&#xff0c;表示链路已经存在问题&…

论文解读:从Dijkstra的On-the-Fly到Go的三色标记算法,并行垃圾回收的起源

我们经常听到关于垃圾回收的说法是&#xff0c;某种垃圾回收算法是一种特定语言特有的&#xff0c;容易理解成&#xff0c;垃圾回收的算法跟特定编程语言是绑定的&#xff0c;但是仔细想想&#xff0c;垃圾回收器是一种分配和管理内存的机制或者程序&#xff0c;内存管理跟语言…

微分方程(Blanchard Differential Equations 4th)中文版Section2.3

阻尼谐振子 在本节中,我们将描述一种解析技术,它适用于本书中最重要的模型之一——阻尼谐振子。这一二阶微分方程用于建模各种现象,如质量-弹簧系统、电路理论中的RLC电路,以及人体的血糖调节系统。 例如,考虑汽车的悬挂系统。它可以平滑崎岖道路上的颠簸,并帮助保持轮…

告别U盘:看医院如何挑选高效安全的文件摆渡系统

基于法规要求和自身安全管理需要&#xff0c;医院普遍使用网闸&#xff0c;将网络隔离为院内网、院外网。网络隔离后&#xff0c;医院各科室部门仍存在频繁的网间数据交换需求&#xff0c;需要文件摆渡系统进行内外网数据的安全交换。具体交换场景如下&#xff1a; 1.影像科&am…

iOS Native与JS通信:JSBridge

文章目录 一、简介二、JS 调用 Native1.使用 URL Schemea.UIWebViewb.WKWebView 2.使用 JavaScriptCore (iOS 7)3.使用 WKWebView 和 WKScriptMessageHandler (iOS 8) 三、Native 调用 JS1.使用 UIWebView2.使用 WKWebView3.使用 JavaScriptCore (iOS 7) 一、简介 对于移动应用…

江西学术会议:第五届计算机、大数据与人工智能国际会议

第五届计算机、大数据与人工智能国际会议(ICCBDAI 2024)将于2024年11月1日-3日在江西景德镇召开。本届会议由景德镇陶瓷大学主办&#xff0c;西安交通大学、暨南大学、南京邮电大学、景德镇学院、ELSP&#xff08;爱迩思出版社&#xff09;、ESBK国际学术交流中心、AC学术平台协…

ctfshow之web29~web51

目录 web29 题解&#xff1a; web30 web31 web32&#xff08;32~36&#xff09; web33 web34 web35 web36 web37 web38 web39 web40 web41 web42 &#xff08;42~51&#xff09; web43 web44 web45 web50 web51 web29 前瞻知识&#xff1a; isset() …

【sgCreateReadonlyForm】自定义小工具:敏捷开发→自动化生成只读表单代码片段脚本(无需列表展示数据,多用于查看某一条数据记录)

sgCreateReadonlyForm源码 <template><!-- 前往https://blog.csdn.net/qq_37860634/article/details/141389231 查看使用说明 --><div :class"$options.name"><div class"sg-head">只读表单生成工具<el-dropdown:show-timeo…

8.20 roles的基本用法+使用剧本安装nginx

安装nginx并更改其端口 创建目录 mkdir /etc/ansible/playbook 编辑配置文件 vim /etc/ansible/palybook/nginx.yml --- - hosts: s remote_user: root tasks: - name: 卸载httpd yu…

人工智能 | 结对编程助手GithubCopilot

简介 GitHub Copilot 是一款 AI 结对程序员&#xff0c;可帮助您更快、更少地编写代码。它从注释和代码中提取上下文&#xff0c;以立即建议单独的行和整个函数。GitHub Copilot 由 GitHub、OpenAI 和 Microsoft 开发的生成式 AI 模型提供支持。它可作为 Visual Studio Code、…

智慧水务平台:数智化驱动,‌实现管理全面升级!‌

智慧生产体系聚焦水务行业的生产环节,涵盖水源管理、水厂管理、生产调度、二次供水管理等各个环节。对各生产环节的实时生产数据和设备运行参数进行监测,并提供报警、日常运维、能耗分析、流程优化,为水务生产管理的成本压降、效率提升、安全保障、服务优化提供支撑。 智慧管网…

Echarts添加水印

如果直接说水印,很难在官方找到一些痕迹,但是换个词【纹理】就能找到了。水印就是一种特殊的纹理背景。 Echarts-backgroundColor backgroundColor 支持使用rgb(255,255,255),rgba(255,255,255,1),#fff等方式设置为纯色,也支持设置为渐变色和纹理填充,具体见option.colo…

哪个牌子的开放式耳机性价比高?五款地表最强机型推荐!

在我们的日常生活中&#xff0c;街道、地铁车厢或公交车等地方常常充满了噪音&#xff0c;这些杂音不仅可能扰乱心情&#xff0c;还可能对我们的听力造成潜在的伤害。在这样的环境下&#xff0c;如果想要享受音乐或追剧&#xff0c;同时又能保持对周围环境的警觉&#xff0c;开…

充电宝哪个品牌好?360度全方面测评热门款充电宝

在这个智能手机、平板电脑等移动设备普及的时代&#xff0c;充电宝已成为我们日常生活中不可或缺的伴侣。无论是在通勤途中、旅行出行&#xff0c;还是在户外运动时&#xff0c;充电宝都能为我们的设备提供源源不断的电力支持。然而&#xff0c;市场上充电宝品牌众多&#xff0…

c++开发,下载安装Boost库并检测是否安装成功

c开发&#xff0c;下载安装Boost库并检测是否安装成功 系统说明下载Boost库安装测试验证 系统说明 win10系统 下载Boost库 从官方网站下载&#xff0c;点击版本号 进去后选择windows系统的下载 安装 第1步 将下载后的压缩包解压到你想存储的文件夹中&#xff0c;比如我这里…

自主身份:Web3如何重新定义个人数据所有权

随着数字时代的快速发展&#xff0c;个人数据成为了一种新型的资产&#xff0c;深刻影响着我们的生活。然而&#xff0c;在Web2时代&#xff0c;个人数据往往被科技巨头所掌控&#xff0c;用户在享受互联网服务时&#xff0c;无意中失去了对自己数据的控制权。Web3的到来&#…