using MaiMuAOI.SysCtrl; using MaiMuAOI.SysUI.DefectPicShow; using MaiMuAOI.SysUI.SysSet; using MaiMuControl.Device.AxisDev; using MaiMuControl.Device.AxisDev.Advantech; using Newtonsoft.Json; using Newtonsoft.Json.Linq; 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.Security.Cryptography; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace ProductionControl.UI { public partial class UIAxisDev : UserControl { SynchronizationContext SyncContext = null; public Action GetParamsEvent; // private AxisDevProp prop = new AxisDevProp(); private AxisDev _dev; private JObject dataReturn = new JObject(); // //ScannerDev devScannerGentl; //ScannerDev devScannerCC; public UIAxisDev(AxisDev dev) { InitializeComponent(); _dev = dev; 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); else this.propertyGrid1.Enabled=item.Enabled = !(_dev == null); } } public string getParamsData() { if (prop.Enable) { string axisIndex = ((int)prop.AxisIndex).ToString(); ConfMgr.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 void setParamsData(string json) { 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("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; refreshAxisVelParam(); refreshUIState(); } private void refreshUI(bool must=false) { 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 int GetMM2Pulse(int axisIndex) { int MM2PulseNum = 1; switch ((AxisName)axisIndex) { case AxisName.Axis0: MM2PulseNum = SysMgr.Instance.HomeParamsX1.MM2PulseNum; break; case AxisName.Axis1: MM2PulseNum = SysMgr.Instance.HomeParamsX2.MM2PulseNum; break; case AxisName.Axis2: MM2PulseNum = SysMgr.Instance.HomeParamsY.MM2PulseNum; break; case AxisName.Axis3: MM2PulseNum = SysMgr.Instance.HomeParamsZ.MM2PulseNum; break; default: MM2PulseNum = 1; break; } return MM2PulseNum; } private void refreshAxisVelParam() { int axisIndex = (int)prop.AxisIndex; //prop.HomeMode = (AxitHomeMode)Config.Axis_HomeMode[axisIndex]; AdvantechMotion tempDev = _dev as AdvantechMotion; var values = tempDev.GetAxisVelocity(axisIndex); prop.VelLow = values[0] / GetMM2Pulse(axisIndex); prop.VelHigh = values[1] / GetMM2Pulse(axisIndex); prop.Acc = values[2] / GetMM2Pulse(axisIndex); prop.Dec = values[3] / GetMM2Pulse(axisIndex); prop.ActualPos = SysMgr.Instance.AxisDev.GetFeedbackPos(axisIndex) / GetMM2Pulse(axisIndex); prop.CmdPos = SysMgr.Instance.AxisDev.GetCommandPos(axisIndex) / GetMM2Pulse(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(); if(frm != null) frm.Close(); } private void tbtnExport_Click(object sender, EventArgs e) { string filePath = ConfMgr.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 = ConfMgr.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("AxisIndex").ToString(), dataReturnTmp); } else dataReturn = dataReturnTmp; // foreach (var property in dataReturn.Properties()) { prop.deserialize(property.Value.ToString()); break; } this.propertyGrid1.Refresh(); } } 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;//改为老的 ConfMgr.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) ConfMgr.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(); } DebugTestFrm frm; private void tsbDebug_Click(object sender, EventArgs e) { if ((frm == null)||(frm.IsDisposed)) frm = new DebugTestFrm(); frm.TopMost = true; frm.Show(); } private void tbtnRun_Click(object sender, EventArgs e) { try { VelocityCurveParams vel = new VelocityCurveParams(prop.Acc, prop.Dec, prop.VelLow, prop.VelHigh,1, GetMM2Pulse((int)prop.AxisIndex)); if(prop.MoveMode == AxMoveMode.绝对位置) SysMgr.Instance.AxisDev.MoveAbsValue((int)prop.AxisIndex, vel, prop.Value ); else SysMgr.Instance.AxisDev.MoveRelValue((int)prop.AxisIndex, vel, prop.Value); //running... } catch (Exception ex) { MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void timer1_Tick(object sender, EventArgs e) { prop.ActualPos = SysMgr.Instance.AxisDev.GetFeedbackPos((int)prop.AxisIndex) / GetMM2Pulse((int)prop.AxisIndex); prop.CmdPos = SysMgr.Instance.AxisDev.GetCommandPos((int)prop.AxisIndex) / GetMM2Pulse((int)prop.AxisIndex); } } }