banboshi_V1/halftoneproject-master/Code/UI/UIAxisDev.cs
2023-10-31 13:19:29 +08:00

509 lines
19 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Advantech.Motion;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using ProductionControl.Device;
using ProductionControl.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
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.AxisDev;
using static ProductionControl.UI.UIAxisDev;
namespace ProductionControl.UI
{
public partial class UIAxisDev : UserControl
{
SynchronizationContext SyncContext = null;
public Action<string> GetParamsEvent;
//
private AxisDevProp prop = new AxisDevProp();
private AxisDev dev;
private JObject dataReturn = new JObject();
//
ScannerDev devScannerGentl;
ScannerDev devScannerCC;
public UIAxisDev()
{
InitializeComponent();
propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
refreshUIState();
//获取UI线程同步上下文
SyncContext = SynchronizationContext.Current;
//init();
}
private void refreshUIState()
{
foreach (ToolStripItem item in this.toolStrip1.Items)
{
if (item.Text == "打开设备")
item.Visible = (dev == null || !dev.IsInit);
else
this.propertyGrid1.Enabled=item.Enabled = !(dev == null || !dev.IsInit);
}
}
public string getParamsData()
{
if (prop.Enable)
{
string axisIndex = ((int)prop.AxisIndex).ToString();
Utils.Util.addKey(dataReturn, axisIndex, JObject.Parse(prop.serialize()));
}
//return prop.serialize();
return dataReturn.ToString();
}
private void tbtnSave_Click(object sender, EventArgs e)
{
if (dataReturn.Properties().Count() < 1)
{
MessageBox.Show("请设置需要启用的轴!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
//GetParamsEvent?.Invoke(prop.serialize());
GetParamsEvent?.Invoke(getParamsData());
}
public bool stopNow()
{
if (dev != null && dev.IsInit)
{
dev.stopNow();
return true;
}
else
return false;
}
public void setParamsData(string json)
{
tsbtnScannerList.Visible = true;
if (json == "") return;
JObject dataReturnTmp = JObject.Parse(json);
//兼容旧版单轴
if (!dataReturnTmp.ContainsKey("0") && !dataReturnTmp.ContainsKey("1") && !dataReturnTmp.ContainsKey("2") && !dataReturnTmp.ContainsKey("3"))
{
if(!dataReturnTmp.ContainsKey("Enable"))
dataReturnTmp.Add("Enable",true);
dataReturn.Add(dataReturnTmp.Value<int>("AxisIndex").ToString(), dataReturnTmp);
}
else
dataReturn = dataReturnTmp;
//
foreach (var property in dataReturn.Properties())
{
prop.deserialize(property.Value.ToString());
break;
}
this.propertyGrid1.Refresh();
}
public void init()
{
this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
this.propertyGrid1.SelectedObject = prop;
dev = new AxisDev();
dev.WarningEvent = (level, info) =>
{
txtLog.Text = $"({level}){info}";
};
//STATE
dev.axisStateEvent += new System.Action<int, AxisStateType, uint>((axisIndex, type, stateValue) =>
{
if (axisIndex != (int)prop.AxisIndex)
return;
switch (type)
{
case AxisStateType.AxisState:
if (prop.AxState != (AxisState)stateValue)
{
prop.AxState = (AxisState)stateValue;
this.refreshUI();
}
break;
case AxisStateType.AxisIOState:
if (prop.AxIOStatus != stateValue)
{
prop.AxIOStatus = stateValue;
prop.ALM = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_ALM) > 0;//需IO close
prop.EMG = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_EMG) > 0;//需重置 state
prop.LMTP = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_LMTP) > 0;//右极限 true->false
prop.LMTN = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_LMTN) > 0;//左极限
prop.ORG = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_ORG) > 0;
prop.EZ = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_EZ) > 0;
prop.SVON = (stateValue & (uint)Ax_Motion_IO.AX_MOTION_IO_SVON) > 0;
this.refreshUI();
}
break;
case AxisStateType.AxisMotionState:
if (prop.AxMotionState != stateValue)
{
prop.AxMotionState = stateValue;
this.refreshUI();
}
break;
}
});
//位置POS
dev.axisPosEvent += new System.Action<int, AxisPosType, double>((axisIndex, type, pos) =>
{
if (axisIndex != (int)prop.AxisIndex)
return;
switch (type)
{
case AxisPosType.CmdPos:
if (prop.CmdPos != pos)
{
prop.CmdPos = pos;
this.refreshUI();
}
break;
case AxisPosType.ActualPos:
if (prop.ActualPos != pos)
{
prop.ActualPos = pos;
this.refreshUI();
}
break;
}
});
if (!dev.start(Config.Axis_PulseOutMode,true))
{
this.closeDev();
return;
}
refreshAxisVelParam();
refreshUIState();
}
private void refreshUI(bool must=false)
{
SyncContext.Post(m =>
{
var result = m as string;
if(!stopRefresh || must)
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 refreshAxisVelParam()
{
int axisIndex = (int)prop.AxisIndex;
//prop.HomeMode = (AxitHomeMode)Config.Axis_HomeMode[axisIndex];
var values = dev.getAxisVelParam(axisIndex);
prop.VelLow = dev.tomm(values[0], Config.Axis_MM2PulseNum[axisIndex]);
prop.VelHigh = dev.tomm(values[1], Config.Axis_MM2PulseNum[axisIndex]);
prop.Acc = dev.tomm(values[2], Config.Axis_MM2PulseNum[axisIndex]);
prop.Dec = dev.tomm(values[3], Config.Axis_MM2PulseNum[axisIndex]);
}
private void closeDev()
{
try
{
if (dev == null)
return;
dev.stop();
closeScannerDev();
}
catch (Exception ex)
{
this.refreshUIState();
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
{
this.stopRefresh = false;
dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex);
dev.move_ptp((int)prop.AxisIndex, prop.Value, prop.MoveMode);
//running...
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void tbtnExport_Click(object sender, EventArgs e)
{
string filePath = FileUtil.saveAsFile($"Axis配置.json", "JSON|*.json");
if (filePath != "")
{
//string jsonText = prop.serialize();
string jsonText = dataReturn.ToString();
File.WriteAllText(filePath, jsonText);
MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void tbtnImport_Click(object sender, EventArgs e)
{
string filePath = FileUtil.selectFile("JSON|*.json");
if (filePath != "")
{
string jsonText = File.ReadAllText(filePath);
JObject dataReturnTmp = JObject.Parse(jsonText);
//兼容旧版单轴
if (!dataReturnTmp.ContainsKey("0") && !dataReturnTmp.ContainsKey("1") && !dataReturnTmp.ContainsKey("2") && !dataReturnTmp.ContainsKey("3"))
{
dataReturnTmp.Add("Enable", true);
dataReturn.Add(dataReturnTmp.Value<int>("AxisIndex").ToString(), dataReturnTmp);
}
else
dataReturn = dataReturnTmp;
//
foreach (var property in dataReturn.Properties())
{
prop.deserialize(property.Value.ToString());
break;
}
this.propertyGrid1.Refresh();
}
}
private void tbtnHome_Click(object sender, EventArgs e)
{
this.stopRefresh = false;
int axitIndex = (int)prop.AxisIndex;
dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex);
//dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex,true);
dev.home(axitIndex, (uint)Config.Axis_HomeMode[axitIndex], (uint)Config.Axis_HomeDir[axitIndex]);
//if (prop.DeviceType == AxisType.默认)
//{
// dev.home((int)prop.AxisIndex);
//}
//else //直线电机
//{
// //dev.move_ptp((int)prop.AxisIndex, - prop.ActualPos, false);
// dev.move_ptp((int)prop.AxisIndex, 0, true);
//}
}
private void tbtnLeft_MouseDown(object sender, MouseEventArgs e)
{
this.stopRefresh = false;
dev.jog((int)prop.AxisIndex, 1);
}
private void tbtnLeft_MouseUp(object sender, MouseEventArgs e)
{
dev.stopDec((int)prop.AxisIndex);
}
private void tbtnRight_MouseDown(object sender, MouseEventArgs e)
{
this.stopRefresh = false;
dev.jog((int)prop.AxisIndex, 0);
}
private void tbtnRight_MouseUp(object sender, MouseEventArgs e)
{
dev.stopDec((int)prop.AxisIndex);
}
private void tbtnJogOnOff_Click(object sender, EventArgs e)
{
this.stopRefresh = false;
if (prop.AxState==AxisState.STA_AX_EXT_JOG)
{
dev.closeJogMode((int)prop.AxisIndex);
//tbtnJogOnOff.Text = "开启Jog";
}
else
{
dev.setAxisVelParam(prop.VelLow, prop.VelHigh, prop.Acc, prop.Dec, (int)prop.AxisIndex);
dev.openJogMode((int)prop.AxisIndex);
//tbtnJogOnOff.Text = "关闭Jog";
}
}
private void tbtnStop_Click(object sender, EventArgs e)
{
this.stopRefresh = false;
dev.stopDec((int)prop.AxisIndex);
}
private void tbtnRestState_Click(object sender, EventArgs e)
{
this.stopRefresh = false;
dev.resetAxisState((int)prop.AxisIndex);
}
private void tbtnResetPos_Click(object sender, EventArgs e)
{
this.stopRefresh = false;
dev.resetCmdPosition((int)prop.AxisIndex);
dev.resetActualPosition((int)prop.AxisIndex);
}
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 "AxisIndex"://切换前先把old更新再加载新的prop
//先更新/删除旧的
string oldAxisIndex = ((int)oldValue).ToString();
string newAxisIndex = ((int)newValue).ToString();
if (dataReturn.ContainsKey(oldAxisIndex))
{
if (prop.Enable)
{
JObject o = JObject.Parse(prop.serialize());
o["AxisIndex"] = oldAxisIndex;//改为老的
Utils.Util.addKey(dataReturn, oldAxisIndex, o);
}
else
dataReturn.Remove(oldAxisIndex);
}
//加载新的
if (dataReturn.ContainsKey(newAxisIndex))
prop.deserialize(dataReturn[newAxisIndex].ToString());
else
{
prop.Enable = false;
refreshAxisVelParam();//更新当前速度值
}
refreshUI(true);
break;
case "Enable"://先进行更新数组,保存或删除
string axisIndex = ((int)prop.AxisIndex).ToString();
if ((bool)newValue)
Utils.Util.addKey(dataReturn, axisIndex, JObject.Parse(prop.serialize()));
else if (dataReturn.ContainsKey(axisIndex))
dataReturn.Remove(axisIndex);
break;
}
}
private void tsbtnOpenDev_Click(object sender, EventArgs e)
{
this.init();
}
//打开相机
FrmScannerShow frmScannerShow1;
FrmScannerShow frmScannerShow2;
private void tbtnScanner1_Click(object sender, EventArgs e)
{
if (devScannerGentl==null)
devScannerGentl = new ScannerDev(ScannerDev.ScannerType.GENTL);
devScannerGentl.ScanEvent += new System.Action<int, Bitmap>((num, bmp) =>
{
string path = Config.appBasePath + "\\temp\\" + DateTime.Now.Ticks + ".bmp";
bmp.Save(path, ImageFormat.Bmp);
});
if (!devScannerGentl.open())
{
devScannerGentl.stop();
return;
}
if (!devScannerGentl.start(IntPtr.Zero, null) && !devScannerGentl.IsInit)
{
devScannerGentl.close();
MessageBox.Show("相机打开失败!");
return;
}
if (frmScannerShow1 == null || frmScannerShow1.IsDisposed)
frmScannerShow1 = new FrmScannerShow(devScannerGentl.size, devScannerGentl);
frmScannerShow1.Show();
devScannerGentl.setPreviewWin(frmScannerShow1.picHwnd);
if (!devScannerGentl.isContinuousMode)
devScannerGentl.setMode(true);
}
private void tbtnScanner2_Click(object sender, EventArgs e)
{
if (devScannerCC == null)
devScannerCC = new ScannerDev(ScannerDev.ScannerType.CC);
if (!devScannerCC.open())
{
devScannerCC.stop();
return;
}
if (!devScannerCC.start(IntPtr.Zero, Config.appBasePath + "\\temp") && !devScannerCC.IsInit)
{
devScannerCC.close();
MessageBox.Show("相机打开失败!");
return;
}
if (frmScannerShow2 == null || frmScannerShow2.IsDisposed)
frmScannerShow2 = new FrmScannerShow(devScannerCC.size, devScannerCC);
frmScannerShow2.Show();
devScannerCC.setPreviewWin(frmScannerShow2.picHwnd);
if (!devScannerCC.isContinuousMode)
devScannerCC.setMode(true);
}
private void closeScannerDev()
{
if (devScannerGentl != null && devScannerGentl.IsInit)
{
devScannerGentl.stop();
devScannerGentl.close();
}
if (devScannerCC != null && devScannerCC.IsInit)
{
devScannerCC.stop();
devScannerCC.close();
}
if (frmScannerShow1 != null && !frmScannerShow1.IsDisposed) frmScannerShow1.Close();
if (frmScannerShow2 != null && !frmScannerShow2.IsDisposed) frmScannerShow2.Close();
}
private bool _stopRefresh=false;
private bool stopRefresh{
get { return _stopRefresh; }
set {
_stopRefresh = value;
this.tsbtnStopRefresh.Text = (_stopRefresh?"开启同步":"停止同步");
}
}
private void tsbtnStopRefresh_Click(object sender, EventArgs e)
{
stopRefresh = !stopRefresh;
}
}
}