private void chunkGrabImageMethod() { string triggerSelectorValue = "FrameStart"; /* Preselect the trigger for image acquisition */ /* Check the available camera trigger mode(s) to select the appropriate one: * acquisition start trigger mode (used by previous cameras; * do not confuse with acquisition start command) or * frame start trigger mode (equivalent to previous acquisition start trigger mode). */ bool isAvailAcquisitionStart = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "EnumEntry_TriggerSelector_AcquisitionStart"); bool isAvailFrameStart = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "EnumEntry_TriggerSelector_FrameStart"); /* Check to see if the camera implements the acquisition start trigger mode only. */ bool isAcqStartTriggerModeOnly = (isAvailAcquisitionStart && !isAvailFrameStart); if (isAcqStartTriggerModeOnly) { /* Camera uses the acquisition start trigger as the only trigger mode. */ Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerSelector", "AcquisitionStart"); Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerMode", "On"); triggerSelectorValue = "AcquisitionStart"; } else { /* Camera may have the acquisition start trigger mode and the frame start trigger mode implemented. * In this case, the acquisition trigger mode must be switched off. */ if (isAvailAcquisitionStart) { Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerSelector", "AcquisitionStart"); Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerMode", "Off"); } /* To trigger each single frame by software or external hardware trigger: Enable the frame start trigger mode. */ Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerSelector", "FrameStart"); Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerMode", "On"); } /* Note: the trigger selector must be set to the appropriate trigger mode * before setting the trigger source or issuing software triggers. * Frame start trigger mode for newer cameras, acquisition start trigger mode for previous cameras. */ Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerSelector", triggerSelectorValue); /* Enable software triggering. */ /* ... Select the software trigger as the trigger source. */ Pylon.DeviceFeatureFromString(_pylonDevHandle, "TriggerSource", "Software"); /* When using software triggering, the Continuous frame mode should be used. Once * acquisition is started, the camera sends one image each time a software trigger is * issued. */ Pylon.DeviceFeatureFromString(_pylonDevHandle, "AcquisitionMode", "Continuous"); //******************************************************************************************************** setChunkModeFeatures(); /* The data block containing the image chunk and the other chunks has a self-descriptive layout. * A chunk parser is used to extract the appended chunk data from the grabbed image frame. * Create a chunk parser. */ PYLON_CHUNKPARSER_HANDLE hChunkParser = Pylon.DeviceCreateChunkParser(_pylonDevHandle); if (!hChunkParser.IsValid) { /* The transport layer doesn't provide a chunk parser. */ throw new Exception("No chunk parser available."); } /* Image grabbing is done using a stream grabber. * A device may be able to provide different streams. A separate stream grabber must * be used for each stream. In this sample, we create a stream grabber for the default * stream, i.e., the first stream ( index == 0 ). */ /* Get the number of streams supported by the device and the transport layer. */ var nStreams = Pylon.DeviceGetNumStreamGrabberChannels(_pylonDevHandle); if (nStreams < 1) { throw new Exception("The transport layer doesn't support image streams."); } /* Create and open a stream grabber for the first channel. */ uint firstChannel = 0; PYLON_STREAMGRABBER_HANDLE hGrabber = Pylon.DeviceGetStreamGrabber(_pylonDevHandle, firstChannel); Pylon.StreamGrabberOpen(hGrabber); /* Get a handle for the stream grabber's wait object. The wait object * allows waiting for buffers to be filled with grabbed data. */ PYLON_WAITOBJECT_HANDLE hWait = Pylon.StreamGrabberGetWaitObject(hGrabber); /* Determine the required size of the grab buffer. Since activating chunks will increase the * payload size and thus the required buffer size, do this after enabling the chunks. */ uint payloadSize = checked ((uint)Pylon.DeviceGetIntegerFeature(_pylonDevHandle, "PayloadSize")); /* We must tell the stream grabber the number and size of the buffers * we are using. */ /* .. We will not use more than NUM_BUFFERS for grabbing. */ uint NUM_BUFFERS = 2; Pylon.StreamGrabberSetMaxNumBuffer(hGrabber, NUM_BUFFERS); /* .. We will not use buffers bigger than payloadSize bytes. */ Pylon.StreamGrabberSetMaxBufferSize(hGrabber, payloadSize); /* Allocate the resources required for grabbing. After this, critical parameters * that impact the payload size must not be changed until FinishGrab() is called. */ Pylon.StreamGrabberPrepareGrab(hGrabber); /* Before using the buffers for grabbing, they must be registered at * the stream grabber. For each registered buffer, a buffer handle * is returned. After registering, these handles are used instead of the * buffer objects pointers. The buffer objects are held in a dictionary, * that provides access to the buffer using a handle as key. */ var buffers = new Dictionary <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> >(); for (int i = 0; i < NUM_BUFFERS; ++i) { PylonBuffer <Byte> buffer = new PylonBuffer <byte>(payloadSize, true); PYLON_STREAMBUFFER_HANDLE handle = Pylon.StreamGrabberRegisterBuffer(hGrabber, ref buffer); buffers.Add(handle, buffer); } /* Feed the buffers into the stream grabber's input queue. For each buffer, the API * allows passing in an integer as additional context information. This integer * will be returned unchanged when the grab is finished. In our example, we use the index of the * buffer as context information. */ var index = 0; foreach (KeyValuePair <PYLON_STREAMBUFFER_HANDLE, PylonBuffer <Byte> > pair in buffers) { Pylon.StreamGrabberQueueBuffer(hGrabber, pair.Key, index++); } /* Issue an acquisition start command. Because the trigger mode is enabled, issuing the start command * itself will not trigger any image acquisitions. Issuing the start command simply prepares the camera. * Once the camera is prepared it will acquire one image for every trigger it receives. */ Pylon.DeviceExecuteCommandFeature(_pylonDevHandle, "AcquisitionStart"); /* Trigger the first image. */ Pylon.DeviceExecuteCommandFeature(_pylonDevHandle, "TriggerSoftware"); /* Grab NUM_GRABS images. */ PylonGrabResult_t grabResult; int NUM_GRABS = 1; int nGrabs = 0; /* Counts the number of images grabbed. */ while (nGrabs < NUM_GRABS) { int bufferIndex; /* Index of the buffer. */ /* Wait for the next buffer to be filled. Wait up to 1000 ms. */ bool isReady = Pylon.WaitObjectWait(hWait, 1000); if (!isReady) { /* Timeout occurred. */ throw new Exception("Grab timeout occurred.\n"); } /* Since the wait operation was successful, the result of at least one grab * operation is available. Retrieve it. */ isReady = Pylon.StreamGrabberRetrieveResult(hGrabber, out grabResult); if (!isReady) { /* Oops. No grab result available? We should never have reached this point. * Since the wait operation above returned without a timeout, a grab result * should be available. */ throw new Exception("Failed to retrieve a grab result.\n"); } nGrabs++; /* Trigger the next image. Since we passed more than one buffer to the stream grabber, * the triggered image will be grabbed while the image processing is performed. */ Pylon.DeviceExecuteCommandFeature(_pylonDevHandle, "TriggerSoftware"); /* Get the buffer index from the context information. */ bufferIndex = (int)grabResult.Context; /* Check to see if the image was grabbed successfully. */ if (grabResult.Status == EPylonGrabStatus.Grabbed) { /* The grab is successful. */ PylonBuffer <Byte> buffer; /* Reference to the buffer attached to the grab result. */ /* Get the buffer from the dictionary. Since we also got the buffer index, * we could alternatively use an array, e.g. buffers[bufferIndex]. */ if (!buffers.TryGetValue(grabResult.hBuffer, out buffer)) { /* Oops. No buffer available? We should never have reached this point. Since all buffers are * in the dictionary. */ throw new Exception("Failed to find the buffer associated with the handle returned in grab result."); } //Console.WriteLine("Grabbed frame {0} into buffer {1}.", nGrabs, bufferIndex); /* Check to see if we really got image data plus chunk data. */ if (grabResult.PayloadType != EPylonPayloadType.PayloadType_ChunkData) { Console.WriteLine("Received a buffer not containing chunk data?"); } else { /* Process the chunk data. This is done by passing the grabbed image buffer * to the chunk parser. When the chunk parser has processed the buffer, the chunk * data can be accessed in the same manner as "normal" camera parameters. * The only exception is the CRC checksum feature. There are dedicated functions for * checking the CRC checksum. */ bool hasCRC; /* Let the parser extract the data. */ Pylon.ChunkParserAttachBuffer(hChunkParser, buffer); /* Check the CRC checksum. */ hasCRC = Pylon.ChunkParserHasCRC(hChunkParser); bool isOk = (hasCRC) ? Pylon.ChunkParserCheckCRC(hChunkParser) : true; bool isAvail = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "ChunkWidth"); long chunkWidth = 0, chunkHeight = 0; //Console.WriteLine("Frame {0} {1} a frame width chunk.", nGrabs, isAvail ? "contains" : "doesn't contain"); if (isAvail) { /* ... Get the value. */ chunkWidth = Pylon.DeviceGetIntegerFeature(_pylonDevHandle, "ChunkWidth"); //Console.WriteLine("Width of frame {0}: {1}.", nGrabs, chunkWidth); } /* Retrieve the chunk height value. */ /* ... Check the availability. */ isAvail = Pylon.DeviceFeatureIsAvailable(_pylonDevHandle, "ChunkHeight"); Console.WriteLine("Frame {0} {1} a frame height chunk.", nGrabs, isAvail ? "contains" : "doesn't contain"); if (isAvail) { /* ... Get the value. */ chunkHeight = Pylon.DeviceGetIntegerFeature(_pylonDevHandle, "ChunkHeight"); //Console.WriteLine("Height of frame {0}: {1}.", nGrabs, chunkHeight); } if (isOk) { _latestImage = new HImage("byte", (int)chunkWidth, (int)chunkHeight, buffer.Pointer); } } /* Before requeueing the buffer, you should detach it from the chunk parser. */ Pylon.ChunkParserDetachBuffer(hChunkParser); /* Now the chunk data in the buffer is no longer accessible. */ } else if (grabResult.Status == EPylonGrabStatus.Failed) { Console.Error.WriteLine("Frame {0} wasn't grabbed successfully. Error code = {1}", nGrabs, grabResult.ErrorCode); } /* Once finished with the processing, requeue the buffer to be filled again. */ Pylon.StreamGrabberQueueBuffer(hGrabber, grabResult.hBuffer, bufferIndex); } }
/// <summary> /// 設定 ChunkMode /// </summary> private void setChunkFeature() { /* Before enabling individual chunks, the chunk mode in general must be activated. */ isAvail = Pylon.DeviceFeatureIsWritable(_hDev, "ChunkModeActive"); if (!isAvail) { /* Feature is not available. */ notifyError("The device doesn't support the chunk mode."); } else { /* Activate the chunk mode. */ Pylon.DeviceSetBooleanFeature(_hDev, "ChunkModeActive", true); } /* Enable some individual chunks... */ /* ... The frame counter chunk feature. */ /* Is the chunk feature available? */ isAvail = Pylon.DeviceFeatureIsAvailable(_hDev, "EnumEntry_ChunkSelector_Framecounter"); if (isAvail) { /* Select the frame counter chunk feature. */ /*Remark*/ Pylon.DeviceFeatureFromString(_hDev, "ChunkSelector", "Framecounter"); /* Can the chunk feature be activated? */ isAvail = Pylon.DeviceFeatureIsWritable(_hDev, "ChunkEnable"); if (isAvail) { /* Activate the chunk feature. */ Pylon.DeviceSetBooleanFeature(_hDev, "ChunkEnable", true); } } /* ... The CRC checksum chunk feature. */ /* Note: Enabling the CRC checksum chunk feature is not a prerequisite for using chunks. Chunks can also be handled when the CRC checksum chunk feature is disabled. */ isAvail = Pylon.DeviceFeatureIsAvailable(_hDev, "EnumEntry_ChunkSelector_PayloadCRC16"); if (isAvail) { /* Select the CRC checksum chunk feature. */ Pylon.DeviceFeatureFromString(_hDev, "ChunkSelector", "PayloadCRC16"); /* Can the chunk feature be activated? */ isAvail = Pylon.DeviceFeatureIsWritable(_hDev, "ChunkEnable"); if (isAvail) { /* Activate the chunk feature. */ Pylon.DeviceSetBooleanFeature(_hDev, "ChunkEnable", true); } } /* The data block containing the image chunk and the other chunks has a self-descriptive layout. A chunk parser is used to extract the appended chunk data from the grabbed image frame. Create a chunk parser. */ _hChunkParser = Pylon.DeviceCreateChunkParser(_hDev); if (!_hChunkParser.IsValid) { notifyError("No chunk parser available."); /* The transport layer doesn't provide a chunk parser. */ } }