204 lines
6.8 KiB
C#
204 lines
6.8 KiB
C#
|
using Advantech.Motion;
|
|||
|
using Newtonsoft.Json;
|
|||
|
using ProductionControl.Device;
|
|||
|
using ProductionControl.Utils;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Drawing;
|
|||
|
using System.IO;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using System.Windows.Forms;
|
|||
|
using System.Xml.Linq;
|
|||
|
using static ProductionControl.Device.Axis;
|
|||
|
using static ProductionControl.Device.Scanner;
|
|||
|
using static ProductionControl.UI.UIAxis;
|
|||
|
|
|||
|
namespace ProductionControl.UI
|
|||
|
{
|
|||
|
public partial class UIScanner : UserControl
|
|||
|
{
|
|||
|
SynchronizationContext SyncContext = null;
|
|||
|
|
|||
|
public Action<int, string> log;
|
|||
|
|
|||
|
//
|
|||
|
private ScannerProp prop;
|
|||
|
private Scanner dev;
|
|||
|
public UIScanner()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
//获取UI线程同步上下文
|
|||
|
SyncContext = SynchronizationContext.Current;
|
|||
|
//init();
|
|||
|
}
|
|||
|
public void init(ScannerType devType)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
prop = new ScannerProp(devType);
|
|||
|
this.propertyGrid1.SelectedObject = prop;
|
|||
|
dev = new Scanner(prop.DeviceType);
|
|||
|
dev.log = log;
|
|||
|
//DATA
|
|||
|
dev.ScanEvent += new System.Action<int, string>((num, path) => {
|
|||
|
prop.ImageList[num - 1] = path;
|
|||
|
this.refreshUI();
|
|||
|
});
|
|||
|
|
|||
|
dev.open();
|
|||
|
dev.start(IntPtr.Zero);
|
|||
|
dev.getParam();
|
|||
|
prop.ExposureTime = dev.ExposureTime;
|
|||
|
prop.Gain= dev.Gain;
|
|||
|
prop.ResultingFrameRate = dev.ResultingFrameRate;
|
|||
|
|
|||
|
this.toolStrip1.Enabled = true;
|
|||
|
this.propertyGrid1.Enabled = true;
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.toolStrip1.Enabled = false;
|
|||
|
this.propertyGrid1.Enabled = false;
|
|||
|
//MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
this.closeDev();
|
|||
|
}
|
|||
|
}
|
|||
|
private void refreshUI()
|
|||
|
{
|
|||
|
SyncContext.Post(m =>
|
|||
|
{
|
|||
|
var result = m as string;
|
|||
|
propertyGrid1.Refresh();
|
|||
|
//tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
|
|||
|
//tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
|
|||
|
}, "异步操作完成结果");
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void closeDev()
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
if (dev == null)
|
|||
|
return;
|
|||
|
dev.stop();
|
|||
|
dev.close();
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
this.toolStrip1.Enabled = false;
|
|||
|
this.propertyGrid1.Enabled = false;
|
|||
|
//MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
protected override void OnHandleDestroyed(EventArgs e)
|
|||
|
{
|
|||
|
base.OnHandleDestroyed(e);
|
|||
|
// 在此添加需要手动释放资源的代码
|
|||
|
this.closeDev();
|
|||
|
}
|
|||
|
|
|||
|
private void tbtnRun_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
dev.setParam(prop.ExposureTime,prop.Gain,prop.ResultingFrameRate);
|
|||
|
dev.getParam();
|
|||
|
prop.ExposureTime = dev.ExposureTime;
|
|||
|
prop.Gain = dev.Gain;
|
|||
|
prop.ResultingFrameRate = dev.ResultingFrameRate;
|
|||
|
}
|
|||
|
catch(Exception ex)
|
|||
|
{
|
|||
|
MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void tbtnExport_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
string filePath = FileUtil.saveAsFile($"Scanner_config.json", "JSON|*.json");
|
|||
|
if (filePath != "")
|
|||
|
{
|
|||
|
string jsonText = prop.serialize();
|
|||
|
File.WriteAllText(filePath, jsonText);
|
|||
|
MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void tbtnImport_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
string filePath = FileUtil.openFile("JSON|*.json");
|
|||
|
if (filePath != "")
|
|||
|
{
|
|||
|
string jsonText = File.ReadAllText(filePath);
|
|||
|
prop.deserialize(jsonText);
|
|||
|
this.propertyGrid1.Refresh();
|
|||
|
//this.propertyGrid1.SelectedObject= prop;
|
|||
|
//MessageBox.Show("加载成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
|||
|
}
|
|||
|
}
|
|||
|
private void tbtnScan_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
prop.ImageList = new string[dev.isContinuousMode ? 2 : 1];
|
|||
|
dev.scan(prop.ImageList.Length);
|
|||
|
}
|
|||
|
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
|
|||
|
{
|
|||
|
//其中包含了两个重要的属性:OldValue和ChangeItem。
|
|||
|
//ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
|
|||
|
//而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
|
|||
|
|
|||
|
string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
|
|||
|
string propertyName = e.ChangedItem.PropertyDescriptor.Name;
|
|||
|
var oldValue = e.OldValue;
|
|||
|
var newValue = e.ChangedItem.Value;
|
|||
|
switch (propertyName)
|
|||
|
{
|
|||
|
//case "ChannelIndex":
|
|||
|
// if((int)newValue <1 || (int)newValue >4)
|
|||
|
// {
|
|||
|
// prop.ChannelIndex = (int)oldValue;
|
|||
|
// this.refreshUI();
|
|||
|
// }
|
|||
|
// else
|
|||
|
// {
|
|||
|
// dev.getDigitalValue((int)newValue);
|
|||
|
// }
|
|||
|
// break;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private void tbtnMode_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if (!dev.isContinuousMode)
|
|||
|
{
|
|||
|
dev.setMode(true);
|
|||
|
tbtnMode.Text = "软触发";
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
dev.setMode(false);
|
|||
|
tbtnMode.Text = "连续";
|
|||
|
}
|
|||
|
}
|
|||
|
FrmScannerShow frm = new FrmScannerShow();
|
|||
|
private void tbtnShow_Click(object sender, EventArgs e)
|
|||
|
{
|
|||
|
if(!frm.IsDisposed)
|
|||
|
frm.Close();
|
|||
|
|
|||
|
frm = new FrmScannerShow();
|
|||
|
frm.Show();
|
|||
|
dev.setPreviewWin(frm.Handle);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|