249 lines
7.4 KiB
C
Raw Normal View History

2025-12-10 00:01:32 +08:00
// GrabContinuous.c
/* @brief: This example shows users how to use IKapBoard library to grab images continuously with CameraLink frame grabber. */
#include <stdio.h>
#include <stdlib.h>
#include "IKapBoard.h"
// Pre-defined.
#define HANDLE void*
#define CALLBACK
// The number of frames requested by buffer.
#define BOARD_FRMAE_COUNT 5
// Frame grabber device handle.
HANDLE g_hBoard = (void*)-1;
// File name of configuration file.
char configFilename[256] = "";
// Current frame index.
int g_nCurFrameIndex = 0;
/* @brief: Determine whether the IKapBoard function is called successfully.
* @param[in] errc: Function return value. */
#define CHECK_IKAPBOARD(errc) if (errc != 1) printIKapBoardErrorAndExit()
/* @brief: Print IKapBoard error message and exit the program. */
void printIKapBoardErrorAndExit();
/* @brief: Users enter Enter to exit the program. */
void pressEnterToExit(void);
/* @brief: Configure frame grabber device.
* @param[in] hCamera: Camera device handle.
* @param[in] hBoard: Frame grabber device handle. */
void ConfigureFrameGrabber();
/* @brief: Unregister callback functions. */
void UnRegisterCallback();
/* @brief: This function is registered as a callback function. When starting grabbing images, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnGrabStart(void *pContext);
/* @brief: This function is registered as a callback function. When a frame of image grabbing ready, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnFrameReady(void *pContext);
/* @brief: This function is registered as a callback function. When grabbing images time out, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnTimeout(void *pContext);
/* @brief: This function is registered as a callback function. When grabbing frame lost, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnFrameLost(void *pContext);
/* @brief: This function is registered as a callback function. When stopping grabbing images, the function will be called.
* @param[in] pContext: Input parameter. */
void CALLBACK OnGrabStop(void *pContext);
int main()
{
// Return value of IKapBoard functions.
int ret = IK_RTN_OK;
// Configure frame grabber device.
ConfigureFrameGrabber();
// Start grabbing images.
ret = IKapStartGrab(g_hBoard, 0);
CHECK_IKAPBOARD(ret);
// Wait for grabbing images finished.
getchar();
// Stop grabbing images.
ret = IKapStopGrab(g_hBoard);
CHECK_IKAPBOARD(ret);
// Unregister callback functions.
UnRegisterCallback();
// Close frame grabber device.
ret = IKapClose(g_hBoard);
CHECK_IKAPBOARD(ret);
pressEnterToExit();
return 0;
}
// Print IKapBoard error message and exit the program.
void printIKapBoardErrorAndExit()
{
// Get error code message.
IKAPERRORINFO errc;
IKapGetLastError(&errc, 1);
// Print error message.
fprintf(stderr, "IKapBoard:Index: %d, error code:%04x\n", errc.uBoardIndex, errc.uErrorCode);
pressEnterToExit();
exit(EXIT_FAILURE);
}
// Configure frame grabber device.
void ConfigureFrameGrabber()
{
int ret = 0;
unsigned int nPCIeDevCount = 0;
char resourceName[128];
unsigned int resourceNameSize = 128;
// Get the number of connected frame grabbers.
ret = IKapGetBoardCount(IKBoardPCIE, &nPCIeDevCount);
CHECK_IKAPBOARD(ret);
// When there is no connected frame grabber.
if (nPCIeDevCount == 0)
{
printf("Get board count 0");
pressEnterToExit();
}
// Get the name of frame grabber.
for (unsigned int i = 0; i < nPCIeDevCount; i++)
{
resourceNameSize = 128;
if (IKapGetBoardName(IKBoardPCIE, i, resourceName, &resourceNameSize) == 1)
{
printf("Resource name %s\n", resourceName);
}
}
// Open frame grabber.
g_hBoard = IKapOpen(IKBoardPCIE, 0);
if (g_hBoard == (void*)-1)
printIKapBoardErrorAndExit();
// Input load configuration address
printf("Please input load configuration address, for example: ./GrabContinuous.vlcf\n");
gets(configFilename);
// Load configuration file.
ret = IKapLoadConfigurationFromFile(g_hBoard, configFilename);
CHECK_IKAPBOARD(ret);
// Set frame count of buffer.
ret = IKapSetInfo(g_hBoard, IKP_FRAME_COUNT, BOARD_FRMAE_COUNT);
CHECK_IKAPBOARD(ret);
// Set time out time.
int timeout = -1;
ret = IKapSetInfo(g_hBoard, IKP_TIME_OUT, timeout);
CHECK_IKAPBOARD(ret);
// Set grab mode.
int grab_mode = IKP_GRAB_NON_BLOCK;
ret = IKapSetInfo(g_hBoard, IKP_GRAB_MODE, grab_mode);
CHECK_IKAPBOARD(ret);
// Set transfer mode.
int transfer_mode = IKP_FRAME_TRANSFER_SYNCHRONOUS_NEXT_EMPTY_WITH_PROTECT;
ret = IKapSetInfo(g_hBoard, IKP_FRAME_TRANSFER_MODE, transfer_mode);
CHECK_IKAPBOARD(ret);
// Register callback functions.
ret = IKapRegisterCallback(g_hBoard, IKEvent_GrabStart, OnGrabStart, g_hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(g_hBoard, IKEvent_FrameReady, OnFrameReady, g_hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(g_hBoard, IKEvent_FrameLost, OnFrameLost, g_hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(g_hBoard, IKEvent_TimeOut, OnTimeout, g_hBoard);
CHECK_IKAPBOARD(ret);
ret = IKapRegisterCallback(g_hBoard, IKEvent_GrabStop, OnGrabStop, g_hBoard);
CHECK_IKAPBOARD(ret);
}
// Unregister callback functions.
void UnRegisterCallback()
{
int ret = IK_RTN_OK;
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_GrabStart);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_FrameReady);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_FrameLost);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_TimeOut);
ret = IKapUnRegisterCallback(g_hBoard, IKEvent_GrabStop);
}
// Users enter Enter to exit the program.
void pressEnterToExit(void)
{
fprintf(stderr, "\nPress enter to exit.\n");
while (getchar() != '\n');
}
// This function is registered as a callback function. When starting grabbing images, the function will be called.
void CALLBACK OnGrabStart(void *pContext)
{
printf("Start grabbing image.\n");
}
// This function is registered as a callback function. When a frame of image grabbing ready, the function will be called.
void CALLBACK OnFrameReady(void *pContext)
{
HANDLE hDev = (HANDLE)pContext;
unsigned char* pUserBuffer = NULL;
int nFrameSize = 0;
int nFrameCount = 0;
int nBufferIndex = g_nCurFrameIndex % BOARD_FRMAE_COUNT;;
IKAPBUFFERSTATUS status;
IKapGetInfo(hDev, IKP_FRAME_COUNT, &nFrameCount);
IKapGetBufferStatus(hDev, nBufferIndex, &status);
// When the buffer is full.
if (status.uFull == 1)
{
// Get the size of a frame of image.
IKapGetInfo(hDev, IKP_FRAME_SIZE, &nFrameSize);
// Get the buffer address.
IKapGetBufferAddress(hDev, nBufferIndex, (void**)&pUserBuffer);
}
printf("Grab %d frame ready.\n", ++g_nCurFrameIndex);
}
// This function is registered as a callback function. When grabbing images time out, the function will be called.
void CALLBACK OnTimeout(void *pContext)
{
printf("Grab image timeout.\n");
}
// This function is registered as a callback function. When grabbing frame lost, the function will be called.
void CALLBACK OnFrameLost(void *pContext)
{
printf("Grab frame lost.\n");
}
// This function is registered as a callback function. When stopping grabbing images, the function will be called.
void CALLBACK OnGrabStop(void *pContext)
{
printf("Stop grabbing image.\n");
}