C# write cell to In-Sight Explorer (cognex)

3.1k Views Asked by At

I found this page where there is an example for extract cell information from an Insight-Explorer but... what about to write into the cell from a c# application?

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

3

There are 3 best solutions below

0
On

If you want to control the cells in the current job file of an In-Sight camera using C#, here is the method that has worked for me. Please note, I'm using In-Sight Explorer v5.9.0, but I've tested this on earlier versions too and it still works.

NOTE: Without a Cognex In-Sight SDK license, you will not be able to run this application within Visual Studio. You will have to build the project and then run the executable directly.

  1. Open Visual Studio
  2. Create a new Console App (.NET Framework) project
  3. I'm using .NET Framework 4.7.2
  4. Right-click on the project in the solution explorer and add a reference to the Cognex.InSight.dll file (It is typically located here: C:\Program Files (x86)\Common Files\Cognex\In-Sight\5.x.x.x\Cognex.InSight.dll)
  5. Set the target platform to x86
  6. Paste the code, below, into your project
  7. Change the username, password and ipAddress variables to match what's setup for your camera
  8. Build
  9. Go to the Debug folder and find the executable that was created after building the project
  10. Double-click the executable to run it
using System;
using Cognex.InSight;

namespace ChangeInSightCellValue2
{
    class Program
    {
        static void Main(string[] args)
        {
            string username = "admin";
            string password = "";
            string ipAddress = "127.0.0.1";

            // Create camera object and connect to it
            CvsInSight camera = LogIntoCamera(ipAddress, username, password, true, false);

            // Define which cell you want to modify
            CvsCellLocation cell = new CvsCellLocation(2, 'C');

            // Modify the cell expression
            camera.SetExpression(cell, "Filter($A$0,0,0,0,80,100,320,440,0,0,3,3,1,128,128,128,1,1,0)", true);
        }

        // Log into camera
        private static CvsInSight LogIntoCamera(string sCamIP, string sCamUsername, string sCamPassword, bool forceConnect, bool connectAsynchronous)
        {
            // Create camera object
            CvsInSight insight = new CvsInSight();
            Console.WriteLine("Object created");

            IAsyncResult result;
            // Try logging into the camera on a different thread to prevent locking this one up
            Action action = () =>
            {
                // Connect to camera
                insight.Connect(sCamIP, sCamUsername, sCamPassword, forceConnect, connectAsynchronous);
            };

            result = action.BeginInvoke(null, null);

            if (result.AsyncWaitHandle.WaitOne(5000))
                return insight;

            else
                return insight;
        }
    }
}

NOTE: If you're connected to the camera with In-Sight Explorer when you run this application, the In-Sight Explorer will disconnect from the camera and then try to reconnect after your application has disconnected from the camera.

0
On

You could use Native Mode Commands to set the value of controls in the spreadsheet in In-Sight Explorer (as discussed in the question you linked to). Note that you won't be able to write data to any cell - you will only be able to write to cells containing EditInt(), EditFloat(), EditString(), CheckBox(), etc functions. Send the commands as text over a socket connection to the cameras port 23. You will need to send a username and password to the camera when the connection is established.

0
On

If you're using the Cognex SDK, use the following functions

  • CvsInSight.SetFloat(...) to set EditFloat control values
  • CvsInSight.SetInteger(...) to set EditInt control values
  • CvsInSight.SetListBoxIndex(...) to select items in list boxes
  • CvsInSight.SetString(...) to set EditString control values
  • CvsInSight.SetCheckBox(...) to change the state of CheckBox controls