It can also be helpful to familiarize yourself with the NodeMapCallback_C example, as nodemap callbacks follow the same general procedure as events, but with a few less steps.
Events generally require a class to be defined as an event handler; however, because C is not an object-oriented language, an event context is created using a function and a struct whereby the function acts as the event method and the struct acts as its properties.
#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
#include "windows.h"
#else
#include "unistd.h"
#endif
#include "stdio.h"
#include <stdlib.h>
#include "string.h"
{
#if defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64
Sleep(milliseconds);
#else
usleep(1000 * milliseconds);
#endif
}
#define MAX_BUFF_LEN 256
{
}
{
{
printf(
"Unable to retrieve node readability (%s node) with error: %s [%d]\n\n",
nodeName,
err);
}
return pbReadable;
}
{
{
printf(
"Unable to retrieve node writability (%s node) with error: %s [%d]\n\n",
nodeName,
err);
}
return pbWritable;
}
{
printf("Unable to get %s (%s %s retrieval failed).\n\n", node, name, node);
}
{
} userData;
{
userData* imageEventInfo = (userData*)pUserData;
if (imageEventInfo->imageCnt < imageEventInfo->numImages)
{
printf("Image event occurred...\n");
{
printf(
"Unable to determine image completion. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return;
}
if (isIncomplete)
{
{
printf(
"Unable to retrieve image status. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
}
else
{
printf("Image incomplete with image status %d...\n", imageStatus);
}
return;
}
size_t width = 0;
size_t height = 0;
{
printf(
"Unable to retrieve image width. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
}
{
printf(
"Unable to retrieve image height. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
}
printf(
"Grabbed image %u, width = %u, height = %u\n",
imageEventInfo->imageCnt,
(unsigned int)width,
(unsigned int)height);
{
}
{
printf(
"Unable to create image processor. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
}
{
printf(
"Unable to set image processor color processing method. Non-fatal error: %s [%d]\n\n",
err);
}
{
return;
}
{
printf(
"Unable to destroy image processor. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
}
if (imageEventInfo->lenDeviceSerialNumber == 0)
{
sprintf(filename, "ImageEvents-C-%d.jpg", imageEventInfo->imageCnt);
}
else
{
sprintf(
filename,
"ImageEvents-C-%d-%d.jpg",
atoi(imageEventInfo->deviceSerialNumber),
imageEventInfo->imageCnt);
}
{
}
else
{
printf("Image saved at %s\n\n", filename);
}
{
}
imageEventInfo->imageCnt++;
}
}
userData* imageEventInfo)
{
printf("\n\n*** IMAGE EVENTS CONFIGURATION ***\n\n");
imageEventInfo->imageCnt = 0;
imageEventInfo->numImages = 10;
{
strcpy(imageEventInfo->deviceSerialNumber, "");
imageEventInfo->lenDeviceSerialNumber = 0;
}
else
{
if (
IsReadable(hDeviceSerialNumber,
"DeviceSerialNumber"))
{
hDeviceSerialNumber, imageEventInfo->deviceSerialNumber, &imageEventInfo->lenDeviceSerialNumber);
{
strcpy(imageEventInfo->deviceSerialNumber, "");
imageEventInfo->lenDeviceSerialNumber = 0;
}
printf("Device serial number retrieved as %s...\n", imageEventInfo->deviceSerialNumber);
}
else
{
strcpy(imageEventInfo->deviceSerialNumber, "");
imageEventInfo->lenDeviceSerialNumber = 0;
}
}
{
printf(
"Unable to create event. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("Image event created...\n");
{
printf(
"Unable to register event. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("Image event registered...\n\n");
return err;
}
{
const int sleepDuration = 200;
while (imageEventInfo->imageCnt < imageEventInfo->numImages)
{
printf("\t//\n");
printf("\t// Sleeping for %d ms. Grabbing images...\n", sleepDuration);
printf("\t//\n");
}
}
{
{
printf(
"Unable to unregister event. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("Image event unregistered...\n");
{
printf(
"Unable to destroy event. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("Image event destroyed...\n\n");
return err;
}
{
unsigned int i = 0;
printf("\n*** DEVICE INFORMATION ***\n\n");
{
return err;
}
size_t numFeatures = 0;
if (
IsReadable(hDeviceInformation,
"DeviceInformation"))
{
{
printf(
"Unable to retrieve number of nodes. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
}
else
{
}
for (i = 0; i < numFeatures; i++)
{
{
continue;
}
{
strcpy(featureName, "Unknown name");
}
{
{
printf(
"Unable to retrieve node type. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
continue;
}
}
else
{
printf("%s: Node not readable\n", featureName);
continue;
}
{
strcpy(featureValue, "Unknown value");
}
printf("%s: %s\n", featureName, featureValue);
}
printf("\n");
return err;
}
{
printf("\n*** IMAGE ACQUISITION ***\n\n");
int64_t acquisitionModeContinuous = 0;
{
printf(
"Unable to set acquisition mode to continuous (node retrieval). Aborting with error: %s [%d]\n\n",
err);
return err;
}
if (
IsReadable(hAcquisitionMode,
"AcquisitionMode"))
{
{
printf(
"Unable to set acquisition mode to continuous (entry 'continuous' retrieval). Aborting with error "
"%d...\n\n",
err);
return err;
}
}
else
{
}
if (
IsReadable(hAcquisitionModeContinuous,
"AcquisitionModeContinuous"))
{
{
printf(
"Unable to set acquisition mode to continuous (entry int value retrieval). Aborting with error "
"%d...\n\n",
err);
return err;
}
}
else
{
}
if (
IsWritable(hAcquisitionMode,
"AcquisitionMode"))
{
{
printf(
"Unable to set acquisition mode to continuous (entry int value setting). Aborting with error %d...\n\n",
err);
return err;
}
printf("Acquisition mode set to continuous...\n");
}
else
{
}
{
printf(
"Unable to begin image acquisition. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("Acquiring images...\n");
{
}
return err;
}
{
{
printf(
"Unable to retrieve TL device nodemap. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
}
else
{
}
{
printf(
"Unable to initialize camera. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
{
printf(
"Unable to retrieve GenICam nodemap. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
userData imageEventInfo;
{
return err;
}
{
return err;
}
{
return err;
}
{
printf(
"Unable to deinitialize camera. Non-fatal error: %s [%d]\n\n",
GetLastErrorMessage(), err);
}
return err;
}
{
unsigned int i = 0;
FILE* tempFile;
tempFile = fopen("test.txt", "w+");
if (tempFile == NULL)
{
printf("Failed to create file in current folder. Please check "
"permissions.\n");
printf("Press Enter to exit...\n");
getchar();
return -1;
}
fclose(tempFile);
remove("test.txt");
printf("Application build date: %s %s \n\n", __DATE__, __TIME__);
{
printf(
"Unable to retrieve system instance. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
spinLibraryVersion hLibraryVersion;
printf(
"Spinnaker library version: %d.%d.%d.%d\n\n",
hLibraryVersion.major,
hLibraryVersion.minor,
hLibraryVersion.type,
hLibraryVersion.build);
{
printf(
"Unable to create camera list. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
{
printf(
"Unable to retrieve camera list. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
size_t numCameras = 0;
{
printf(
"Unable to retrieve number of cameras. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("Number of cameras detected: %u\n\n", (unsigned int)numCameras);
if (numCameras == 0)
{
{
printf(
"Unable to clear camera list. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
{
printf(
"Unable to destroy camera list. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
{
printf(
"Unable to release system instance. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("Not enough cameras!\n");
printf("Done! Press Enter to exit...\n");
getchar();
return -1;
}
for (i = 0; i < numCameras; i++)
{
printf("\nRunning example for camera %d...\n", i);
{
printf(
"Unable to retrieve camera from list. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
errReturn = err;
}
else
{
{
errReturn = err;
}
}
{
errReturn = err;
}
printf("Camera %d example complete...\n\n", i);
}
{
printf(
"Unable to clear camera list. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
{
printf(
"Unable to destroy camera list. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
{
printf(
"Unable to release system instance. Aborting with error: %s [%d]\n\n",
GetLastErrorMessage(), err);
return err;
}
printf("\nDone! Press Enter to exit...\n");
getchar();
return errReturn;
}