Visionpro联合C#二次开发之CogAcqFifoTool

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);
                }

            }
        }
    }
}

代码不是很规范,如有错误,望指出,谢谢!

  • 3
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值