VisionPro - VisionPro Advanced – Section 2 Building a VB.Net App Step 3 Lab Approximate Duration: 30 minutes môn Cơ sở Toán cho các nhà kinh tế 1 | Học viện Nông nghiệp Việt Nam

Add a checkbox to the Form to control Running the Jobcontinuously.• In the designer, drag a CheckBox onto your form. • Change the “Appearance” property to be “Button”.• Change the button text to “Run Continuous”.• Change the button name to “chkContinuous”. • Double-click on the CheckBox and start typing the following lines into the resulting “CheckedChanged”
 method:. Tài liệu giúp bạn tham  khảo, ôn tập và đạt kết quả cao. Mời đọc đón xem!

Trường:

Học viện Nông nghiệp Việt Nam 428 tài liệu

Thông tin:
8 trang 15 giờ trước

Bình luận

Vui lòng đăng nhập hoặc đăng ký để gửi bình luận.

VisionPro - VisionPro Advanced – Section 2 Building a VB.Net App Step 3 Lab Approximate Duration: 30 minutes môn Cơ sở Toán cho các nhà kinh tế 1 | Học viện Nông nghiệp Việt Nam

Add a checkbox to the Form to control Running the Jobcontinuously.• In the designer, drag a CheckBox onto your form. • Change the “Appearance” property to be “Button”.• Change the button text to “Run Continuous”.• Change the button name to “chkContinuous”. • Double-click on the CheckBox and start typing the following lines into the resulting “CheckedChanged”
 method:. Tài liệu giúp bạn tham  khảo, ôn tập và đạt kết quả cao. Mời đọc đón xem!

