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 CarLocalMeter
{
///
/// Moxa重量采集及判稳
///
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 lWeight = new List();
///
/// 数据采集线程
///
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.voicCarNo = "湘G88888";
//*/
#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
}
}