UCAxisPos.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. 
  2. using Newtonsoft.Json.Linq;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Drawing.Text;
  9. using System.Linq;
  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 UCAxisPos : UserControl
  17. {
  18. public UCAxisPos()
  19. {
  20. InitializeComponent();
  21. }
  22. ClsAxisPos clsAxisPos = null;
  23. public Thread thMonitor = null;
  24. public UCAxisPos(string strInfo, Dictionary<string, ClsAxisPos> dictAxisPos)
  25. {
  26. InitializeComponent();
  27. // DictAxisPos = dictAxisPos;
  28. string [] str = strInfo.Split(',');
  29. PosName = str[0];
  30. clsAxisPos = dictAxisPos[PosName];
  31. strPLCName = clsAxisPos.PLCName;
  32. strAxisName = clsAxisPos.AxisName;
  33. strAddGo = clsAxisPos.GoAdd;
  34. strAddPos = clsAxisPos.PosAdd;
  35. strAddSpeed = clsAxisPos.SpeedAdd;
  36. strAddLight = clsAxisPos.LightAdd;
  37. commPLC = GetPLC();
  38. btnPosGo.Text = PosName;
  39. thMonitor = new Thread(new ThreadStart(ThMonitor));
  40. thMonitor.IsBackground = true;
  41. }
  42. private string strPLCName = "";
  43. private string strAxisName = "";
  44. CommPLC commPLC;
  45. private string strAddGo = "";
  46. private string strAddPos = "";
  47. private string strAddSpeed = "";
  48. private string strAddLight = "";
  49. #region MyRegion
  50. public string PosName = "";
  51. //{
  52. // set { titleName.Text = value; }
  53. // get { return titleName.Text; }
  54. //}
  55. public string PLCName
  56. {
  57. get { return strPLCName; }
  58. set { strPLCName = value; }
  59. }
  60. public string AxisName
  61. {
  62. get { return strAxisName; }
  63. set { strAxisName = value; }
  64. }
  65. public string AddGo
  66. {
  67. get { return strAddGo; }
  68. set { strAddGo = value; }
  69. }
  70. public string AddPos
  71. {
  72. get { return strAddPos; }
  73. set { strAddPos = value; }
  74. }
  75. public string AddSpeed
  76. {
  77. get { return strAddSpeed; }
  78. set { strAddSpeed = value; }
  79. }
  80. public string AddLight
  81. {
  82. get { return strAddLight; }
  83. set { strAddLight = value; }
  84. }
  85. #endregion
  86. private void UCM01M02取料X轴点位_Load(object sender, EventArgs e)
  87. {
  88. this.Dock = DockStyle.Fill;
  89. txtPos.Text = clsAxisPos.ReadPara(AxisName, $"{clsAxisPos.PosName}位置");
  90. txtSpeed.Text = clsAxisPos.ReadPara(AxisName, $"{clsAxisPos.PosName}速度");
  91. }
  92. private void ThMonitor()
  93. {
  94. while(true)
  95. {
  96. try
  97. {
  98. if ((PLCName == "M01M02" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.上料称重房) || (PLCName == "M03" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.压机房)
  99. || (PLCName == "M04M05" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.检测装箱房))
  100. {
  101. Thread.Sleep(10);
  102. continue;
  103. }
  104. if (clsAxisPos.LightAdd != "") uiLight.BeginInvoke(new Action(() => { uiLight.State = clsAxisPos.isLightOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  105. btnPosGo.BeginInvoke(new Action(() => { btnPosGo.FillColor = ((clsAxisPos.GoCurrValue != clsAxisPos.uGoValue) ? Color.Gray : Color.Green); }));
  106. }
  107. catch (Exception ) { }
  108. Thread.Sleep(10);
  109. }
  110. }
  111. private CommPLC GetPLC()
  112. {
  113. if (PLCName == "M01M02") return ProgramHelp.Instance.commPLC_M01M02;
  114. if (PLCName == "M03") return ProgramHelp.Instance.commPLC_M03;
  115. if (PLCName == "M04M05") return ProgramHelp.Instance.commPLC_M04M05;
  116. return null;
  117. }
  118. private void btnPosGo_Click(object sender, EventArgs e)
  119. {
  120. try
  121. {
  122. float value = Convert.ToSingle(txtPos.Text);
  123. //commPLC.WriteInt(clsAxisPos.PosAdd,Convert.ToInt32(value * 1000), out string str);
  124. //commPLC.WriteInt(clsAxisPos.SpeedAdd, Convert.ToInt32(txtSpeed.Text), out str);
  125. commPLC.WriteUShort(clsAxisPos.GoAdd, clsAxisPos.uGoValue, out string str);
  126. }
  127. catch (Exception ex)
  128. {
  129. MessageBox.Show(ex.ToString());
  130. }
  131. }
  132. private void btnWrite_Click(object sender, EventArgs e)
  133. {
  134. try
  135. {
  136. float value = Convert.ToSingle(txtPos.Text);
  137. commPLC.WriteInt(clsAxisPos.PosAdd,Convert.ToInt32(value * 1000),out string str);
  138. commPLC.WriteInt(clsAxisPos.SpeedAdd, Convert.ToInt32(txtSpeed.Text), out str);
  139. clsAxisPos.WritePara($"{clsAxisPos.PosName}位置", AxisName, txtPos.Text.Trim());
  140. clsAxisPos.WritePara($"{clsAxisPos.PosName}速度", AxisName, txtSpeed.Text.Trim());
  141. }
  142. catch (Exception ex)
  143. {
  144. MessageBox.Show(ex.ToString());
  145. }
  146. }
  147. private void timer1_Tick(object sender, EventArgs e)
  148. {
  149. try
  150. {
  151. if(clsAxisPos.LightAdd!= "") uiLight.BeginInvoke(new Action(() => { uiLight.State = clsAxisPos.isLightOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  152. btnPosGo.BeginInvoke(new Action(() => { btnPosGo.FillColor = ((clsAxisPos.GoCurrValue != clsAxisPos.uGoValue) ? Color.Gray : Color.Green); }));
  153. }
  154. catch (Exception ex) { MessageBox.Show(ex.ToString()); }
  155. }
  156. private void UCAxisPos_Leave(object sender, EventArgs e)
  157. {
  158. timer1.Enabled = false;
  159. }
  160. }
  161. }