当前位置: 首页 > news >正文

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

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

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

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

计数器触发

说明书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 widthstConvertParam.nHeight = pFrameInfo->nHeight; // image heightstConvertParam.pSrcData = pData; // input data bufferstConvertParam.nSrcDataLen = pFrameInfo->nFrameLen; // input data sizestConvertParam.enSrcPixelType = pFrameInfo->enPixelType; // input pixel formatstConvertParam.enDstPixelType = PixelType_Gvsp_BGR8_Packed; // output pixel formatstConvertParam.pDstBuffer =  (unsigned char*)malloc(pFrameInfo->nWidth * pFrameInfo->nHeight * 3); // output data bufferstConvertParam.nDstBufferSize =  pFrameInfo->nWidth * pFrameInfo->nHeight * 3; // output buffer sizeMV_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*1240camera->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 nameprintf("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 accessnRet = 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 modeg_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 modeg_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 deviceMV_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 handlenRet = MV_CC_CreateHandle(&handle, stDeviceList.pDeviceInfo[nIndex]);if (MV_OK != nRet){printf("Create Handle fail! nRet [0x%x]\n", nRet);break;}// Open devicenRet = MV_CC_OpenDevice(handle);if (MV_OK != nRet){printf("Open Device fail! nRet [0x%x]\n", nRet);break;}// Read modeg_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 modeg_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 devicenRet = MV_CC_CloseDevice(handle);if (MV_OK != nRet){printf("ClosDevice fail! nRet [0x%x]\n", nRet);break;}// Destroy handlenRet = 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;
}

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【Python】高效的Web自动化测试利器—Python+Playwright快速上手自动化实战指南(限时开放)
  • 经典游戏,用java实现的坦克大战小游戏
  • c语言编程有什么难点
  • js拖拽交换元素位置
  • Android笔试面试题AI答之Kotlin(11)
  • vue 实现批量引入组件
  • 设计模式六大原则(六)--迪米特法则
  • 音频提取软件分享:6款好用音频提取工具推荐
  • 中仕公考怎么样?考教师编有什么具体要求?
  • chrome浏览器插件开发--如何在content_scripts的js中使用插件的本地图片
  • jenkins最佳实践(二):Pipeline流水线部署springCloud微服务项目
  • uniapp在线视频监控开发
  • 精通推荐算法27:行为序列建模之BST— 代码实现
  • 开学季好物分享,精选五款开学必备的数码好物推荐!
  • 第二十六届中国机器人及人工智能大赛(智能驾驶)思路
  • 08.Android之View事件问题
  • 11111111
  • axios 和 cookie 的那些事
  • JavaScript 基础知识 - 入门篇(一)
  • java中的hashCode
  • Lsb图片隐写
  • Mac转Windows的拯救指南
  • React系列之 Redux 架构模式
  • 多线程 start 和 run 方法到底有什么区别?
  • 前端知识点整理(待续)
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 使用agvtool更改app version/build
  • 小程序button引导用户授权
  • 小程序开发中的那些坑
  •  一套莫尔斯电报听写、翻译系统
  • 原创:新手布局福音!微信小程序使用flex的一些基础样式属性(一)
  • ​queue --- 一个同步的队列类​
  • # windows 运行框输入mrt提示错误:Windows 找不到文件‘mrt‘。请确定文件名是否正确后,再试一次
  • #NOIP 2014#Day.2 T3 解方程
  • ( )的作用是将计算机中的信息传送给用户,计算机应用基础 吉大15春学期《计算机应用基础》在线作业二及答案...
  • (06)金属布线——为半导体注入生命的连接
  • (1)常见O(n^2)排序算法解析
  • (安卓)跳转应用市场APP详情页的方式
  • (代码示例)使用setTimeout来延迟加载JS脚本文件
  • (附源码)springboot青少年公共卫生教育平台 毕业设计 643214
  • (九)信息融合方式简介
  • (三)elasticsearch 源码之启动流程分析
  • (四)鸿鹄云架构一服务注册中心
  • (一)Dubbo快速入门、介绍、使用
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转) ns2/nam与nam实现相关的文件
  • (转载)hibernate缓存
  • .bat批处理(八):各种形式的变量%0、%i、%%i、var、%var%、!var!的含义和区别
  • .NET Framework 4.6.2改进了WPF和安全性
  • .NET MVC、 WebAPI、 WebService【ws】、NVVM、WCF、Remoting
  • .NET/C# 如何获取当前进程的 CPU 和内存占用?如何获取全局 CPU 和内存占用?
  • .NET编程——利用C#调用海康机器人工业相机SDK实现回调取图与软触发取图【含免费源码】
  • .NET中统一的存储过程调用方法(收藏)
  • @angular/cli项目构建--Dynamic.Form
  • @JsonFormat与@DateTimeFormat注解的使用