1.工具介绍
在Visionpro中:
2.在C#中常用的代码
相关引用+初始化
using Cognex.VisionPro;
CogAcqFifoTool cogAcqFifoTool = new CogAcqFifoTool();
加载设置好的vpp文件
cogAcqFifoTool = CogSerializer.LoadObjectFromFile(VppPath) as CogAcqFifoTool;
设置相关参数
cogAcqFifoTool.Operator.*
运行工具
cogAcqFifoTool.Run();
取图
cogAcqFifoTool.OutputImage;
获取工具运行的信息
cogAcqFifoTool.RunStatus.*
3.实际案例
功能介绍:
1.通过CogAcqFifoTool工具进行取像并显示在窗口上
2.连续运行CogAcqFifoTool工具并记录下每次工具运行的耗时(自带的记录耗时和自己计时的耗时)
界面:
代码:
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.IO;
using System.Threading;
using Cognex.VisionPro;
using Cognex.VisionPro.Exceptions;
namespace test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public CogAcqFifoTool cogAcqFifoTool = new CogAcqFifoTool();
public CogAcqFifoTool cogAcqFifoTool2 = new CogAcqFifoTool();
private void checkBoxLeft_CheckedChanged(object sender, EventArgs e)
{
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
string path = "";
if (checkBoxLeft.Checked)
{
checkBoxLeft.Text = "停止";
Task task1 = Task.Factory.StartNew(() =>
{
try
{
using (OpenFileDialog opendlg = new OpenFileDialog())
{
opendlg.Filter = "相机工具文件|*.vpp";
opendlg.Title = "请选择事先保存好的相机工具文件";
DialogResult result = opendlg.ShowDialog();
if (DialogResult.OK == result)
{
path = opendlg.FileName;
}
}
cogAcqFifoTool = CogSerializer.LoadObjectFromFile(path) as CogAcqFifoTool;
while (checkBoxLeft.Checked)
{
DateTime S = DateTime.Now;
cogAcqFifoTool.Run();
DateTime E = DateTime.Now;
this.Invoke(new Action(() =>
{
label1.Text = E.Subtract(S).TotalMilliseconds.ToString();
label2.Text = cogAcqFifoTool.RunStatus.TotalTime.ToString();
}));
cogRecordDisplayLeft.Image = cogAcqFifoTool.OutputImage;
cogRecordDisplayLeft.Fit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}, token);
}
else
{
try
{
checkBoxLeft.Text = "运行";
cts.Cancel();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void checkBoxRight_CheckedChanged(object sender, EventArgs e)
{
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken token = cts.Token;
string path ="";
if (checkBoxRight.Checked)
{
checkBoxRight.Text = "停止";
Task task2 = Task.Factory.StartNew(() =>
{
try
{
using (OpenFileDialog opendlg = new OpenFileDialog())
{
opendlg.Filter = "相机工具文件|*.vpp";
opendlg.Title = "请选择事先保存好的相机工具文件";
DialogResult result = opendlg.ShowDialog();
if (DialogResult.OK == result)
{
path = opendlg.FileName;
}
}
CogAcqFifoTool cogAcqFifoTool2 = CogSerializer.LoadObjectFromFile(path) as CogAcqFifoTool;
while (checkBoxRight.Checked)
{
DateTime S = DateTime.Now;
cogAcqFifoTool2.Run();
DateTime E = DateTime.Now;
this.Invoke(new Action(() =>
{
label3.Text = E.Subtract(S).TotalMilliseconds.ToString();
label4.Text = cogAcqFifoTool2.RunStatus.TotalTime.ToString();
}));
cogRecordDisplayRight.Image = cogAcqFifoTool2.OutputImage;
cogRecordDisplayRight.Fit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}, token);
}
else
{
try
{
checkBoxRight.Text = "运行";
cts.Cancel();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
代码不是很规范,如有错误,望指出,谢谢!