UCCylinder.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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.Linq;
  9. using System.Text;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace HRT_Measure
  14. {
  15. public partial class UCCylinder : UserControl
  16. {
  17. public UCCylinder()
  18. { InitializeComponent(); }
  19. public UCCylinder(string plcName, string strInfo,Dictionary<string,ClsPLCIO> dictIO)
  20. {
  21. InitializeComponent();
  22. strPLCName = plcName;
  23. DictPLCIO = dictIO;
  24. string[] arrStr = strInfo.Split(',');
  25. CylinderName = arrStr[0];
  26. if (arrStr.Length == 2)
  27. {
  28. lightReset.Visible = false;
  29. lightSet.Visible = false;
  30. btnReset.Visible = false;
  31. string[] arrStrAdd = arrStr[1].Split('-');
  32. btnSetting.Text = arrStrAdd[0];
  33. strSetAdd = arrStrAdd[1];
  34. strLedSetAdd = arrStrAdd[2];
  35. }
  36. else if (arrStr.Length == 3)
  37. {
  38. string strreset = arrStr[1];
  39. string[] arrreset = strreset.Split('-');
  40. if(arrreset.Length == 5)
  41. {
  42. btnReset.Text = arrreset[0];
  43. strResetAdd = arrreset[1];
  44. strLedResetAdd = arrreset[2];
  45. strResetEnableAdd = arrreset[3];
  46. strSensorResetAdd = arrreset[4];
  47. }
  48. else if(arrreset.Length == 4)
  49. {
  50. btnReset.Text = arrreset[0];
  51. strResetAdd = arrreset[1];
  52. strLedResetAdd = arrreset[2];
  53. strResetEnableAdd = arrreset[3];
  54. lightReset.Visible = false;
  55. }
  56. string strset= arrStr[2];
  57. string[] arrset = strset.Split('-');
  58. if(arrset.Length == 5)
  59. {
  60. btnSetting.Text = arrset[0];
  61. strSetAdd = arrset[1];
  62. strLedSetAdd = arrset[2];
  63. strSetEnableAdd = arrset[3];
  64. strSensorSetAdd = arrset[4];
  65. }
  66. else if(arrset.Length == 4)
  67. {
  68. btnSetting.Text = arrset[0];
  69. strSetAdd = arrset[1];
  70. strLedSetAdd = arrset[2];
  71. strSetEnableAdd = arrset[3];
  72. lightSet.Visible = false;
  73. }
  74. }
  75. commPLC = GetPLC();
  76. thMonitor = new Thread(new ThreadStart(ThMonitor));
  77. thMonitor.IsBackground = true;
  78. }
  79. public Thread thMonitor ;
  80. CommPLC commPLC;
  81. private Dictionary<string, ClsPLCIO> DictPLCIO = null;
  82. public string CylinderName
  83. {
  84. set { TitleName.Text = value; }
  85. get { return TitleName.Text; }
  86. }
  87. private string strResetAdd = "";
  88. private string strSetAdd = "";
  89. private string strLedResetAdd = "";
  90. private string strLedSetAdd = "";
  91. private string strSensorResetAdd = "";
  92. private string strSensorSetAdd = "";
  93. private string strResetEnableAdd = "";
  94. private string strSetEnableAdd = "";
  95. private string strPLCName = "";
  96. #region MyRegion
  97. public string ResetAdd
  98. {
  99. get { return strResetAdd; }
  100. set { strResetAdd = value; }
  101. }
  102. public string SetAdd
  103. {
  104. get { return strSetAdd; }
  105. set { strSetAdd = value; }
  106. }
  107. public string LedResetAdd
  108. {
  109. get { return strLedResetAdd; }
  110. set { strLedResetAdd = value; }
  111. }
  112. public string LedSetAdd
  113. {
  114. get { return strLedSetAdd; }
  115. set { strLedSetAdd = value; }
  116. }
  117. public string SensorResetAdd
  118. {
  119. get { return strSensorResetAdd; }
  120. set { strSensorResetAdd = value; }
  121. }
  122. public string SensorSetAdd
  123. {
  124. get { return strSensorSetAdd; }
  125. set { strSensorSetAdd = value; }
  126. }
  127. public string ResetEnableAdd
  128. {
  129. get { return strResetEnableAdd; }
  130. set { strResetEnableAdd = value; }
  131. }
  132. public string SetEnableAdd
  133. {
  134. get { return strSetEnableAdd; }
  135. set { strSetEnableAdd = value; }
  136. }
  137. public string PLCName
  138. {
  139. get { return strPLCName; }
  140. set { strPLCName = value; }
  141. }
  142. #endregion
  143. private void UCCylinder_Load(object sender, EventArgs e)
  144. {
  145. this.Dock = DockStyle.Fill;
  146. }
  147. private CommPLC GetPLC()
  148. {
  149. if(PLCName == "M01M02") return ProgramHelp.Instance.commPLC_M01M02;
  150. if (PLCName == "M03") return ProgramHelp.Instance.commPLC_M03;
  151. if (PLCName == "M04M05") return ProgramHelp.Instance.commPLC_M04M05;
  152. return null;
  153. }
  154. private void btnReset_MouseDown(object sender, MouseEventArgs e)
  155. {
  156. try
  157. {
  158. ReadPLCHelp.SetBoolValue(ResetAdd, commPLC, true);
  159. }
  160. catch (Exception ex)
  161. {
  162. MessageBox.Show(ex.ToString());
  163. }
  164. }
  165. private void btnReset_MouseUp(object sender, MouseEventArgs e)
  166. {
  167. try
  168. {
  169. ReadPLCHelp.SetBoolValue(ResetAdd, commPLC, false);
  170. }
  171. catch (Exception ex)
  172. {
  173. MessageBox.Show(ex.ToString());
  174. }
  175. }
  176. private void btnSetting_MouseDown(object sender, MouseEventArgs e)
  177. {
  178. try
  179. {
  180. ReadPLCHelp.SetBoolValue(SetAdd, commPLC, true);
  181. }
  182. catch (Exception ex)
  183. {
  184. MessageBox.Show(ex.ToString());
  185. }
  186. }
  187. private void btnSetting_MouseUp(object sender, MouseEventArgs e)
  188. {
  189. try
  190. {
  191. ReadPLCHelp.SetBoolValue(SetAdd, commPLC, false);
  192. }
  193. catch (Exception ex)
  194. {
  195. MessageBox.Show(ex.ToString());
  196. }
  197. }
  198. private void timer1_Tick(object sender, EventArgs e)
  199. {
  200. try
  201. {
  202. if (strSensorResetAdd != "") lightReset?.BeginInvoke(new Action(() => { lightReset.State = DictPLCIO[CylinderName].isSensorResetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  203. if (strSensorSetAdd != "") lightSet?.BeginInvoke(new Action(() => { lightSet.State = DictPLCIO[CylinderName].isSensorSetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  204. if (strLedResetAdd != "") btnReset?.BeginInvoke(new Action(() => { btnReset.FillColor = DictPLCIO[CylinderName].isLedResetOn ? Color.Green : Color.Gray; }));
  205. if (strLedSetAdd != "") btnSetting?.BeginInvoke(new Action(() => { btnSetting.FillColor = DictPLCIO[CylinderName].isLedSetOn ? Color.Green : Color.Gray; }));
  206. }
  207. catch (Exception )
  208. {
  209. //MessageBox.Show(ex.ToString());
  210. }
  211. }
  212. private void UCCylinder_Leave(object sender, EventArgs e)
  213. {
  214. timer1.Enabled = false;
  215. //try
  216. //{
  217. // thMonitor.Abort();
  218. //}
  219. //catch (Exception)
  220. //{
  221. //}
  222. }
  223. private void ThMonitor()
  224. {
  225. while (true)
  226. {
  227. try
  228. {
  229. if ((PLCName == "M01M02" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.上料称重房) || (PLCName == "M03" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.压机房)
  230. || (PLCName == "M04M05" && ReadPLCHelp.EnumWindowDisplay != WindowDisplay.检测装箱房))
  231. {
  232. Thread.Sleep(10);
  233. continue;
  234. }
  235. if (strSensorResetAdd != "") lightReset?.BeginInvoke(new Action(() => { lightReset.State = DictPLCIO[CylinderName].isSensorResetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  236. if (strSensorSetAdd != "") lightSet?.BeginInvoke(new Action(() => { lightSet.State = DictPLCIO[CylinderName].isSensorSetOn ? Sunny.UI.UILightState.On : Sunny.UI.UILightState.Off; }));
  237. if (strLedResetAdd != "") btnReset?.BeginInvoke(new Action(() => { btnReset.FillColor = DictPLCIO[CylinderName].isLedResetOn ? Color.Green : Color.Gray; }));
  238. if (strLedSetAdd != "") btnSetting?.BeginInvoke(new Action(() => { btnSetting.FillColor = DictPLCIO[CylinderName].isLedSetOn ? Color.Green : Color.Gray; }));
  239. }
  240. catch (Exception )
  241. {
  242. //MessageBox.Show(ex.ToString());
  243. }
  244. Thread.Sleep(20);
  245. }
  246. }
  247. }
  248. }