MoxaCls.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace CarLocalMeter
  11. {
  12. /// <summary>
  13. /// Moxa重量采集及判稳
  14. /// </summary>
  15. public class MoxaCls
  16. {
  17. Socket client = null;
  18. IPAddress ip = null;
  19. IPEndPoint point = null;
  20. bool blThreadFlag;//数据采集线程开关
  21. Thread DataCollectThread = null;//采集进程
  22. public MoxaCls()
  23. {
  24. //新数据采集
  25. //获取IP地址和端口并创建IPEndPoint
  26. ip = IPAddress.Parse(AppConfigCache.moxaIP);
  27. point = new IPEndPoint(ip, AppConfigCache.moxaPort);
  28. blThreadFlag = true;
  29. DataCollectThread = new Thread(new ThreadStart(DataCollect));
  30. }
  31. public void start()
  32. {
  33. if (client == null)
  34. {
  35. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  36. //连接到服务器
  37. client.Connect(point);
  38. }
  39. //连接到服务器
  40. else if (!client.Connected)
  41. {
  42. client.Close();
  43. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  44. client.Connect(point);
  45. }
  46. DataCollectThread.Start();
  47. }
  48. public void ClosingCollect()
  49. {
  50. blThreadFlag = false;
  51. }
  52. #region 数据采集
  53. private List<int> lWeight = new List<int>();
  54. /// <summary>
  55. /// 数据采集线程
  56. /// </summary>
  57. private void DataCollect()
  58. {
  59. if (!blThreadFlag) return;
  60. //连接到服务器
  61. while (!client.Connected)
  62. {
  63. try
  64. {
  65. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  66. client.Connect(point);
  67. }
  68. catch (Exception err)
  69. {
  70. CacleCls.weightFlag = "1";
  71. CacleCls.weightMsgInfo = "Moxa连接异常";
  72. //WriteLog($"连接Moxa失败,正重试{err.Message}");
  73. }
  74. Thread.Sleep(500);
  75. }
  76. int isLd = 0;
  77. while (blThreadFlag)
  78. {
  79. try
  80. {
  81. byte[] buffer = new byte[32 * 32];
  82. int n = client.Receive(buffer);
  83. if (AppConfigCache.isFz == "true")
  84. {
  85. byte[] bf = buffer.Reverse().ToArray();
  86. int mx = 0;
  87. foreach (byte b in bf)
  88. {
  89. if (b > 0) buffer[mx++] = b;
  90. }
  91. }
  92. #region 在这里要数一下buffer里面的数据,看下结束符的byte值是多少,每个秤都确定好之后,就删除这个即可
  93. int m = 0;
  94. string strX = "[";
  95. for (int i = 0; i < buffer.Length; i++)
  96. {
  97. if (buffer[i] > 0)
  98. {
  99. m++;
  100. strX += $"{buffer[i]} ";
  101. }
  102. }
  103. strX += "][";
  104. strX += Encoding.UTF8.GetString(buffer, 0, m);
  105. strX += "]";
  106. //string strx = Encoding.UTF8.GetString(buffer, 0, m);
  107. //在这里要数一下buffer里面的数据,看下结束符的byte值是多少
  108. //textBox1.Invoke(new Action(() => {
  109. // textBox1.Text += strX;
  110. //}));
  111. #endregion
  112. byte[] bt = new byte[AppConfigCache.messageLength];
  113. int iLast = 0, iCnt = 0;
  114. for (int i = n - 1; i > -1; i--)
  115. {
  116. //byte字节里面都是数值 0-15 0-12
  117. if (iLast == 0 && buffer[i] == AppConfigCache.separate)//endStr
  118. {
  119. iLast = i;
  120. bt[iCnt] = buffer[i];
  121. }
  122. else if (iLast != 0)
  123. {
  124. if (iCnt < AppConfigCache.messageLength - 1)
  125. {
  126. bt[++iCnt] = buffer[i];
  127. }
  128. else
  129. {
  130. break;
  131. }
  132. }
  133. }
  134. Array.Reverse(bt);
  135. if (iCnt + 1 != AppConfigCache.messageLength) continue;
  136. byte[] btDb = new byte[AppConfigCache.dataLength];
  137. int iC = 0;
  138. for (int i = AppConfigCache.startPosition; i < (AppConfigCache.startPosition + AppConfigCache.dataLength); i++)
  139. {
  140. btDb[iC++] = bt[i - 1];
  141. }
  142. string str = Encoding.UTF8.GetString(btDb, 0, btDb.Length);
  143. int weight = Convert.ToInt32(str);
  144. CacleCls.weight = weight;
  145. CacleCls.weightFlag = "0";
  146. CacleCls.weightMsgInfo = "";
  147. if (CacleCls.weight <= AppConfigCache.undulateValue)
  148. {
  149. CacleCls.isWd = 2;
  150. lWeight.Clear();
  151. }
  152. else
  153. {
  154. if (lWeight.Count == 0)
  155. {
  156. lWeight.Add(weight);
  157. CacleCls.isWd = 0;
  158. }
  159. else if (Math.Abs(weight - lWeight[0]) > AppConfigCache.undulateValue)
  160. {
  161. lWeight.Clear();
  162. lWeight.Add(weight);
  163. CacleCls.isWd = 1;
  164. }
  165. else if (lWeight.Count < AppConfigCache.undulateCount)
  166. {
  167. lWeight.Add(weight);
  168. }
  169. else
  170. {
  171. CacleCls.isWd = 0;
  172. }
  173. }
  174. if (Math.Abs(weight) > 0)
  175. {
  176. if (Math.Abs(weight) <= AppConfigCache.minValue)
  177. {
  178. if (isLd < 4) isLd++; //连续4次采集到不为0,且满足零点最小绝对值
  179. if (isLd == 4) CacleCls.isZeroState = true;
  180. }
  181. }
  182. else
  183. {
  184. CacleCls.isZeroState = false;
  185. isLd = 0;
  186. }
  187. }
  188. catch (Exception ex)
  189. {
  190. CacleCls.isWd = CacleCls.isWd == 0 ? 1 : CacleCls.isWd;
  191. CacleCls.weightFlag = "1";
  192. CacleCls.weightMsgInfo = "Moxa采集异常:" + ex.Message;
  193. }
  194. finally
  195. {
  196. Thread.Sleep(AppConfigCache.sleepTime);
  197. }
  198. }
  199. }
  200. #endregion
  201. }
  202. }