5 3 lượt tải Tải xuống
lOMoARcPSD| 47708777
VisionPro - VisionPro Advanced Section 2
Building a VB.Net App Step 3 Lab
Approximate Duration: 30 minutes
The following may be used as a reference on the basic steps for building a VisionPro application in Microsoft
Visual Basic .NET. This should be used in conjunction with the notes presented in Section 2 of the class.
Objectives:
Add a checkbox to run the CogJob continuously.
Display the close buttons whe it is OK to close the application.
Display RunStatus of the CogJob each time it runs.
Add a checkbox to the Form to control Running the Job continuously.
In the designer, drag a CheckBox onto your form.
Change the “Appearance” property to be “Button”.
Change the button text to “Run Continuous”.
Change the button name to “chkContinuous”.
Double-click on the CheckBox and start typing the following lines into the resulting “CheckedChanged”
method:
If (chkContinuous.Checked) Then
'Disable buttons while running application
btnRunOnce.Enabled = False 'Attempt to run
the application continuously
Try
myJobManager.RunContinuous()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
'Enable buttons when stopping continuous execution
chkContinuous.Enabled = True Try
myJobManager.Stop() Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
Add a line to the bottom of the Stopped event handler: chkContinuous.Enabled =
True
lOMoARcPSD| 47708777
\
Add another to the btnRunOnce_Clicked method, where shown: chkContinuous.Enabled
= False
Display the from close button (Control Box) only when it is OK to close the application
Add the following to btnRunOnce_Clicked and chkContinuous_Checked function: ControlBox
= False
Add the following to myJobManager_Stopped event handler function: ControlBox = True
Add a Status Label to display information about the RunStatus
Add a new Status Strip Windows control and within it add a Status Label to your form and call it
stlStatus.
Change its Text property to show the word “Ready” by default
Using the drop-downs, add a new UserResultAvailable handler from the the CogJobManager:
Private Sub myJobManager_UserResultAvailable(ByVal sender As Object, _
ByVal e As Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs)_
Handles myJobManager.UserResultAvailable
'Check to see if we are calling Windows forms from other threads
If InvokeRequired Then
'Create a pointer to this function
Dim myDel As New myJobManagerDelegate(AddressOf _
myJobManager_UserResultAvailable)
'Call this same function on the correct thread
Dim eventArgs() As Object = {sender, e}
Invoke(myDel, eventArgs)
Return
End If
'Declare and assign local ICogRecord variable
Dim topRecord As Cognex.VisionPro.ICogRecord = _
myJobManager.UserResult
'Populate status label with application information
stlStatus.Text = _
topRecord.SubRecords("UserResultTag").Content & ": " _
lOMoARcPSD| 47708777
& topRecord.SubRecords("JobName").Content & " --> " _
Complete Solution (in VB)
Imports Cognex.VisionPro
Imports Cognex.VisionPro.QuickBuild
Public Class Form1
Private WithEvents myJobManager As CogJobManager
Private myJob As CogJob
Private myIndependentJob As CogJobIndependent
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Shut down the CogJobManager to clean up threads properly
myJob.AcqFifo.FrameGrabber.Disconnect(False) myJobManager.Shutdown()
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles
Me.Load
'Load your QuickBuild application
myJobManager = CType( _
CogSerializer.LoadObjectFromFile( _
"C:\Documents and Settings\mperez\My
Documents\Temp\Cognex\VisionPro\Demos\DemoPlate.vpp"), _
CogJobManager)
'Initialize variable myJob =
myJobManager.Job("CogJob1")
myIndependentJob = myJob.OwnedIndependent
'Flush queues
myJobManager.UserQueueFlush()
myJobManager.FailureQueueFlush()
myJob.ImageQueueFlush()
myIndependentJob.RealTimeQueueFlush()
End Sub
Private Sub btnRunOnce_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRunOnce.Click
& topRecord.SubRecords(
"RunStatus"
)
.Content.ToString
lOMoARcPSD| 47708777
\
'Disable buttons while running application
btnRunOnce.Enabled = False
chkContinuous.Enabled = False ControlBox =
False
'Attempt to run the application
Try
myJobManager.Run()
Catch ex As Exception
'If there is a problem show the error message
MessageBox.Show(ex.Message)
End Try
End Sub
'Delegate whose signature matches CJM events.
Delegate Sub myJobManagerDelegate(ByVal sender As Object, ByVal e As
CogJobManagerActionEventArgs)
Private Sub myJobManager_Stopped(ByVal sender As Object, ByVal e As
Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs) Handles myJobManager.Stopped
'Check to see if we are calling Windows forms from other threads
If InvokeRequired Then
' Create a pointer to this function
Dim myDel As New myJobManagerDelegate(AddressOf myJobManager_Stopped)
' Call this same function on the correct thread
Dim eventArgs() As Object = {sender, e}
Invoke(myDel, eventArgs)
Return
End If
'Enable the buttons after running application
btnRunOnce.Enabled = True
chkContinuous.Enabled = True ControlBox =
True
End Sub
Private Sub chkContinuous_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles chkContinuous.CheckedChanged
If (chkContinuous.Checked) Then
'Disable buttons while running application
btnRunOnce.Enabled = False
ControlBox = False
lOMoARcPSD| 47708777
'Attempt to run the application continuously
Try
myJobManager.RunContinuous()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
'Enable buttons when stopping continuous execution
chkContinuous.Enabled = True Try
myJobManager.Stop() Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Private Sub myJobManager_UserResultAvailable(ByVal sender As Object, ByVal e As
Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs) Handles
myJobManager.UserResultAvailable
'Check to see if we are calling Windows forms from other threads
If InvokeRequired Then
'Create a pointer to this function
Dim myDel As New myJobManagerDelegate(AddressOf
myJobManager_UserResultAvailable)
'Call this same function on the correct thread
Dim eventArgs() As Object = {sender, e}
Invoke(myDel, eventArgs)
Return
End If
'Declare and assign local ICogRecord variable
Dim topRecord As Cognex.VisionPro.ICogRecord = myJobManager.UserResult
'Populate status label with application information
stlStatus.Text = _
topRecord.SubRecords("UserResultTag").Content & ": " _ &
topRecord.SubRecords("JobName").Content & " --> " _
& topRecord.SubRecords("RunStatus").Content.ToString
End Sub End
Class
lOMoARcPSD| 47708777
\
Complete Solution (in C#)
using System;
using System.Collections.Generic;
using System.ComponentModel; using
System.Data; using System.Drawing;
using System.Linq; using
System.Text;
using System.Windows.Forms;
using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild;
namespace May2013_VPro
{ public partial class Form1 :
Form
{
private CogJobManager myJobManager;
private CogJob myJob;
private CogJobIndependent myIndependentJob;
public
Form1()
{
InitializeComponent();
// Load QuickBuild Application
myJobManager = (CogJobManager)(CogSerializer.LoadObjectFromFile(@"C:\MyDataAnalysis.vpp"));
// Initialize variables
myJob = myJobManager.Job("CogJob1");
myIndependentJob = myJob.OwnedIndependent;
// Flush Queues
myJobManager.UserQueueFlush();
myJobManager.FailureQueueFlush(); myJob.ImageQueueFlush();
myIndependentJob.RealTimeQueueFlush();
// Register Job Manager events (equivalent to WithEvents in vb)
myJobManager.Stopped += new
CogJobManager.CogJobManagerStoppedEventHandler(myJobManager_Stopped);
myJobManager.UserResultAvailable += new
CogJobManager.CogUserResultAvailableEventHandler(myJobManager_UserResultAvailable);
}
private void myJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e)
{
if (InvokeRequired)
{
myJobManagerDelegate myDel = new
myJobManagerDelegate(myJobManager_UserResultAvailable);
Object[] eventArgs = { sender, e };
Invoke(myDel, eventArgs); return;
lOMoARcPSD| 47708777
}
// Declare and assign local ICogRecord varialble
ICogRecord topRecord = myJobManager.UserResult();
// Populate status label with application information
stlStatus.Text = topRecord.SubRecords["UserResultTag"].Content.ToString() + ": " +
topRecord.SubRecords["JobName"].Content.ToString() + " --> " +
topRecord.SubRecords["RunStatus"].Content.ToString();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// Gracefully clean up threads
myJob.AcqFifo.FrameGrabber.Disconnect(false);
myJobManager.Shutdown();
}
private void btnRunOnce_Click(object sender, EventArgs e)
{
ControlBox = false;
btnRunOnce.Enabled = false;
chkContinuous.Enabled = false; try
{
myJobManager.Run();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
delegate void myJobManagerDelegate(object sender, CogJobManagerActionEventArgs e);
private void myJobManager_Stopped(object sender, CogJobManagerActionEventArgs e) {
// Cross-thread handling
if (InvokeRequired)
{
myJobManagerDelegate myDel = new myJobManagerDelegate(myJobManager_Stopped);
Object[] eventArgs = {sender, e}; Invoke(myDel, eventArgs);
return;
}
// Enable button when job is complete
ControlBox = true; btnRunOnce.Enabled
= true; chkContinuous.Enabled = true;
}
private void chkContinuous_CheckedChanged(object sender, EventArgs e)
{
if (chkContinuous.Checked)
{
btnRunOnce.Enabled = false;
ControlBox = false;
try
{
myJobManager.RunContinuous();
}
catch (Exception ex)
lOMoARcPSD| 47708777
\
{
MessageBox.Show(ex.Message);
}
} else
{
btnRunOnce.Enabled = true;
try
{
myJobManager.Stop();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
}
| 1/8

Preview text:

lOMoAR cPSD| 47708777
VisionPro - VisionPro Advanced – Section 2
Building a VB.Net App Step 3 Lab
Approximate Duration: 30 minutes
The following may be used as a reference on the basic steps for building a VisionPro application in Microsoft
Visual Basic .NET. This should be used in conjunction with the notes presented in Section 2 of the class. Objectives:
 Add a checkbox to run the CogJob continuously.
 Display the close buttons whe it is OK to close the application.
 Display RunStatus of the CogJob each time it runs.
Add a checkbox to the Form to control Running the Job continuously.
• In the designer, drag a CheckBox onto your form.
• Change the “Appearance” property to be “Button”.
• Change the button text to “Run Continuous”.
• Change the button name to “chkContinuous”.
• Double-click on the CheckBox and start typing the following lines into the resulting “CheckedChanged” method:
If (chkContinuous.Checked) Then
'Disable buttons while running application
btnRunOnce.Enabled = False 'Attempt to run the application continuously Try myJobManager.RunContinuous() Catch ex As Exception MessageBox.Show(ex.Message) End Try Else
'Enable buttons when stopping continuous execution
chkContinuous.Enabled = True Try
myJobManager.Stop() Catch ex As Exception MessageBox.Show(ex.Message) End Try End If
• Add a line to the bottom of the Stopped event handler: chkContinuous.Enabled = True lOMoAR cPSD| 47708777
• Add another to the btnRunOnce_Clicked method, where shown: chkContinuous.Enabled = False
Display the from close button (Control Box) only when it is OK to close the application
• Add the following to btnRunOnce_Clicked and chkContinuous_Checked function: ControlBox = False
• Add the following to myJobManager_Stopped event handler function: ControlBox = True
Add a Status Label to display information about the RunStatus
• Add a new Status Strip Windows control and within it add a Status Label to your form and call it stlStatus.
• Change its Text property to show the word “Ready” by default
• Using the drop-downs, add a new UserResultAvailable handler from the the CogJobManager:
Private Sub myJobManager_UserResultAvailable(ByVal sender As Object, _
ByVal e As Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs)_
Handles myJobManager.UserResultAvailable
'Check to see if we are calling Windows forms from other threads If InvokeRequired Then
'Create a pointer to this function
Dim myDel As New myJobManagerDelegate(AddressOf _
myJobManager_UserResultAvailable)
'Call this same function on the correct thread
Dim eventArgs() As Object = {sender, e} Invoke(myDel, eventArgs) Return End If
'Declare and assign local ICogRecord variable
Dim topRecord As Cognex.VisionPro.ICogRecord = _ myJobManager.UserResult
'Populate status label with application information stlStatus.Text = _
topRecord.SubRecords("UserResultTag").Content & ": " _ \ lOMoAR cPSD| 47708777
& topRecord.SubRecords("JobName").Content & " --> " _ Complete Solution (in VB) Imports Cognex.VisionPro
Imports Cognex.VisionPro.QuickBuild Public Class Form1
Private WithEvents myJobManager As CogJobManager Private myJob As CogJob
Private myIndependentJob As CogJobIndependent
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Shut down the CogJobManager to clean up threads properly
myJob.AcqFifo.FrameGrabber.Disconnect(False) myJobManager.Shutdown() End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
'Load your QuickBuild application myJobManager = CType( _
CogSerializer.LoadObjectFromFile( _
"C:\Documents and Settings\mperez\My
Documents\Temp\Cognex\VisionPro\Demos\DemoPlate.vpp"), _ CogJobManager) 'Initialize variable myJob = myJobManager.Job("CogJob1")
myIndependentJob = myJob.OwnedIndependent 'Flush queues myJobManager.UserQueueFlush()
myJobManager.FailureQueueFlush() myJob.ImageQueueFlush()
myIndependentJob.RealTimeQueueFlush() End Sub
Private Sub btnRunOnce_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnRunOnce.Click
& topRecord.SubRecords( "RunStatus" ) .Content.ToString lOMoAR cPSD| 47708777
'Disable buttons while running application btnRunOnce.Enabled = False
chkContinuous.Enabled = False ControlBox = False
'Attempt to run the application Try myJobManager.Run() Catch ex As Exception
'If there is a problem show the error message MessageBox.Show(ex.Message) End Try End Sub
'Delegate whose signature matches CJM events.
Delegate Sub myJobManagerDelegate(ByVal sender As Object, ByVal e As CogJobManagerActionEventArgs)
Private Sub myJobManager_Stopped(ByVal sender As Object, ByVal e As
Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs) Handles myJobManager.Stopped
'Check to see if we are calling Windows forms from other threads If InvokeRequired Then
' Create a pointer to this function
Dim myDel As New myJobManagerDelegate(AddressOf myJobManager_Stopped)
' Call this same function on the correct thread
Dim eventArgs() As Object = {sender, e} Invoke(myDel, eventArgs) Return End If
'Enable the buttons after running application btnRunOnce.Enabled = True
chkContinuous.Enabled = True ControlBox = True End Sub
Private Sub chkContinuous_CheckedChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles chkContinuous.CheckedChanged
If (chkContinuous.Checked) Then
'Disable buttons while running application btnRunOnce.Enabled = False ControlBox = False \ lOMoAR cPSD| 47708777
'Attempt to run the application continuously Try myJobManager.RunContinuous() Catch ex As Exception MessageBox.Show(ex.Message) End Try Else
'Enable buttons when stopping continuous execution
chkContinuous.Enabled = True Try
myJobManager.Stop() Catch ex As Exception MessageBox.Show(ex.Message) End Try End If End Sub
Private Sub myJobManager_UserResultAvailable(ByVal sender As Object, ByVal e As
Cognex.VisionPro.QuickBuild.CogJobManagerActionEventArgs) Handles
myJobManager.UserResultAvailable
'Check to see if we are calling Windows forms from other threads If InvokeRequired Then
'Create a pointer to this function
Dim myDel As New myJobManagerDelegate(AddressOf
myJobManager_UserResultAvailable)
'Call this same function on the correct thread
Dim eventArgs() As Object = {sender, e} Invoke(myDel, eventArgs) Return End If
'Declare and assign local ICogRecord variable
Dim topRecord As Cognex.VisionPro.ICogRecord = myJobManager.UserResult
'Populate status label with application information stlStatus.Text = _
topRecord.SubRecords("UserResultTag").Content & ": " _ &
topRecord.SubRecords("JobName").Content & " --> " _
& topRecord.SubRecords("RunStatus").Content.ToString End Sub End Class lOMoAR cPSD| 47708777
Complete Solution (in C#) using System;
using System.Collections.Generic;
using System.ComponentModel; using
System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Cognex.VisionPro;
using Cognex.VisionPro.QuickBuild; namespace May2013_VPro
{ public partial class Form1 : Form {
private CogJobManager myJobManager; private CogJob myJob;
private CogJobIndependent myIndependentJob; public Form1() { InitializeComponent();
// Load QuickBuild Application
myJobManager = (CogJobManager)(CogSerializer.LoadObjectFromFile(@"C:\MyDataAnalysis.vpp")); // Initialize variables
myJob = myJobManager.Job("CogJob1");
myIndependentJob = myJob.OwnedIndependent; // Flush Queues
myJobManager.UserQueueFlush();
myJobManager.FailureQueueFlush(); myJob.ImageQueueFlush();
myIndependentJob.RealTimeQueueFlush();
// Register Job Manager events (equivalent to WithEvents in vb) myJobManager.Stopped += new
CogJobManager.CogJobManagerStoppedEventHandler(myJobManager_Stopped);
myJobManager.UserResultAvailable += new
CogJobManager.CogUserResultAvailableEventHandler(myJobManager_UserResultAvailable); }
private void myJobManager_UserResultAvailable(object sender, CogJobManagerActionEventArgs e) { if (InvokeRequired) {
myJobManagerDelegate myDel = new
myJobManagerDelegate(myJobManager_UserResultAvailable);
Object[] eventArgs = { sender, e };
Invoke(myDel, eventArgs); return; \ lOMoAR cPSD| 47708777 }
// Declare and assign local ICogRecord varialble
ICogRecord topRecord = myJobManager.UserResult();
// Populate status label with application information
stlStatus.Text = topRecord.SubRecords["UserResultTag"].Content.ToString() + ": " +
topRecord.SubRecords["JobName"].Content.ToString() + " --> " +
topRecord.SubRecords["RunStatus"].Content.ToString(); }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) {
// Gracefully clean up threads
myJob.AcqFifo.FrameGrabber.Disconnect(false); myJobManager.Shutdown(); }
private void btnRunOnce_Click(object sender, EventArgs e) { ControlBox = false; btnRunOnce.Enabled = false;
chkContinuous.Enabled = false; try { myJobManager.Run(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
delegate void myJobManagerDelegate(object sender, CogJobManagerActionEventArgs e);
private void myJobManager_Stopped(object sender, CogJobManagerActionEventArgs e) { // Cross-thread handling if (InvokeRequired) {
myJobManagerDelegate myDel = new myJobManagerDelegate(myJobManager_Stopped);
Object[] eventArgs = {sender, e}; Invoke(myDel, eventArgs); return; }
// Enable button when job is complete
ControlBox = true; btnRunOnce.Enabled
= true; chkContinuous.Enabled = true; }
private void chkContinuous_CheckedChanged(object sender, EventArgs e) { if (chkContinuous.Checked) { btnRunOnce.Enabled = false; ControlBox = false; try {
myJobManager.RunContinuous(); } catch (Exception ex) lOMoAR cPSD| 47708777 { MessageBox.Show(ex.Message); } } else { btnRunOnce.Enabled = true; try { myJobManager.Stop(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } } } } \