Skip to content

Instantly share code, notes, and snippets.

@immengineer
Created February 26, 2019 04:22
Show Gist options
  • Save immengineer/b3ee82a4e416da6540315a2142ef6b95 to your computer and use it in GitHub Desktop.
Save immengineer/b3ee82a4e416da6540315a2142ef6b95 to your computer and use it in GitHub Desktop.

Revisions

  1. immengineer created this gist Feb 26, 2019.
    207 changes: 207 additions & 0 deletions Form1.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,207 @@
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Threading;

    using PvDotNet;
    using PvGUIDotNet;

    namespace Sample
    {
    public partial class Form1 : Form
    {
    private PvSystem mSys;

    private const UInt16 cBufferCount = 16;

    private PvDevice mDevice = null;
    private PvStream mStream = null;

    private Thread mThread = null;
    private bool mIsStopping = false;



    public Form1()
    {
    InitializeComponent();
    mSys = new PvSystem();
    }
    private void Form1_Load(object sender, EventArgs e)
    {
    label1.Text += mSys.InterfaceCount.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {

    using (PvDeviceFinderForm lForm = new PvDeviceFinderForm())
    {
    if ((lForm.ShowDialog() == DialogResult.OK) && (lForm.Selected != null))
    {
    try
    {
    // Get DeviceInfo, Show DisplayID
    PvDeviceInfo lDeviceInfo = lForm.Selected as PvDeviceInfo;
    label2.Text += lDeviceInfo.DisplayID;

    // Create and connect device.
    mDevice = PvDevice.CreateAndConnect(lDeviceInfo);

    // Create and Open stream.
    mStream = PvStream.CreateAndOpen(lDeviceInfo);

    // Perform GigE Vision only configuration
    PvDeviceGEV lDGEV = mDevice as PvDeviceGEV;
    if (lDGEV != null)
    {
    // Negotiate packet size
    lDGEV.NegotiatePacketSize();

    // Set stream destination.
    PvStreamGEV lSGEV = mStream as PvStreamGEV;
    lDGEV.SetStreamDestination(lSGEV.LocalIPAddress, lSGEV.LocalPort);
    }

    // Read payload size, preallocate buffers of the pipeline.
    Int64 lPayloadSize = mDevice.PayloadSize;

    // Get minimum buffer count, creates and allocates buffer.
    UInt32 lBufferCount = (mStream.QueuedBufferMaximum < cBufferCount) ? mStream.QueuedBufferMaximum : cBufferCount;
    PvBuffer[] lBuffers = new PvBuffer[lBufferCount];
    for (UInt32 i = 0; i < lBufferCount; i++)
    {
    lBuffers[i] = new PvBuffer();
    lBuffers[i].Alloc((UInt32)lPayloadSize);
    }
    // Queue all buffers in the stream.
    for (UInt32 i = 0; i < lBufferCount; i++)
    {
    mStream.QueueBuffer(lBuffers[i]);
    }
    }
    catch (PvException ex)
    {
    MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    // Enable Start Button
    button2.Enabled = true;
    }
    }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
    if (mStream != null)
    {
    // Close and release stream
    mStream.Close();
    mStream = null;
    }
    if (mDevice != null)
    {
    // Disconnect and release device
    mDevice.Disconnect();
    mDevice = null;
    }
    }

    private void button2_Click(object sender, EventArgs e)
    {
    // Start display thread.
    mThread = new Thread(new ParameterizedThreadStart(ThreadProc));
    Form1 lP1 = this;
    object[] lParameters = new object[] { lP1 };
    mIsStopping = false;
    mThread.Start(lParameters);

    // Enables streaming before sending the AcquisitionStart command.
    mDevice.StreamEnable();

    // Start acquisition on the device
    mDevice.Parameters.ExecuteCommand("AcquisitionStart");

    // Disable Start Button & Enable Stop Button
    button2.Enabled = false;
    button3.Enabled = true;
    }
    private void button3_Click(object sender, EventArgs e)
    {
    try
    {
    PvBuffer lBuffer = null;
    PvResult lOperationResult = new PvResult(PvResultCode.OK);
    PvResult lResult = new PvResult(PvResultCode.OK);

    // Stop acquisition.
    mDevice.Parameters.ExecuteCommand("AcquisitionStop");

    // Disable streaming after sending the AcquisitionStop command.
    mDevice.StreamDisable();

    // Stop display thread.
    mIsStopping = true;
    mThread.Join();
    mThread = null;

    // Abort all buffers in the stream and dequeue
    mStream.AbortQueuedBuffers();
    for (int i = 0; i < mStream.QueuedBufferCount; i++)
    {
    lResult = mStream.RetrieveBuffer(ref lBuffer, ref lOperationResult);
    if (lResult.IsOK)
    {
    lBuffer = null;
    }
    }
    }
    catch (PvException lExc)
    {
    MessageBox.Show(lExc.Message, Text);
    }
    // Enaable Start Button & Disable Stop Button
    button2.Enabled = true;
    button3.Enabled = false;
    }
    private static void ThreadProc(object aParameters)
    {
    object[] lParameters = (object[])aParameters;
    Form1 lThis = (Form1)lParameters[0];

    for (; ; )
    {
    if (lThis.mIsStopping)
    {
    // Signaled to terminate thread, return.
    return;
    }

    PvBuffer lBuffer = null;
    PvResult lOperationResult = new PvResult(PvResultCode.OK);

    // Retrieve next buffer from acquisition pipeline
    PvResult lResult = lThis.mStream.RetrieveBuffer(ref lBuffer, ref lOperationResult, 100);
    if (lResult.IsOK)
    {
    // Operation result of buffer is OK, display.
    if (lOperationResult.IsOK)
    {
    lThis.label3.Text = lBuffer.Timestamp.ToString();
    }

    // We have an image - do some processing (...) and VERY IMPORTANT,
    // re-queue the buffer in the stream object.
    lThis.mStream.QueueBuffer(lBuffer);
    }
    }
    }
    }
    }