public void Send() { if (ReceiveDatas != null) { ReceiveDatas.Add(SendData); } }
/// <summary> /// 自动控制 /// </summary> public static void CompareData() { try { while (true) { ReceiveDatas receive = ReceiveDatasServices.LoadEntities(u => true).LastOrDefault(); ControlData control = ControlDataServices.LoadEntities(u => true).LastOrDefault(); if (receive != null && control != null) { //控制温度 if (receive.EnviroTemperate < control.MinTemperate) { Thread.Sleep(500); //如果温度过低提高温度 SendData(true, new byte[] { 0x13 }); Thread.Sleep(500); //如果温度过低提高温度 SendData(true, new byte[] { 0x13 }); Thread.Sleep(500); //如果温度过低提高温度 SendData(true, new byte[] { 0x13 }); } else if (receive.EnviroTemperate > control.MaxTemperate) { Thread.Sleep(500); //如果温度过高停止升温 SendData(true, new byte[] { 0x14 }); Thread.Sleep(500); //如果温度过高停止升温 SendData(true, new byte[] { 0x14 }); Thread.Sleep(500); //如果温度过高停止升温 SendData(true, new byte[] { 0x14 }); } else { //正常状态 Thread.Sleep(500); //如果温度过高停止升温 SendData(true, new byte[] { 0x14 }); Thread.Sleep(500); //如果温度过高停止升温 SendData(true, new byte[] { 0x14 }); Thread.Sleep(500); //如果温度过高停止升温 SendData(true, new byte[] { 0x14 }); } //控制氧气 if (receive.Oxygen < control.Oxygen) { Thread.Sleep(500); //开始增氧 SendData(true, new byte[] { 0x21 }); Thread.Sleep(500); //开始增氧 SendData(true, new byte[] { 0x21 }); Thread.Sleep(500); //开始增氧 SendData(true, new byte[] { 0x21 }); } else { Thread.Sleep(500); //正常状态,停止增氧 SendData(true, new byte[] { 0x22 }); Thread.Sleep(500); //正常状态,停止增氧 SendData(true, new byte[] { 0x22 }); Thread.Sleep(500); //正常状态,停止增氧 SendData(true, new byte[] { 0x22 }); } //控制排水 if (receive.PhValues > control.PhValues) { Thread.Sleep(500); //开始排水 SerialPortHelper.SendData(true, new byte[] { 0x11 }); Thread.Sleep(500); //开始排水 SerialPortHelper.SendData(true, new byte[] { 0x11 }); Thread.Sleep(500); //开始排水 SerialPortHelper.SendData(true, new byte[] { 0x11 }); } else { Thread.Sleep(500); //停止排水 SendData(true, new byte[] { 0x12 }); Thread.Sleep(500); //停止排水 SendData(true, new byte[] { 0x12 }); Thread.Sleep(500); //停止排水 SendData(true, new byte[] { 0x12 }); } } Thread.Sleep(10000); } } catch { } }
/// <summary> /// 接收数据处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> static void DataReceived() { try { while (true) { byte[] bytes = new byte[1024 * 1024 * 3]; int r = socket.Receive(bytes); string content = getStringFromBytes(bytes, r); builder.Append(content + " ");//添加到数据缓存中 sum = builder.Length; if (sum > 100) { string text = builder.ToString(); #region 一号节点的数据 Match match = Regex.Match(text, "FC A1 ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2})"); //温度 int value1 = GetHexadecimalValue(match.Groups[1].Value); //湿度 int value2 = GetHexadecimalValue(match.Groups[2].Value); //水温 int value3 = GetHexadecimalValue(match.Groups[3].Value); //光照 int value4 = GetHexadecimalValue(match.Groups[4].Value); #endregion #region 二号节点的数据 Match matchtwo = Regex.Match(text, "FC A2 ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2})"); //温度 int value21 = GetHexadecimalValue(matchtwo.Groups[1].Value); //湿度 int value22 = GetHexadecimalValue(matchtwo.Groups[2].Value); //水温 int value23 = GetHexadecimalValue(matchtwo.Groups[3].Value); //光照 int value24 = GetHexadecimalValue(matchtwo.Groups[4].Value); #endregion //溶解氧的数据 Match match2 = Regex.Match(text, "FC AB 01 03 0C ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2})"); string v1 = match2.Groups[1].Value; string v2 = match2.Groups[2].Value; int shuju = GetHexadecimalValue(v1 + v2); double all = ((double)shuju) / 1000; //PH值 Match match3 = Regex.Match(text, "FC AB 02 03 02 ([a-fA-F0-9]{2}) ([a-fA-F0-9]{2})"); string s1 = match3.Groups[1].Value; string s2 = match3.Groups[2].Value; int phvalue = GetHexadecimalValue(s1 + s2); double phv = phvalue / 100; if (value1 != 0 && value2 != 0 && value3 != 0 && all != 0 && phvalue != 0) { ReceiveDatas data = new ReceiveDatas(); data.EnviroTemperate = value1; data.EnviroHumidity = value2; data.FishTemperate = value3; data.SunValue = value4; data.Oxygen = all; data.PhValues = phv; data.EnviroTemperate2 = value21; data.EnviroHumidity2 = value22; data.FishTemperate2 = value23; data.SunValue2 = value24; data.SubTime = DateTime.Now; ReceiveDatasServices.Add(data); //Thread.Sleep(0000); } sum = 0; //清除接收数据 builder.Clear(); } } } catch { Thread.Sleep(1000); socket.Close(); StartGet(); } }