|
|
@@ -0,0 +1,317 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Configuration;
|
|
|
+using System.Diagnostics;
|
|
|
+using System.IO.Ports;
|
|
|
+using System.Linq;
|
|
|
+using System.Net;
|
|
|
+using System.Net.Sockets;
|
|
|
+using System.Text;
|
|
|
+using System.Threading;
|
|
|
+using System.Threading.Tasks;
|
|
|
+
|
|
|
+namespace CarLocalMeter
|
|
|
+{
|
|
|
+ public class ComCls
|
|
|
+ {
|
|
|
+ Log lg = Log.GetInstance();
|
|
|
+ bool blThreadFlag;//数据采集线程开关
|
|
|
+ Thread DataCollectThread = null;//采集进程
|
|
|
+ SerialPort serialPort1;
|
|
|
+ private object obj = new object();
|
|
|
+ string msg = "";
|
|
|
+
|
|
|
+ public ComCls()
|
|
|
+ {
|
|
|
+ //新数据采集
|
|
|
+ blThreadFlag = true;
|
|
|
+ DataCollectThread = new Thread(new ThreadStart(DataCollect));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void start()
|
|
|
+ {
|
|
|
+ #region 串口采集配置
|
|
|
+ Parity parity = Parity.None;
|
|
|
+
|
|
|
+ if (AppConfigCache.comParity.ToUpper() == "ODD")
|
|
|
+ {
|
|
|
+ parity = Parity.Odd;
|
|
|
+ }
|
|
|
+ if (AppConfigCache.comParity.ToUpper() == "EVEN")
|
|
|
+ {
|
|
|
+ parity = Parity.Even;
|
|
|
+ }
|
|
|
+
|
|
|
+ StopBits stopBits = StopBits.None;
|
|
|
+ if (AppConfigCache.comStopBits == "1")
|
|
|
+ {
|
|
|
+ stopBits = StopBits.One;
|
|
|
+ }
|
|
|
+ if (AppConfigCache.comStopBits == "1.5")
|
|
|
+ {
|
|
|
+ stopBits = StopBits.OnePointFive;
|
|
|
+ }
|
|
|
+ if (AppConfigCache.comStopBits == "2")
|
|
|
+ {
|
|
|
+ stopBits = StopBits.Two;
|
|
|
+ }
|
|
|
+ serialPort1 = new SerialPort(AppConfigCache.comPort, AppConfigCache.comBaud, parity, AppConfigCache.comLength, stopBits);
|
|
|
+ int icnt = 0;
|
|
|
+ msg = "";
|
|
|
+ while (!serialPort1.IsOpen)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ icnt++;
|
|
|
+ serialPort1.Open();
|
|
|
+ }
|
|
|
+ catch (Exception err)
|
|
|
+ {
|
|
|
+ lg.WriteLog(LogType.MoxaLog, $"打开串口失败,正重试{err.Message}");
|
|
|
+ }
|
|
|
+ System.Threading.Thread.Sleep(500);
|
|
|
+ if (icnt > 5)
|
|
|
+ {
|
|
|
+ msg = "串口打开失败,请检查串口是否存在,且是否被占用";
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ DataCollectThread.Start();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void ClosingCollect()
|
|
|
+ {
|
|
|
+ blThreadFlag = false;
|
|
|
+ if (serialPort1?.IsOpen == true) serialPort1?.Close();
|
|
|
+ }
|
|
|
+
|
|
|
+ #region 数据采集
|
|
|
+ private List<int> lWeight = new List<int>();
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 数据采集线程
|
|
|
+ /// </summary>
|
|
|
+ private void DataCollect()
|
|
|
+ {
|
|
|
+ if (!blThreadFlag) return;
|
|
|
+
|
|
|
+ byte[] btDbs = new byte[AppConfigCache.messageLength];//数据填充bt
|
|
|
+ byte[] buffer = new byte[AppConfigCache.messageLength];
|
|
|
+ int isLd = 0;
|
|
|
+ while (blThreadFlag)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ lock (obj)
|
|
|
+ {
|
|
|
+ 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"));
|
|
|
+
|
|
|
+ Thread.Sleep(50);
|
|
|
+ int n = serialPort1.BytesToRead;
|
|
|
+ if (n > 0)
|
|
|
+ {
|
|
|
+ serialPort1.Read(buffers, 0, buffers.Length);
|
|
|
+ }
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+
|
|
|
+ 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, $"串口采集异常:{ex.Message}");
|
|
|
+ CacleCls.isWd = CacleCls.isWd == 0 ? 1 : CacleCls.isWd;
|
|
|
+ CacleCls.weightFlag = "1";
|
|
|
+ CacleCls.weightMsgInfo = $"串口采集异常:{ex.Message}";
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ //Thread.Sleep(100);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|