版博士V2.0程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 

314 righe
12 KiB

  1. using MaiMuAOI.SysCtrl;
  2. using MaiMuAOI.SysUI.DefectPicShow;
  3. using MaiMuAOI.SysUI.SysSet;
  4. using MaiMuControl.Device.AxisDev;
  5. using MaiMuControl.Device.AxisDev.Advantech;
  6. using Newtonsoft.Json;
  7. using Newtonsoft.Json.Linq;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.ComponentModel;
  11. using System.Data;
  12. using System.Drawing;
  13. using System.Drawing.Imaging;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Security.Cryptography;
  17. using System.Text;
  18. using System.Threading;
  19. using System.Threading.Tasks;
  20. using System.Windows.Forms;
  21. using System.Xml.Linq;
  22. namespace ProductionControl.UI
  23. {
  24. public partial class UIAxisDev : UserControl
  25. {
  26. SynchronizationContext SyncContext = null;
  27. public Action<string> GetParamsEvent;
  28. //
  29. private AxisDevProp prop = new AxisDevProp();
  30. private AxisDev _dev;
  31. private JObject dataReturn = new JObject();
  32. //
  33. //ScannerDev devScannerGentl;
  34. //ScannerDev devScannerCC;
  35. public UIAxisDev(AxisDev dev)
  36. {
  37. InitializeComponent();
  38. _dev = dev;
  39. propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged;
  40. refreshUIState();
  41. //获取UI线程同步上下文
  42. SyncContext = SynchronizationContext.Current;
  43. //init();
  44. }
  45. private void refreshUIState()
  46. {
  47. foreach (ToolStripItem item in this.toolStrip1.Items)
  48. {
  49. if (item.Text == "打开设备")
  50. item.Visible = (_dev == null);
  51. else
  52. this.propertyGrid1.Enabled=item.Enabled = !(_dev == null);
  53. }
  54. }
  55. public string getParamsData()
  56. {
  57. if (prop.Enable)
  58. {
  59. string axisIndex = ((int)prop.AxisIndex).ToString();
  60. ConfMgr.addKey(dataReturn, axisIndex, JObject.Parse(prop.serialize()));
  61. }
  62. //return prop.serialize();
  63. return dataReturn.ToString();
  64. }
  65. private void tbtnSave_Click(object sender, EventArgs e)
  66. {
  67. if (dataReturn.Properties().Count() < 1)
  68. {
  69. MessageBox.Show("请设置需要启用的轴!", "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  70. return;
  71. }
  72. //GetParamsEvent?.Invoke(prop.serialize());
  73. GetParamsEvent?.Invoke(getParamsData());
  74. }
  75. public void setParamsData(string json)
  76. {
  77. if (json == "") return;
  78. JObject dataReturnTmp = JObject.Parse(json);
  79. //兼容旧版单轴
  80. if (!dataReturnTmp.ContainsKey("0") && !dataReturnTmp.ContainsKey("1") && !dataReturnTmp.ContainsKey("2") && !dataReturnTmp.ContainsKey("3"))
  81. {
  82. if(!dataReturnTmp.ContainsKey("Enable"))
  83. dataReturnTmp.Add("Enable",true);
  84. dataReturn.Add(dataReturnTmp.Value<int>("AxisIndex").ToString(), dataReturnTmp);
  85. }
  86. else
  87. dataReturn = dataReturnTmp;
  88. //
  89. foreach (var property in dataReturn.Properties())
  90. {
  91. prop.deserialize(property.Value.ToString());
  92. break;
  93. }
  94. this.propertyGrid1.Refresh();
  95. }
  96. public void init()
  97. {
  98. this.tbtnSave.Visible = tssSave.Visible = (GetParamsEvent != null);
  99. this.propertyGrid1.SelectedObject = prop;
  100. refreshAxisVelParam();
  101. refreshUIState();
  102. }
  103. private void refreshUI(bool must=false)
  104. {
  105. SyncContext.Post(m =>
  106. {
  107. var result = m as string;
  108. propertyGrid1.Refresh();
  109. //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog";
  110. //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG);
  111. }, "异步操作完成结果");
  112. }
  113. private int GetMM2Pulse(int axisIndex)
  114. {
  115. int MM2PulseNum = 1;
  116. switch ((AxisName)axisIndex)
  117. {
  118. case AxisName.Axis0:
  119. MM2PulseNum = SysMgr.Instance.HomeParamsX1.MM2PulseNum;
  120. break;
  121. case AxisName.Axis1:
  122. MM2PulseNum = SysMgr.Instance.HomeParamsX2.MM2PulseNum;
  123. break;
  124. case AxisName.Axis2:
  125. MM2PulseNum = SysMgr.Instance.HomeParamsY.MM2PulseNum;
  126. break;
  127. case AxisName.Axis3:
  128. MM2PulseNum = SysMgr.Instance.HomeParamsZ.MM2PulseNum;
  129. break;
  130. default:
  131. MM2PulseNum = 1;
  132. break;
  133. }
  134. return MM2PulseNum;
  135. }
  136. private void refreshAxisVelParam()
  137. {
  138. int axisIndex = (int)prop.AxisIndex;
  139. //prop.HomeMode = (AxitHomeMode)Config.Axis_HomeMode[axisIndex];
  140. AdvantechMotion tempDev = _dev as AdvantechMotion;
  141. var values = tempDev.GetAxisVelocity(axisIndex);
  142. prop.VelLow = values[0] / GetMM2Pulse(axisIndex);
  143. prop.VelHigh = values[1] / GetMM2Pulse(axisIndex);
  144. prop.Acc = values[2] / GetMM2Pulse(axisIndex);
  145. prop.Dec = values[3] / GetMM2Pulse(axisIndex);
  146. prop.ActualPos = SysMgr.Instance.AxisDev.GetFeedbackPos(axisIndex) / GetMM2Pulse(axisIndex);
  147. prop.CmdPos = SysMgr.Instance.AxisDev.GetCommandPos(axisIndex) / GetMM2Pulse(axisIndex);
  148. }
  149. private void closeDev()
  150. {
  151. //try
  152. //{
  153. // if (dev == null)
  154. // return;
  155. // dev.stop();
  156. // closeScannerDev();
  157. //}
  158. //catch (Exception ex)
  159. //{
  160. // this.refreshUIState();
  161. // MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  162. //}
  163. }
  164. protected override void OnHandleDestroyed(EventArgs e)
  165. {
  166. base.OnHandleDestroyed(e);
  167. // 在此添加需要手动释放资源的代码
  168. this.closeDev();
  169. if(frm != null)
  170. frm.Close();
  171. }
  172. private void tbtnExport_Click(object sender, EventArgs e)
  173. {
  174. string filePath = ConfMgr.SaveAsFile($"Axis配置.json", "JSON|*.json");
  175. if (filePath != "")
  176. {
  177. //string jsonText = prop.serialize();
  178. string jsonText = dataReturn.ToString();
  179. File.WriteAllText(filePath, jsonText);
  180. MessageBox.Show("保存成功!", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  181. }
  182. }
  183. private void tbtnImport_Click(object sender, EventArgs e)
  184. {
  185. string filePath = ConfMgr.SelectFile("JSON|*.json");
  186. if (filePath != "")
  187. {
  188. string jsonText = File.ReadAllText(filePath);
  189. JObject dataReturnTmp = JObject.Parse(jsonText);
  190. //兼容旧版单轴
  191. if (!dataReturnTmp.ContainsKey("0") && !dataReturnTmp.ContainsKey("1") && !dataReturnTmp.ContainsKey("2") && !dataReturnTmp.ContainsKey("3"))
  192. {
  193. dataReturnTmp.Add("Enable", true);
  194. dataReturn.Add(dataReturnTmp.Value<int>("AxisIndex").ToString(), dataReturnTmp);
  195. }
  196. else
  197. dataReturn = dataReturnTmp;
  198. //
  199. foreach (var property in dataReturn.Properties())
  200. {
  201. prop.deserialize(property.Value.ToString());
  202. break;
  203. }
  204. this.propertyGrid1.Refresh();
  205. }
  206. }
  207. private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  208. {
  209. //其中包含了两个重要的属性:OldValue和ChangeItem。
  210. //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。
  211. //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。
  212. string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name;
  213. string propertyName = e.ChangedItem.PropertyDescriptor.Name;
  214. var oldValue = e.OldValue;
  215. var newValue = e.ChangedItem.Value;
  216. switch (propertyName)
  217. {
  218. case "AxisIndex"://切换前先把old更新,再加载新的prop
  219. //先更新/删除旧的
  220. string oldAxisIndex = ((int)oldValue).ToString();
  221. string newAxisIndex = ((int)newValue).ToString();
  222. if (dataReturn.ContainsKey(oldAxisIndex))
  223. {
  224. if (prop.Enable)
  225. {
  226. JObject o = JObject.Parse(prop.serialize());
  227. o["AxisIndex"] = oldAxisIndex;//改为老的
  228. ConfMgr.addKey(dataReturn, oldAxisIndex, o);
  229. }
  230. else
  231. dataReturn.Remove(oldAxisIndex);
  232. }
  233. //加载新的
  234. if (dataReturn.ContainsKey(newAxisIndex))
  235. prop.deserialize(dataReturn[newAxisIndex].ToString());
  236. else
  237. {
  238. prop.Enable = false;
  239. refreshAxisVelParam();//更新当前速度值
  240. }
  241. refreshUI(true);
  242. break;
  243. case "Enable"://先进行更新数组,保存或删除
  244. string axisIndex = ((int)prop.AxisIndex).ToString();
  245. if ((bool)newValue)
  246. ConfMgr.addKey(dataReturn, axisIndex, JObject.Parse(prop.serialize()));
  247. else if (dataReturn.ContainsKey(axisIndex))
  248. dataReturn.Remove(axisIndex);
  249. break;
  250. }
  251. }
  252. private void tsbtnOpenDev_Click(object sender, EventArgs e)
  253. {
  254. this.init();
  255. }
  256. DebugTestFrm frm;
  257. private void tsbDebug_Click(object sender, EventArgs e)
  258. {
  259. if ((frm == null)||(frm.IsDisposed))
  260. frm = new DebugTestFrm();
  261. frm.TopMost = true;
  262. frm.Show();
  263. }
  264. private void tbtnRun_Click(object sender, EventArgs e)
  265. {
  266. try
  267. {
  268. VelocityCurveParams vel = new VelocityCurveParams(prop.Acc, prop.Dec, prop.VelLow, prop.VelHigh,1, GetMM2Pulse((int)prop.AxisIndex));
  269. if(prop.MoveMode == AxMoveMode.绝对位置)
  270. SysMgr.Instance.AxisDev.MoveAbsValue((int)prop.AxisIndex, vel, prop.Value );
  271. else
  272. SysMgr.Instance.AxisDev.MoveRelValue((int)prop.AxisIndex, vel, prop.Value);
  273. //running...
  274. }
  275. catch (Exception ex)
  276. {
  277. MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
  278. }
  279. }
  280. private void timer1_Tick(object sender, EventArgs e)
  281. {
  282. prop.ActualPos = SysMgr.Instance.AxisDev.GetFeedbackPos((int)prop.AxisIndex) / GetMM2Pulse((int)prop.AxisIndex);
  283. prop.CmdPos = SysMgr.Instance.AxisDev.GetCommandPos((int)prop.AxisIndex) / GetMM2Pulse((int)prop.AxisIndex);
  284. }
  285. }
  286. }