C#与VisionPro联合编程
2023-12-26 22:59:19
C#与VisionPro联合
1. 参照康耐视提供的样例
C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\EfficientApp\C#
C:\Program Files\Cognex\VisionPro\samples\Programming\QuickBuild\mySavedQB.vpp
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;
}
}
已发送项:
当上一次处理没有完成,就又立即点击运行按钮,就会出现下列异常。
当然了,我们可以使用try catch语句来抛出异常
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. 资源下载
文章来源:https://blog.csdn.net/weixin_38566632/article/details/135219066
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!