MoxaCls.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. while (blThreadFlag)
  77. {
  78. try
  79. {
  80. byte[] buffer = new byte[32 * 32];
  81. int n = client.Receive(buffer);
  82. if (AppConfigCache.isFz == "true")
  83. {
  84. byte[] bf = buffer.Reverse().ToArray();
  85. int mx = 0;
  86. foreach (byte b in bf)
  87. {
  88. if (b > 0) buffer[mx++] = b;
  89. }
  90. }
  91. #region 在这里要数一下buffer里面的数据,看下结束符的byte值是多少,每个秤都确定好之后,就删除这个即可
  92. int m = 0;
  93. string strX = "[";
  94. for (int i = 0; i < buffer.Length; i++)
  95. {
  96. if (buffer[i] > 0)
  97. {
  98. m++;
  99. strX += $"{buffer[i]} ";
  100. }
  101. }
  102. strX += "][";
  103. strX += Encoding.UTF8.GetString(buffer, 0, m);
  104. strX += "]";
  105. //string strx = Encoding.UTF8.GetString(buffer, 0, m);
  106. //在这里要数一下buffer里面的数据,看下结束符的byte值是多少
  107. //textBox1.Invoke(new Action(() => {
  108. // textBox1.Text += strX;
  109. //}));
  110. #endregion
  111. byte[] bt = new byte[AppConfigCache.messageLength];
  112. int iLast = 0, iCnt = 0;
  113. for (int i = n - 1; i > -1; i--)
  114. {
  115. //byte字节里面都是数值 0-15 0-12
  116. if (iLast == 0 && buffer[i] == AppConfigCache.separate)//endStr
  117. {
  118. iLast = i;
  119. bt[iCnt] = buffer[i];
  120. }
  121. else if (iLast != 0)
  122. {
  123. if (iCnt < AppConfigCache.messageLength - 1)
  124. {
  125. bt[++iCnt] = buffer[i];
  126. }
  127. else
  128. {
  129. break;
  130. }
  131. }
  132. }
  133. Array.Reverse(bt);
  134. if (iCnt + 1 != AppConfigCache.messageLength) continue;
  135. byte[] btDb = new byte[AppConfigCache.dataLength];
  136. int iC = 0;
  137. for (int i = AppConfigCache.startPosition; i < (AppConfigCache.startPosition + AppConfigCache.dataLength); i++)
  138. {
  139. btDb[iC++] = bt[i - 1];
  140. }
  141. string str = Encoding.UTF8.GetString(btDb, 0, btDb.Length);
  142. int weight = Convert.ToInt32(str);
  143. CacleCls.weight = weight;
  144. CacleCls.weightFlag = "0";
  145. CacleCls.weightMsgInfo = "";
  146. if (CacleCls.weight <= AppConfigCache.undulateValue)
  147. {
  148. CacleCls.isWd = 2;
  149. lWeight.Clear();
  150. }
  151. else
  152. {
  153. if (lWeight.Count == 0)
  154. {
  155. lWeight.Add(weight);
  156. CacleCls.isWd = 0;
  157. }
  158. else if (Math.Abs(weight - lWeight[0]) > AppConfigCache.undulateValue)
  159. {
  160. lWeight.Clear();
  161. lWeight.Add(weight);
  162. CacleCls.isWd = 1;
  163. }
  164. else if (lWeight.Count < AppConfigCache.undulateCount)
  165. {
  166. lWeight.Add(weight);
  167. }
  168. else
  169. {
  170. CacleCls.isWd = 0;
  171. }
  172. }
  173. }
  174. catch (Exception ex)
  175. {
  176. CacleCls.isWd = CacleCls.isWd == 0 ? 1 : CacleCls.isWd;
  177. CacleCls.weightFlag = "1";
  178. CacleCls.weightMsgInfo = "Moxa采集异常:" + ex.Message;
  179. }
  180. finally
  181. {
  182. Thread.Sleep(AppConfigCache.sleepTime);
  183. }
  184. }
  185. }
  186. #endregion
  187. }
  188. }