External Device Synchronization + SingleImage (GbE-2 / C++)
These are the steps to configure external synchronization mode, in which the GbE-2 captures in sync with an external pulse signal (SYNC IN), and to create a program that captures one frame of image data and saves it in the PGM format.
-
Write the source code
Open the main file (e.g.
ExternalSync.cpp) and replace its contents with the following code.#include <stdio.h>#include <stdlib.h>#include <PUCLIB.h> // Load the PUCLIB headerint main() {printf("Starting INFINICAM GbE-2 external device synchronization setup & capture...\n");// 1. Initialize the SDKPUCRESULT ret = PUC_Initialize();if (ret != PUC_SUCCEEDED) {printf("Error: Failed to initialize the SDK. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 2. Search for a GbE-2 device on the networkPUC_DETECT_FILTER filter = { 0 };filter.nDeviceTypeFlags = PUC_DETECT_TARGET_GBE2; // Specify GbE-2PUC_DETECT_INFO detectInfo = { 0 };ret = PUC_DetectDevice2(&filter, &detectInfo);if (ret != PUC_SUCCEEDED || detectInfo.nDeviceCount == 0) {printf("Error: No GbE-2 camera was found. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 3. Open the first detected devicePUC_HANDLE hDevice = NULL;ret = PUC_OpenDevice2(0, &hDevice);if (ret != PUC_SUCCEEDED) {printf("Error: Failed to open the device. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 4. Configure the desired frame rate and shutter speed in advance// Note: Capture parameters must be set before configuring external synchronizationPUC_SetFramerateShutter(hDevice, 1000, 1000); // 1000 fps, shutter 1/1000 sec// 5. Switch to external device synchronization mode (positive polarity pulse)// 2nd argument: PUC_SYNC_EXTERNAL (external sync), 3rd argument: PUC_SIGNAL_POSI (positive polarity)ret = PUC_SetSyncInMode(hDevice, PUC_SYNC_EXTERNAL, PUC_SIGNAL_POSI);if (ret != PUC_SUCCEEDED) {printf("Error: Failed to set external synchronization mode. ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}printf("Set to external device synchronization (positive polarity).\n");// 6. Get the resolution, quantization table, and transfer data sizeUINT32 width = 0, height = 0;PUC_GetResolution(hDevice, &width, &height);unsigned short qVals[PUC_Q_COUNT] = { 0 };PUC_GetQuantization2(hDevice, qVals);UINT32 xferDataSize = 0;PUC_GetXferDataSize(hDevice, &xferDataSize);// 7. Allocate memory buffersunsigned char* pCompressedBuffer = (unsigned char*)malloc(xferDataSize);UINT32 lineBytes = (width + 3) & ~3;unsigned long decodedBufferSize = (unsigned long)lineBytes * height;unsigned char* pDecodedBuffer = (unsigned char*)malloc(decodedBufferSize);if (pCompressedBuffer == NULL || pDecodedBuffer == NULL) {printf("Error: Memory allocation failed.\n");free(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// 8. Capture one frame of data synchronized to the external sync signalPUC_XFER_DATA_INFO xferDataInfo = { 0 };xferDataInfo.pData = pCompressedBuffer;xferDataInfo.nDataSize = xferDataSize;printf("Waiting for the external sync pulse (capturing)...\n");ret = PUC_GetSingleXferData(hDevice, &xferDataInfo);if (ret == PUC_SUCCEEDED) {// 9. Decode the compressed dataret = PUC_DecodeData(pDecodedBuffer, 0, 0, width, height, lineBytes, pCompressedBuffer, qVals);if (ret == PUC_SUCCEEDED) {// 10. Save the decoded image in PGM (P5) formatconst char* filename = "ext_sync_image.pgm";FILE* fp = NULL;if (fopen_s(&fp, filename, "wb") == 0 && fp != NULL) {fprintf(fp, "P5\n%u %u\n255\n", width, height);for (UINT32 y = 0; y < height; y++) {fwrite(pDecodedBuffer + (y * lineBytes), sizeof(unsigned char), width, fp);}fclose(fp);printf("SUCCESS: Saved the externally synchronized capture image (%s)\n", filename);} else {printf("Error: Failed to save the file.\n");}} else {printf("Error: Failed to decode the image data. ErrorCode: %d\n", ret);}} else {// This results in an error if no external signal is applied or if a timeout occursprintf("Error: Failed to capture the image data (no external signal applied, or timeout). ErrorCode: %d\n", ret);}// 11. Cleanup (restore internal synchronization mode before exiting)PUC_SetSyncInMode(hDevice, PUC_SYNC_INTERNAL, PUC_SIGNAL_POSI);free(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_SUCCESS;} -
Build and run
-
Connect an external signal source (such as a function generator) to the SYNC IN terminal of the GbE-2 and output pulses.
-
Press
Ctrl+F5. When the build finishes, the program runs automatically. If you see the following, it worked.Starting INFINICAM GbE-2 external device synchronization setup & capture...Set to external device synchronization (positive polarity).Waiting for the external sync pulse (capturing)...SUCCESS: Saved the externally synchronized capture image (ext_sync_image.pgm)
The image file
ext_sync_image.pgmis saved in the project folder (for exampleC:\infinicam_projects\ExternalSync\). Open it the same way as in SingleImage.If no external signal is applied, you see the following.
ErrorCode: 37means “no external sync signal detected” (PUC_ERROR_NO_SYNC). The program switches back to internal sync before it ends, so you can keep using the camera as it is.Error: Failed to capture the image data (no external signal applied, or timeout). ErrorCode: 37 -
Key Points for Configuring External Device Synchronization
Section titled “Key Points for Configuring External Device Synchronization”1. Synchronization mode configuration function (PUC_SetSyncInMode)
Section titled “1. Synchronization mode configuration function (PUC_SetSyncInMode)”PUC_SetSyncInMode(hDevice, nMode, nSignal);- nMode: Specifies the synchronization input mode.
PUC_SYNC_INTERNAL: Internal synchronization (free-running capture using the camera’s internal clock)PUC_SYNC_EXTERNAL: External signal synchronization (synchronized to pulses input to the SYNC IN port)PUC_SYNC_EXTERNAL_PTP: Synchronization to a PTP (Precision Time Protocol) master clock
- nSignal: Specifies the rising/falling edge (polarity).
PUC_SIGNAL_POSI: Positive polarity (synchronized to the rising edge)PUC_SIGNAL_NEGA: Negative polarity (synchronized to the falling edge)
2. About the order of configuration
Section titled “2. About the order of configuration”When external device synchronization is configured, the camera automatically adjusts the exposure time to account for variation in the input pulse. For this reason, always change to the desired frame rate and shutter speed using PUC_SetFramerateShutter (or PUC_SetExposeTime) before calling PUC_SetSyncInMode.
