版博士V2.0程序
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

TensionDev.cs 7.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. using ProductionControl.Utils;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using System.Timers;
  9. namespace ProductionControl.Device
  10. {
  11. public class TensionDev : IDisposable
  12. {
  13. public double TensionValue { private set; get; }
  14. public string Unit { get; private set; }
  15. public Action<WarningEnum, string> WarningEvent;
  16. /// <summary>
  17. /// 值<值,单位>
  18. /// </summary>
  19. public Action<double,string> TensionEvent;
  20. /// <summary>
  21. /// 是否打开设备成功
  22. /// </summary>
  23. public bool IsInit { get; private set; } = false;
  24. private System.Timers.Timer timer = new System.Timers.Timer();
  25. private SSerialPort sSerialPort;
  26. private bool _isDebug=false;
  27. public TensionDev(bool isDebug)
  28. {
  29. _isDebug= isDebug;
  30. }
  31. public TensionDev()
  32. {
  33. }
  34. public bool start(int comNum)
  35. {
  36. try
  37. {
  38. sSerialPort = new SSerialPort("COM" + comNum, 2400, 1);
  39. IsInit = true;
  40. if (_isDebug)
  41. {
  42. timer.Elapsed += Timer_Elapsed;
  43. timer.Interval = 300;
  44. timer.Start();
  45. }
  46. return true;
  47. }
  48. catch (Exception ex)
  49. {
  50. WarningEvent?.Invoke(WarningEnum.High, ex.Message);
  51. try { sSerialPort.close(); } catch { }
  52. return false;
  53. }
  54. }
  55. public void stop()
  56. {
  57. if (!IsInit) return;
  58. IsInit = false;
  59. if (_isDebug)
  60. {
  61. timer.Elapsed -= Timer_Elapsed;
  62. timer.Stop();
  63. }
  64. sSerialPort.close();
  65. }
  66. private bool reading=false;
  67. private void Timer_Elapsed(object sender, ElapsedEventArgs e)
  68. {
  69. if (!IsInit || reading) return;
  70. API.OutputDebugString($"Timer_Elapsed:{reading}");
  71. reading = true;
  72. getValue();
  73. reading = false;
  74. }
  75. private readonly byte[] HEAD = { 0x10,0x08,0x19 };
  76. private readonly byte[] EOT = { 0x0D, 0x0A };
  77. public double getValue()
  78. {
  79. try
  80. {
  81. if (!IsInit) return -1;
  82. //接收
  83. int readTimeout = 3000;
  84. byte[] recv, data=null;
  85. IEnumerable<long> indexList;
  86. //找HEAD
  87. do
  88. {
  89. recv = sSerialPort.getMessage(readTimeout);
  90. if (recv == null || recv.Length < 1)
  91. throw new Exception("无数据或接收超时(HEAD)!");
  92. API.OutputDebugString($"RECV HEAD:{HexUtil.toHexString(recv)}");
  93. //合并后的
  94. if (data == null || data.Length < 1)
  95. data = recv;
  96. else
  97. data = HexUtil.joinByteList(new List<byte[]> { data, recv });
  98. indexList = Utils.HexUtil.IndexOf(data, 0, HEAD);
  99. } while (indexList.Count() == 0);
  100. data = HexUtil.subBytes(data, (int)indexList.Last());
  101. API.OutputDebugString($"HEAD:{HexUtil.toHexString(data)}");
  102. //找EOT
  103. //indexList = Utils.HexUtil.IndexOf(data, 0, EOT);
  104. //while (indexList.Count() == 0)
  105. //{
  106. // recv = comPortJDJ.getMessage(readTimeout);
  107. // if (recv == null || recv.Length < 1)
  108. // throw new Exception("无数据或接收超时(EOT)!");
  109. // //AddTextEvent("焦度计", $"读取数据:{HexUtil.toHexString(recv)}");
  110. // //合并后的
  111. // data = HexUtil.joinByteList(new List<byte[]> { data, recv });
  112. // indexList = Utils.HexUtil.IndexOf(data, 0, JDJ_EOT);
  113. //}
  114. //data = HexUtil.subBytes(data, 0, (int)indexList.First() + JDJ_EOT.Length);
  115. // byte长度 总报文11byte-3head=8
  116. while (data.Length<11)
  117. {
  118. recv = sSerialPort.getMessage(readTimeout);
  119. if (recv == null || recv.Length < 1)
  120. throw new Exception("无数据或接收超时(EOT)!");
  121. API.OutputDebugString($"RECV EOT:{HexUtil.toHexString(recv)}");
  122. //合并后的
  123. data = HexUtil.joinByteList(new List<byte[]> { data, recv });
  124. }
  125. data = HexUtil.subBytes(data, 0, 11);
  126. API.OutputDebugString($"RECV DATA:{HexUtil.toHexString(data)}");
  127. var buff = data;
  128. //if (buff.Length % 11 != 0)
  129. // throw new Exception("Tension数据错误!");
  130. //if (buff.Length > 11)
  131. // buff = subBuff(buff, buff.Length - 11, 11);
  132. //if (buff[0] != 0x10)
  133. // throw new Exception("Tension数据0错误!");
  134. int add = 0;
  135. for (int i = 1; i < 10; i++)
  136. add += (int)buff[i];
  137. if (add != buff[10])
  138. throw new Exception("Tension数据ADD错误!");
  139. string unit = buff[3] == 0x10 ? "N/cm" : "Kgf/cm";
  140. string szData = "";
  141. int zf = 1;
  142. for (int i = 4; i <= 8; i++)
  143. {
  144. if (buff[i] == 0x0a) continue;
  145. if (buff[i] == 0x0b) zf = -1;
  146. if (buff[i] < 0x0a)
  147. szData += (int)buff[i];
  148. }
  149. double ldNum = Convert.ToDouble(szData) * zf;
  150. if (buff[9] > 0) ldNum /= Math.Pow(10, buff[9]);
  151. //
  152. //int add = 0;
  153. //for (int i = 1; i < HEAD.Length; i++)
  154. // add += (int)HEAD[i];
  155. //for (int i = 0; i < buff.Length-1; i++)
  156. // add += (int)buff[i];
  157. //if (add != buff[buff.Length - 1])
  158. // throw new Exception("Tension数据ADD错误!");
  159. //string unit = buff[0] == 0x10 ? "N/cm" : "Kgf/cm";
  160. //string szData = "";
  161. //int zf = 1;
  162. //for (int i = 1; i <= 5; i++)
  163. //{
  164. // if (buff[i] == 0x0a) continue;
  165. // if (buff[i] == 0x0b) zf = -1;
  166. // if (buff[i] < 0x0a)
  167. // szData += (int)buff[i];
  168. //}
  169. //double ldNum = Convert.ToDouble(szData) * zf;
  170. //if (buff[6] > 0) ldNum /= Math.Pow(10, buff[6]);
  171. //
  172. TensionValue = ldNum;
  173. Unit = unit;
  174. TensionEvent?.Invoke(ldNum, unit);
  175. return ldNum;
  176. }
  177. catch (Exception ex)
  178. {
  179. API.OutputDebugString($"ERR:{ex.Message}");
  180. WarningEvent?.Invoke(WarningEnum.Low,ex.Message);
  181. return -1;
  182. }
  183. }
  184. private byte[] subBuff(byte[] buff, int start, int length)
  185. {
  186. byte[] res = new byte[length];
  187. for (int i = start; i < buff.Length && i < start + length; i++)
  188. res[i - start] = buff[i];
  189. return res;
  190. }
  191. public void Dispose()
  192. {
  193. stop();
  194. }
  195. }
  196. }