| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318 |
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Diagnostics;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace RailLocalMeter
- {
- /// <summary>
- /// Moxa重量采集及判稳
- /// </summary>
- public class MoxaCls
- {
- Log lg = Log.GetInstance();
- Socket client = null;
- IPAddress ip = null;
- IPEndPoint point = null;
- bool blThreadFlag;//数据采集线程开关
- Thread DataCollectThread = null;//采集进程
- public MoxaCls()
- {
- //新数据采集
- //获取IP地址和端口并创建IPEndPoint
- ip = IPAddress.Parse(AppConfigCache.moxaIP);
- point = new IPEndPoint(ip, AppConfigCache.moxaPort);
- blThreadFlag = true;
- DataCollectThread = new Thread(new ThreadStart(DataCollect));
- }
- public void start()
- {
- if (client == null)
- {
- client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- //client.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, -300);
- //连接到服务器
- client.Connect(point);
- }
- //连接到服务器
- else if (!client.Connected)
- {
- client.Close();
- client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- client.Connect(point);
- }
- DataCollectThread.Start();
- }
- public void ClosingCollect()
- {
- blThreadFlag = false;
- }
- #region 数据采集
- private List<int> lWeight = new List<int>();
- /// <summary>
- /// 数据采集线程
- /// </summary>
- private void DataCollect()
- {
- if (!blThreadFlag) return;
- //client.Listen(10);
- //连接到服务器
- while (!client.Connected)
- {
- try
- {
- client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
- client.Connect(point);
- }
- catch (Exception err)
- {
- CacleCls.weightFlag = "1";
- CacleCls.weightMsgInfo = "Moxa连接异常";
- //WriteLog($"连接Moxa失败,正重试{err.Message}");
- }
- Thread.Sleep(500);
- }
- byte[] btDbs = new byte[AppConfigCache.messageLength];//数据填充bt
- byte[] buffer = new byte[AppConfigCache.messageLength];
- int isLd = 0, itest = 0;
- while (blThreadFlag)
- {
- try
- {
- byte[] buffers = new byte[AppConfigCache.messageLength];
- //Debug.WriteLine("client开始:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
- int n = client.Receive(buffers); //由于仪表每1秒是采集多少次是不确定的,如果这个buffer的长度设置的太长会导致这里读取的时间很长
- //Debug.WriteLine("client结束:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
- int icn = 0, iGet = 0;
- for (int i = 0; i < buffer.Length; i++)
- {
- if (buffer[i] != 0)
- {
- icn++;
- }
- else
- {
- break;
- }
- }
- if (icn != buffer.Length) //没有12个长度则继续接收直到满12个长度
- {
- for (int i = 0; i < n; i++)
- {
- if (icn < buffer.Length)
- {
- buffer[icn++] = buffers[i];
- iGet++;
- }
- else break;
- }
- }
- if (icn == buffer.Length)
- {
- if (AppConfigCache.isFz == "true")
- {
- byte[] bf = buffer.Reverse().ToArray();
- int mx = 0;
- foreach (byte b in bf)
- {
- if (b > 0) buffer[mx++] = b;
- }
- }
- int iLastNew = 0;
- byte[] btNew = new byte[btDbs.Length + buffer.Length];
- for (int i = 0; i < btDbs.Length; i++)
- {
- if (btDbs[i] != 0)
- {
- btNew[iLastNew++] = btDbs[i];
- }
- }
- int bufferLast = 0;
- for (int i = 0; i < buffer.Length; i++)
- {
- if (buffer[i] == AppConfigCache.separate)
- {
- bufferLast = i;
- break;
- }
- else
- {
- btNew[iLastNew++] = buffer[i];
- }
- }
- for (int i = 0; i < btDbs.Length; i++)
- {
- btDbs[i] = 0;
- }
- for (int i = bufferLast; i < buffer.Length; i++)
- {
- btDbs[i - bufferLast] = buffer[i];
- }
- byte[] bt = new byte[AppConfigCache.messageLength];
- if (iLastNew == AppConfigCache.messageLength)
- {
- for (int i = 0; i < iLastNew; i++)
- {
- bt[i] = btNew[i];
- }
- }
- if (iLastNew != AppConfigCache.messageLength) continue;
- #region 在这里要数一下buffer里面的数据,看下结束符的byte值是多少,每个秤都确定好之后,就删除这个即可
- /*
- int m = 0;
- string strX = "[";
- for (int i = 0; i < bt.Length; i++)
- {
- if (bt[i] > 0)
- {
- m++;
- strX += $"{bt[i]} ";
- }
- }
- strX += "]";
- string strx = Encoding.UTF8.GetString(bt, 0, bt.Length);
- Debug.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + strX);
- //Debug.WriteLine(strx);
- //*/
- #endregion
- byte[] btDb = new byte[AppConfigCache.dataLength];
- int iC = 0;
- for (int i = AppConfigCache.startPosition; i < (AppConfigCache.startPosition + AppConfigCache.dataLength); i++)
- {
- btDb[iC++] = bt[i - 1];
- }
- int weight = 0;
- string str = Encoding.UTF8.GetString(btDb, 0, btDb.Length);
- bool flag = int.TryParse(str, out weight);
- #if DEBUG
- #region 用于debug调试的代码
- /*
- if (itest> 100 && itest < 300)
- {
- weight = 28500;
- itest++;
- }
- else if (itest < 500)
- {
- weight = 28500;
- itest++;
- }
- else
- {
- itest = 0;
- }
- CacleCls.rfidCarNo = "X70 5337172";
- //*/
- #endregion
- #endif
- CacleCls.weight = weight;
- CacleCls.weightFlag = "0";
- CacleCls.weightMsgInfo = "";
- if (CacleCls.weight <= AppConfigCache.undulateValue)
- {
- CacleCls.isWd = 2;
- lWeight.Clear();
- }
- else
- {
- if (lWeight.Count == 0)
- {
- lWeight.Add(weight);
- CacleCls.isWd = 1;
- }
- else if (Math.Abs(weight - lWeight[0]) > AppConfigCache.undulateValue)
- {
- lWeight.Clear();
- lWeight.Add(weight);
- CacleCls.isWd = 1;
- }
- else if (lWeight.Count < AppConfigCache.undulateCount)
- {
- lWeight.Add(weight);
- }
- else
- {
- CacleCls.isWd = 0;
- }
- }
- if (Math.Abs(weight) > 0)
- {
- if (Math.Abs(weight) <= AppConfigCache.minValue)
- {
- if (isLd < 4) isLd++; //连续4次采集到不为0,且满足零点最小绝对值
- if (isLd == 4) CacleCls.isZeroState = true;
- }
- }
- else
- {
- CacleCls.isZeroState = false;
- isLd = 0;
- }
- for (int i = 0; i < buffer.Length; i++)
- {
- buffer[i] = 0;
- }
- if (iGet < n)
- {
- for (int i = 0; i < n - iGet; i++)
- {
- buffer[i] = buffers[iGet + i];
- }
- }
- }
- }
- catch (Exception ex)
- {
- lg.WriteLog(LogType.MoxaLog, "Moxa采集异常:" + ex.Message);
- CacleCls.isWd = CacleCls.isWd == 0 ? 1 : CacleCls.isWd;
- CacleCls.weightFlag = "1";
- CacleCls.weightMsgInfo = "Moxa采集异常:" + ex.Message;
- }
- finally
- {
- //Thread.Sleep(100);
- }
- }
- }
- #endregion
- }
- }
|