External device synchronization
This sample sets the INFINICAM GbE-2 to external sync and captures one image in step with a sync signal from an external device. It also checks what happens when no sync signal is coming in.
-
Enter and save the program
Save the following code as
external_sync.pyinC:\infinicam_projects\python.import cv2from pypuclib import (CameraFactory, PUCException,PUC_SYNC_MODE, PUC_SIGNAL, PUC_GENERAL_IN_SIGNAL_TYPE)print("Connecting to the camera...")try:# Connect to the GbE-2 at this IP addresscam = CameraFactory().create(ip="192.168.0.10")except PUCException as e:print("Error: could not connect to the camera.", e)raise SystemExit(1)try:# Use general-purpose input 1 (General In(1), number 0 in the SDK) as the sync signal inputcam.setGeneralInSignal(0, PUC_GENERAL_IN_SIGNAL_TYPE.SYNC_POS_IN)# Switch to external sync (capture on the rising edge of the incoming sync signal)cam.setSyncInMode(PUC_SYNC_MODE.PUC_SYNC_EXTERNAL, PUC_SIGNAL.PUC_SIGNAL_POSI)print("Set to external sync (positive polarity).")print("Waiting for the sync signal...")image = cam.decoder().decode(cam.grab())filename = "external_sync_image.png"cv2.imwrite(filename, image)print("Saved:", filename)except PUCException as e:print("Error: could not capture with external sync.", e)print("Check that the sync signal is connected, and check the wiring and signal voltage.")finally:# Switch back to internal sync (the camera runs on its own) before closingcam.setSyncInMode(PUC_SYNC_MODE.PUC_SYNC_INTERNAL, PUC_SIGNAL.PUC_SIGNAL_POSI)cam.close()print("Switched back to internal sync.") -
Run it
Run the following 2 lines, one at a time, in Command Prompt (or PowerShell).
Terminal window cd C:\infinicam_projects\pythonTerminal window python external_sync.pyIf a sync signal is coming in, you see the following and
external_sync_image.pngis created.Connecting to the camera...Set to external sync (positive polarity).Waiting for the sync signal...Saved: external_sync_image.pngSwitched back to internal sync.If no sync signal is coming in, it immediately prints the following and exits. The
37in the error is the error number for “external sync signal not detected”.Connecting to the camera...Set to external sync (positive polarity).Waiting for the sync signal...Error: could not capture with external sync. PUC_GetSingleXferData:37 external sync signal is not detected.Check that the sync signal is connected, and check the wiring and signal voltage.Switched back to internal sync.
How the program works
Section titled “How the program works”1. Use a general-purpose input as the sync signal input
Section titled “1. Use a general-purpose input as the sync signal input”cam.setGeneralInSignal(0, PUC_GENERAL_IN_SIGNAL_TYPE.SYNC_POS_IN)The GbE-2 lets you assign which signal each of the 2 general-purpose inputs on its I/O connector accepts. The first argument is the terminal number, which the SDK counts from 0 (0 = General In(1), 1 = General In(2)).
| Example second argument | Meaning |
|---|---|
SYNC_POS_IN / SYNC_NEG_IN |
Sync signal (positive / negative polarity) |
TRIG_POS_IN / TRIG_NEG_IN |
Trigger signal (positive / negative polarity) |
2. Choose how to synchronize
Section titled “2. Choose how to synchronize”cam.setSyncInMode(PUC_SYNC_MODE.PUC_SYNC_EXTERNAL, PUC_SIGNAL.PUC_SIGNAL_POSI)| First argument | Behavior |
|---|---|
PUC_SYNC_MODE.PUC_SYNC_INTERNAL |
Internal sync. Captures using the camera’s internal clock (the normal state) |
PUC_SYNC_MODE.PUC_SYNC_EXTERNAL |
External sync. Captures in step with the sync signal on a general-purpose input |
| Second argument | Behavior |
|---|---|
PUC_SIGNAL.PUC_SIGNAL_POSI |
Positive polarity. Follows the rising edge of the signal |
PUC_SIGNAL.PUC_SIGNAL_NEGA |
Negative polarity. Follows the falling edge of the signal |
