using System; using System.Collections.Generic; using System.Configuration; 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 { 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.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; //连接到服务器 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); } while (blThreadFlag) { try { byte[] buffer = new byte[32 * 32]; int n = client.Receive(buffer); if (AppConfigCache.isFz == "true") { byte[] bf = buffer.Reverse().ToArray(); int mx = 0; foreach (byte b in bf) { if (b > 0) buffer[mx++] = b; } } #region 在这里要数一下buffer里面的数据,看下结束符的byte值是多少,每个秤都确定好之后,就删除这个即可 int m = 0; string strX = "["; for (int i = 0; i < buffer.Length; i++) { if (buffer[i] > 0) { m++; strX += $"{buffer[i]} "; } } strX += "]["; strX += Encoding.UTF8.GetString(buffer, 0, m); strX += "]"; //string strx = Encoding.UTF8.GetString(buffer, 0, m); //在这里要数一下buffer里面的数据,看下结束符的byte值是多少 //textBox1.Invoke(new Action(() => { // textBox1.Text += strX; //})); #endregion byte[] bt = new byte[AppConfigCache.messageLength]; int iLast = 0, iCnt = 0; for (int i = n - 1; i > -1; i--) { //byte字节里面都是数值 0-15 0-12 if (iLast == 0 && buffer[i] == AppConfigCache.separate)//endStr { iLast = i; bt[iCnt] = buffer[i]; } else if (iLast != 0) { if (iCnt < AppConfigCache.messageLength - 1) { bt[++iCnt] = buffer[i]; } else { break; } } } Array.Reverse(bt); if (iCnt + 1 != AppConfigCache.messageLength) continue; 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]; } string str = Encoding.UTF8.GetString(btDb, 0, btDb.Length); int weight = Convert.ToInt32(str); 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 = 0; } 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; } } } catch (Exception ex) { CacleCls.isWd = CacleCls.isWd == 0 ? 1 : CacleCls.isWd; CacleCls.weightFlag = "1"; CacleCls.weightMsgInfo = "Moxa采集异常:" + ex.Message; } finally { Thread.Sleep(AppConfigCache.sleepTime); } } } #endregion } }