C#与VisionPro联合编程

C#与VisionPro联合

  • 1. 参照康耐视提供的样例
  • 2. 参照样例写一个
    • 1. 创建工程
    • 2. 添加引用
    • 3. 声明变量
    • 4. 初始化
    • 5. 刷新队列
    • 6. 用户数据获取
    • 7. 跨线程访问Windows控件--委托
    • 8. 显示图像
    • 9. 释放资源
  • 3. 代码
  • 4. 资源下载

1. 参照康耐视提供的样例

C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\EfficientApp\C#

C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\mySavedQB.vpp
C#与VisionPro联合编程_第1张图片

2. 参照样例写一个

1. 创建工程

创建一个Windows窗体应用程序(.NET Framework)

2. 添加引用

懒得一个一个分辨了,我全都添加进来。

同时,下面这两句不能落下:

using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;

3. 声明变量

        //定义变量
        CogJobManager m_jobManager;
        CogJob m_job;
        CogJobIndependent m_jobIndependent;

4. 初始化

            //初始化
            m_jobManager = (CogJobManager)CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\mySavedQB.vpp");
            m_job = m_jobManager.Job(0);
            m_jobIndependent = m_job.OwnedIndependent;

5. 刷新队列

            //刷新队列
            m_jobManager.UserQueueFlush();
            m_jobManager.FailureQueueFlush();
            m_job.ImageQueueFlush();
            m_jobIndependent.RealTimeQueueFlush();

6. 用户数据获取

            //结果处理
            m_jobManager.UserResultAvailable += M_jobManager_UserResultAvailable;
            
        private void M_jobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
        {
            //用户代码
            ICogRecord topRecord = m_jobManager.UserResult();
            ICogRecord tmp = topRecord.SubRecords[@"Tools.Item[""CogBlobTool1""].CogBlobTool.Results.GetBlobs().Count"];
            //throw new NotImplementedException();
        }
        private void ButtonRunOnce_Click(object sender, EventArgs e)
        {
            try
            {
                m_jobManager.Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }           
        }

已发送项:
C#与VisionPro联合编程_第2张图片

当上一次处理没有完成,就又立即点击运行按钮,就会出现下列异常。
C#与VisionPro联合编程_第3张图片
当然了,我们可以使用try catch语句来抛出异常
C#与VisionPro联合编程_第4张图片

7. 跨线程访问Windows控件–委托

delegate void UserResultDelegate(object sender, CogJobManagerActionEventArgs e);
        //用户可用的事件处理程序
        private void M_jobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new UserResultDelegate(M_jobManager_UserResultAvailable), new object[] { sender, e });
                return;
            }

            //用户代码
            ICogRecord topRecord = m_jobManager.UserResult();
            ICogRecord tmp = topRecord.SubRecords[@"Tools.Item[""CogBlobTool1""].CogBlobTool.Results.GetBlobs().Count"];
            //throw new NotImplementedException();
            int count = (int)tmp.Content;
            textBoxCount.Text = count.ToString();
        }

8. 显示图像

放置一个checkbox控件
修改以下几个参数
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

        private void CheckBoxRunContinue_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxRunContinue.Checked)
            {
                try
                {
                    m_jobManager.RunContinuous();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                buttonRunOnce.Enabled = false;
            }
            else
            {
                try
                {
                    m_jobManager.Stop();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                buttonRunOnce.Enabled = true;
            }
        }

9. 释放资源

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //窗口关闭
            m_jobManager.UserResultAvailable -= M_jobManager_UserResultAvailable;//取消事件关联
            cogRecordDisplay1.Dispose();
            m_jobManager.Shutdown();
        }

3. 代码

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 Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;

namespace FirstVP
{
    delegate void UserResultDelegate(object sender, CogJobManagerActionEventArgs e);

    public partial class Form1 : Form
    {
        //定义变量
        CogJobManager m_jobManager;
        CogJob m_job;
        CogJobIndependent m_jobIndependent;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //初始化
            m_jobManager = (CogJobManager)CogSerializer.LoadObjectFromFile(@"C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\mySavedQB.vpp");
            m_job = m_jobManager.Job(0);
            m_jobIndependent = m_job.OwnedIndependent;

            //刷新队列
            m_jobManager.UserQueueFlush();
            m_jobManager.FailureQueueFlush();
            m_job.ImageQueueFlush();
            m_jobIndependent.RealTimeQueueFlush();

            //结果处理
            m_jobManager.UserResultAvailable += M_jobManager_UserResultAvailable;
        }

        //用户可用的事件处理程序
        private void M_jobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new UserResultDelegate(M_jobManager_UserResultAvailable), new object[] { sender, e });
                return;
            }

            //用户代码
            ICogRecord topRecord = m_jobManager.UserResult();
            ICogRecord tmpRecord = topRecord.SubRecords[@"Tools.Item[""CogBlobTool1""].CogBlobTool.Results.GetBlobs().Count"];
            //throw new NotImplementedException();
            int count = (int)tmpRecord.Content;
            textBoxCount.Text = count.ToString();

            //显示图像
            tmpRecord = topRecord.SubRecords["ShowLastRunRecordForUserQueue"];
            tmpRecord = tmpRecord.SubRecords["LastRun"];
            tmpRecord = tmpRecord.SubRecords["Image Source.OutputImage"];
            cogRecordDisplay1.Record = tmpRecord;
            cogRecordDisplay1.Fit(true);
        }
        private void ButtonRunOnce_Click(object sender, EventArgs e)
        {
            try
            {
                m_jobManager.Run();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //throw;
            }           
        }

        private void CheckBoxRunContinue_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxRunContinue.Checked)
            {
                try
                {
                    m_jobManager.RunContinuous();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                buttonRunOnce.Enabled = false;
            }
            else
            {
                try
                {
                    m_jobManager.Stop();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                buttonRunOnce.Enabled = true;
            }
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            //窗口关闭
            m_jobManager.UserResultAvailable -= M_jobManager_UserResultAvailable;//取消事件关联
            cogRecordDisplay1.Dispose();
            m_jobManager.Shutdown();
        }
    }
}

4. 资源下载

C#与VisionPro联合编程.zip

你可能感兴趣的:(VisionPro)