Search code examples
c#cognex

C# extract Cell Information from In-Sight Explorer (Cognex)


Does anyone have experience using C# and a Cognex camera? I wish to extract information from an in-sight explorer spreadsheet and display it on a C# program using: Cognex.InSight.Cell.CvsCellFloat but I don't know how. I am new to C# so any help will be greatly appreciated.


Solution

  • I am assuming you have already purchased and installed the In-Sight SDK. If not, please contact your local Cognex distributor.

    Here's instructions for a very simple demo application:

    1. Start Visual Studio (I'm using Visual Studio 2012).
    2. Create a new Visual C# Windows Forms Application.
    3. Choose "Add Reference..." from the Project menu.
    4. Add the framework assembly reference 'Cognex.InSight'. Also consider adding 'Cognex.InSight.Controls.Display' if you want to display camera images (not covered here).
    5. Click the OK button to close the Reference Manager.
    6. Add 'Load' and 'FormClosed' event handlers for your main Form.
    7. Edit the code for the form.
    8. Add 'using Cognex.InSight;' and 'using Cognex.InSight.Cell;' to the top of the form code.
    9. Add a CsvInSight object to the form.
    10. Add code to your form similar to that shown below.

    Note: The ResultsChanged event doesn't only occur on an inspection. I would suggest you add an inspection counter to your spreadsheet (using an Accumulate function triggered by event A0) and add code at the start of the ResultsChanged event to check if the inspection count has changed since the occurrence of the ResultsChanged event. If the counter hasn't changed, simply exit the ResultsChanged event handler by using a 'return;' statement.

    public partial class Form1 : Form
    {
        CvsInSight insight = new CvsInSight();
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            insight.Connect("127.0.0.1", "admin", "", false, false);
            insight.ResultsChanged += insight_ResultsChanged;
        }
    
        void insight_ResultsChanged(object sender, EventArgs e)
        {
            // Here: Consider inserting code to check that this event
            // has been triggered by an image acquisition
    
            CvsResultSet results = insight.Results;
            // Note the reference below to row 10, column 3 (cell D10)
            CvsCell cell = results.Cells.GetCell(10, 3);
            if (cell == null)
            {
                MessageBox.Show("Error: Cell D10 is null");
                return;
            }
            if (cell.DataType != CvsCellDataType.FloatingPoint) {
                MessageBox.Show("Error: Unexpected data type at cell D10");
                return;
            }
            CvsCellFloat floatCell = (CvsCellFloat)cell;
            MessageBox.Show("Floating point value at cell D10 is: " + floatCell.Value.ToString());
        }
    
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            insight.Disconnect();
        }