UCAxis.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. 
  2. using Sunny.UI;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Net.NetworkInformation;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. namespace HRT_Measure
  15. {
  16. public partial class UCAxis : UserControl
  17. {
  18. public UCAxis()
  19. {
  20. InitializeComponent();
  21. }
  22. ClsAxis clsAxis = null;
  23. public Thread thMonitor = null;
  24. public UCAxis(string plc,string axisName, string strInfo, Dictionary<string, ClsAxis> dictAxis)
  25. {
  26. InitializeComponent();
  27. strPLCName = plc;
  28. AxisName = axisName;
  29. clsAxis = dictAxis[AxisName];
  30. string[] str = strInfo.Split(',');
  31. 当前位置 = str[0];
  32. 异常代码 = str[1];
  33. 点动速度 = str[2];
  34. 寸动负 = str[3];
  35. 寸动正 = str[4];
  36. 轴使能 = str[5];
  37. 回原点 = str[6];
  38. 使能完成 = str[7];
  39. 运行允许 = str[8];
  40. 异常 = str[9];
  41. 正极限 = str[10];
  42. 原点 = str[11];
  43. 负极限 = str[12];
  44. 定位 = str[13];
  45. commPLC = GetPLC();
  46. thMonitor = new Thread(new ThreadStart(ThMonitor));
  47. thMonitor.IsBackground = true;
  48. }
  49. private string strPLCName = "";
  50. CommPLC commPLC;
  51. public string 当前位置 = "";
  52. public string 异常代码 = "";
  53. public string 点动速度 = "";
  54. public string 寸动负 = "";
  55. public string 寸动正 = "";
  56. public string 轴使能 = "";
  57. public string 回原点 = "";
  58. public string 使能完成 = "";
  59. public string 运行允许 = "";
  60. public string 异常 = "";
  61. public string 正极限 = "";
  62. public string 原点 = "";
  63. public string 负极限 = "";
  64. public string 定位 = "";
  65. public string AxisName
  66. {
  67. set { titleName.Text = value; }
  68. get { return titleName.Text; }
  69. }
  70. public string PLCName
  71. {
  72. get { return strPLCName; }
  73. set { strPLCName = value; }
  74. }
  75. private CommPLC GetPLC()
  76. {
  77. if (PLCName == "M01M02") return ProgramHelp.Instance.commPLC_M01M02;
  78. if (PLCName == "M03") return ProgramHelp.Instance.commPLC_M03;
  79. if (PLCName == "M04M05") return ProgramHelp.Instance.commPLC_M04M05;
  80. return null;
  81. }
  82. private void UCAxis_Load(object sender, EventArgs e)
  83. {
  84. this.Dock = DockStyle.Fill;
  85. }
  86. private void ThMonitor()
  87. {
  88. while (true)
  89. {
  90. try
  91. {
  92. if ((PLCName == "M01M02" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.上料称重房) || (PLCName == "M03" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.压机房)
  93. || (PLCName == "M04M05" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.检测装箱房))
  94. {
  95. Thread.Sleep(10);
  96. continue;
  97. }
  98. if (clsAxis.使能完成 != "") uiLightAxisEnableDone.BeginInvoke(new Action(() => { uiLightAxisEnableDone.State = clsAxis.is使能完成 ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  99. if (clsAxis.运行允许 != "") uiLightRunAllow.BeginInvoke(new Action(() => { uiLightRunAllow.State = clsAxis.is运行允许 ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  100. if (clsAxis.异常 != "") uiLightException.BeginInvoke(new Action(() => { uiLightException.State = clsAxis.is异常 ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  101. if (clsAxis.正极限 != "") uiLightPostive.BeginInvoke(new Action(() => { uiLightPostive.State = clsAxis.isPositiveLimitOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  102. if (clsAxis.原点 != "") uiLightOriginal.BeginInvoke(new Action(() => { uiLightOriginal.State = clsAxis.isOriginalLimitOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  103. if (clsAxis.负极限 != "") uiLightNegative.BeginInvoke(new Action(() => { uiLightNegative.State = clsAxis.isNegativeLimitOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  104. if (clsAxis.当前位置 != "") lblCurrentPos.BeginInvoke(new Action(() => { lblCurrentPos.Text = (clsAxis.i当前位置 / 1000.0).ToString(); }));
  105. if (clsAxis.异常代码 != "") lblExceptionCode.BeginInvoke(new Action(() => { lblExceptionCode.Text = clsAxis.s异常代码.ToString(); }));
  106. if (clsAxis.轴使能 != "") btnAxisEnable.BeginInvoke(new Action(() => { btnAxisEnable.FillColor = clsAxis.is轴使能 ? Color.Red : Color.Green; }));
  107. }
  108. catch (Exception ) { }
  109. Thread.Sleep(10);
  110. }
  111. }
  112. private void btnJogNegative_MouseDown(object sender, MouseEventArgs e)
  113. {
  114. try
  115. {
  116. commPLC.WriteInt(点动速度, Convert.ToInt32(txtSpeed.Text), out string str);
  117. commPLC.WriteCoin(寸动负, true, out str);
  118. }
  119. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  120. }
  121. private void btnJogNegative_MouseUp(object sender, MouseEventArgs e)
  122. {
  123. try
  124. {
  125. commPLC.WriteCoin(寸动负, false, out string str);
  126. }
  127. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  128. }
  129. private void btnJogPositive_MouseDown(object sender, MouseEventArgs e)
  130. {
  131. try
  132. {
  133. commPLC.WriteInt(点动速度, Convert.ToInt32(txtSpeed.Text), out string str);
  134. commPLC.WriteCoin(寸动正, true, out str);
  135. }
  136. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  137. }
  138. private void btnJogPositive_MouseUp(object sender, MouseEventArgs e)
  139. {
  140. try
  141. {
  142. commPLC.WriteCoin(寸动正, false, out string str);
  143. }
  144. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  145. }
  146. private void btnAxisEnable_Click(object sender, EventArgs e)
  147. {
  148. try
  149. {
  150. //commPLC.ReadCoin(轴使能, out bool result, out string str);
  151. commPLC.WriteCoin(轴使能, !clsAxis.is轴使能, out string str);
  152. }
  153. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  154. }
  155. private void btnAxisHome_MouseDown(object sender, MouseEventArgs e)
  156. {
  157. try
  158. {
  159. commPLC.WriteCoin(回原点, true, out string str);
  160. }
  161. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  162. }
  163. private void btnAxisHome_MouseUp(object sender, MouseEventArgs e)
  164. {
  165. try
  166. {
  167. commPLC.WriteCoin(回原点, false, out string str);
  168. }
  169. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  170. }
  171. private void timer1_Tick(object sender, EventArgs e)
  172. {
  173. try
  174. {
  175. if (clsAxis.使能完成 != "") uiLightAxisEnableDone.BeginInvoke(new Action(() => { uiLightAxisEnableDone.State = clsAxis.is使能完成 ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  176. if (clsAxis.运行允许 != "") uiLightRunAllow.BeginInvoke(new Action(() => { uiLightRunAllow.State = clsAxis.is运行允许 ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  177. if (clsAxis.异常 != "") uiLightException.BeginInvoke(new Action(() => { uiLightException.State = clsAxis.is异常 ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  178. if (clsAxis.正极限 != "") uiLightPostive.BeginInvoke(new Action(() => { uiLightPostive.State = clsAxis.isPositiveLimitOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  179. if (clsAxis.原点 != "") uiLightOriginal.BeginInvoke(new Action(() => { uiLightOriginal.State = clsAxis.isOriginalLimitOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  180. if (clsAxis.负极限 != "") uiLightNegative.BeginInvoke(new Action(() => { uiLightNegative.State = clsAxis.isNegativeLimitOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  181. //if (AxisName == "取料X轴")
  182. //{
  183. if (clsAxis.当前位置 != "") lblCurrentPos.BeginInvoke(new Action(() => { lblCurrentPos.Text = (clsAxis.i当前位置 / 1000.0).ToString(); }));
  184. //}
  185. if (clsAxis.异常代码 != "") lblExceptionCode.BeginInvoke(new Action(() => { lblExceptionCode.Text = clsAxis.s异常代码.ToString(); }));
  186. if (clsAxis.轴使能 != "") btnAxisEnable.BeginInvoke(new Action(() => { btnAxisEnable.FillColor = clsAxis.is轴使能 ? Color.Red : Color.Green; }));
  187. }
  188. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  189. }
  190. private void UCAxis_Leave(object sender, EventArgs e)
  191. {
  192. timer1.Enabled = false;
  193. }
  194. private void btnLocation_MouseDown(object sender, MouseEventArgs e)
  195. {
  196. try
  197. {
  198. commPLC.WriteCoin(定位, true, out string str);
  199. }
  200. catch (Exception)
  201. {
  202. }
  203. }
  204. private void btnLocation_MouseUp(object sender, MouseEventArgs e)
  205. {
  206. try
  207. {
  208. commPLC.WriteCoin(定位, false, out string str);
  209. }
  210. catch (Exception)
  211. {
  212. }
  213. }
  214. }
  215. }