MoxaCls.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Sockets;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace RailLocalMeter
  12. {
  13. /// <summary>
  14. /// Moxa重量采集及判稳
  15. /// </summary>
  16. public class MoxaCls
  17. {
  18. Log lg = Log.GetInstance();
  19. Socket client = null;
  20. IPAddress ip = null;
  21. IPEndPoint point = null;
  22. bool blThreadFlag;//数据采集线程开关
  23. Thread DataCollectThread = null;//采集进程
  24. public MoxaCls()
  25. {
  26. //新数据采集
  27. //获取IP地址和端口并创建IPEndPoint
  28. ip = IPAddress.Parse(AppConfigCache.moxaIP);
  29. point = new IPEndPoint(ip, AppConfigCache.moxaPort);
  30. blThreadFlag = true;
  31. DataCollectThread = new Thread(new ThreadStart(DataCollect));
  32. }
  33. public void start()
  34. {
  35. if (client == null)
  36. {
  37. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  38. //client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, -300);
  39. //连接到服务器
  40. client.Connect(point);
  41. }
  42. //连接到服务器
  43. else if (!client.Connected)
  44. {
  45. client.Close();
  46. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  47. client.Connect(point);
  48. }
  49. DataCollectThread.Start();
  50. }
  51. public void ClosingCollect()
  52. {
  53. blThreadFlag = false;
  54. }
  55. #region 数据采集
  56. private List<int> lWeight = new List<int>();
  57. /// <summary>
  58. /// 数据采集线程
  59. /// </summary>
  60. private void DataCollect()
  61. {
  62. if (!blThreadFlag) return;
  63. //client.Listen(10);
  64. //连接到服务器
  65. while (!client.Connected)
  66. {
  67. try
  68. {
  69. client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  70. client.Connect(point);
  71. }
  72. catch (Exception err)
  73. {
  74. CacleCls.weightFlag = "1";
  75. CacleCls.weightMsgInfo = "Moxa连接异常";
  76. //WriteLog($"连接Moxa失败,正重试{err.Message}");
  77. }
  78. Thread.Sleep(500);
  79. }
  80. byte[] btDbs = new byte[AppConfigCache.messageLength];//数据填充bt
  81. byte[] buffer = new byte[AppConfigCache.messageLength];
  82. int isLd = 0, itest = 0;
  83. while (blThreadFlag)
  84. {
  85. try
  86. {
  87. byte[] buffers = new byte[AppConfigCache.messageLength];
  88. //Debug.WriteLine("client开始:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  89. int n = client.Receive(buffers); //由于仪表每1秒是采集多少次是不确定的,如果这个buffer的长度设置的太长会导致这里读取的时间很长
  90. //Debug.WriteLine("client结束:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
  91. int icn = 0, iGet = 0;
  92. for (int i = 0; i < buffer.Length; i++)
  93. {
  94. if (buffer[i] != 0)
  95. {
  96. icn++;
  97. }
  98. else
  99. {
  100. break;
  101. }
  102. }
  103. if (icn != buffer.Length) //没有12个长度则继续接收直到满12个长度
  104. {
  105. for (int i = 0; i < n; i++)
  106. {
  107. if (icn < buffer.Length)
  108. {
  109. buffer[icn++] = buffers[i];
  110. iGet++;
  111. }
  112. else break;
  113. }
  114. }
  115. if (icn == buffer.Length)
  116. {
  117. if (AppConfigCache.isFz == "true")
  118. {
  119. byte[] bf = buffer.Reverse().ToArray();
  120. int mx = 0;
  121. foreach (byte b in bf)
  122. {
  123. if (b > 0) buffer[mx++] = b;
  124. }
  125. }
  126. int iLastNew = 0;
  127. byte[] btNew = new byte[btDbs.Length + buffer.Length];
  128. for (int i = 0; i < btDbs.Length; i++)
  129. {
  130. if (btDbs[i] != 0)
  131. {
  132. btNew[iLastNew++] = btDbs[i];
  133. }
  134. }
  135. int bufferLast = 0;
  136. for (int i = 0; i < buffer.Length; i++)
  137. {
  138. if (buffer[i] == AppConfigCache.separate)
  139. {
  140. bufferLast = i;
  141. break;
  142. }
  143. else
  144. {
  145. btNew[iLastNew++] = buffer[i];
  146. }
  147. }
  148. for (int i = 0; i < btDbs.Length; i++)
  149. {
  150. btDbs[i] = 0;
  151. }
  152. for (int i = bufferLast; i < buffer.Length; i++)
  153. {
  154. btDbs[i - bufferLast] = buffer[i];
  155. }
  156. byte[] bt = new byte[AppConfigCache.messageLength];
  157. if (iLastNew == AppConfigCache.messageLength)
  158. {
  159. for (int i = 0; i < iLastNew; i++)
  160. {
  161. bt[i] = btNew[i];
  162. }
  163. }
  164. if (iLastNew != AppConfigCache.messageLength) continue;
  165. #region 在这里要数一下buffer里面的数据,看下结束符的byte值是多少,每个秤都确定好之后,就删除这个即可
  166. /*
  167. int m = 0;
  168. string strX = "[";
  169. for (int i = 0; i < bt.Length; i++)
  170. {
  171. if (bt[i] > 0)
  172. {
  173. m++;
  174. strX += $"{bt[i]} ";
  175. }
  176. }
  177. strX += "]";
  178. string strx = Encoding.UTF8.GetString(bt, 0, bt.Length);
  179. Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + strX);
  180. //Debug.WriteLine(strx);
  181. //*/
  182. #endregion
  183. byte[] btDb = new byte[AppConfigCache.dataLength];
  184. int iC = 0;
  185. for (int i = AppConfigCache.startPosition; i < (AppConfigCache.startPosition + AppConfigCache.dataLength); i++)
  186. {
  187. btDb[iC++] = bt[i - 1];
  188. }
  189. int weight = 0;
  190. string str = Encoding.UTF8.GetString(btDb, 0, btDb.Length);
  191. bool flag = int.TryParse(str, out weight);
  192. #if DEBUG
  193. #region 用于debug调试的代码
  194. /*
  195. if (itest> 100 && itest < 300)
  196. {
  197. weight = 28500;
  198. itest++;
  199. }
  200. else if (itest < 500)
  201. {
  202. weight = 28500;
  203. itest++;
  204. }
  205. else
  206. {
  207. itest = 0;
  208. }
  209. CacleCls.rfidCarNo = "X70 5337172";
  210. //*/
  211. #endregion
  212. #endif
  213. CacleCls.weight = weight;
  214. CacleCls.weightFlag = "0";
  215. CacleCls.weightMsgInfo = "";
  216. if (CacleCls.weight <= AppConfigCache.undulateValue)
  217. {
  218. CacleCls.isWd = 2;
  219. lWeight.Clear();
  220. }
  221. else
  222. {
  223. if (lWeight.Count == 0)
  224. {
  225. lWeight.Add(weight);
  226. CacleCls.isWd = 1;
  227. }
  228. else if (Math.Abs(weight - lWeight[0]) > AppConfigCache.undulateValue)
  229. {
  230. lWeight.Clear();
  231. lWeight.Add(weight);
  232. CacleCls.isWd = 1;
  233. }
  234. else if (lWeight.Count < AppConfigCache.undulateCount)
  235. {
  236. lWeight.Add(weight);
  237. }
  238. else
  239. {
  240. CacleCls.isWd = 0;
  241. }
  242. }
  243. if (Math.Abs(weight) > 0)
  244. {
  245. if (Math.Abs(weight) <= AppConfigCache.minValue)
  246. {
  247. if (isLd < 4) isLd++; //连续4次采集到不为0,且满足零点最小绝对值
  248. if (isLd == 4) CacleCls.isZeroState = true;
  249. }
  250. }
  251. else
  252. {
  253. CacleCls.isZeroState = false;
  254. isLd = 0;
  255. }
  256. for (int i = 0; i < buffer.Length; i++)
  257. {
  258. buffer[i] = 0;
  259. }
  260. if (iGet < n)
  261. {
  262. for (int i = 0; i < n - iGet; i++)
  263. {
  264. buffer[i] = buffers[iGet + i];
  265. }
  266. }
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. lg.WriteLog(LogType.MoxaLog, "Moxa采集异常:" + ex.Message);
  272. CacleCls.isWd = CacleCls.isWd == 0 ? 1 : CacleCls.isWd;
  273. CacleCls.weightFlag = "1";
  274. CacleCls.weightMsgInfo = "Moxa采集异常:" + ex.Message;
  275. }
  276. finally
  277. {
  278. //Thread.Sleep(100);
  279. }
  280. }
  281. }
  282. #endregion
  283. }
  284. }