static void testLiquidConfig() { byte ss = byte.Parse(DateTime.Now.ToString("yy")); byte ss1 = byte.Parse(DateTime.Now.ToString("MM")); byte ss2 = byte.Parse(DateTime.Now.ToString("dd")); byte ss3 = byte.Parse(DateTime.Now.ToString("HH")); byte ss4 = byte.Parse(DateTime.Now.ToString("mm")); byte ss5 = byte.Parse(DateTime.Now.ToString("ss")); byte ss7 = byte.Parse("0" + ((int)DateTime.Now.DayOfWeek).ToString()); DeviceConfigInfo conf; conf = (new DeviceConfig()).GetDeviceConfByDeviceCodeAndSensorCode("112015090038", "000034"); if (conf != null) { string content = conf.FrameContent; string[] items = content.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries); byte[] sendData = new byte[items.Length + 9 + 2];//补充2个字节,用于扩展报警界限 for (int i = 0; i < items.Length - 10; i++) { sendData[i + 9] = byte.Parse(items[i], System.Globalization.NumberStyles.HexNumber); } //修正报警规则信息 SensorHub.BLL.DjLiquid djBll = new SensorHub.BLL.DjLiquid(); AlarmRuleInfo rule = djBll.getAlarmRuleByDevcode("112015090038"); float highValue = rule != null ? rule.HighValue : 1000; byte[] btHighValue = BitConverter.GetBytes(highValue); sendData[9 + 10 + 0] = btHighValue[0]; sendData[9 + 10 + 1] = btHighValue[1]; sendData[9 + 10 + 2] = btHighValue[2]; sendData[9 + 10 + 3] = btHighValue[3]; // if (requestInfo.Parameters[0] == "50") // { //液位时间修正 sendData[21 + 2] = ss5; sendData[22 + 2] = ss4; sendData[23 + 2] = ss3; sendData[24 + 2] = ss7; sendData[25 + 2] = ss2; sendData[26 + 2] = ss1; sendData[27 + 2] = ss; sendData[27 + 2 + 1] = 0x03; // } sendData[0] = 0XAA; sendData[1] = 0X1D; sendData[2] = 0x00; sendData[3] = 0x00; sendData[4] = 0X03; sendData[5] = 0X00; sendData[6] = System.BitConverter.GetBytes(sendData.Length - 9)[0]; sendData[7] = 0X00; sendData[8] = 0X00; String crcIn = ""; for (int i = 0; i < sendData.Length; i++) { crcIn += sendData[i].ToString("X2"); } ushort crcOut = CodeUtils.QuickCRC16(crcIn, 0, crcIn.Length); byte[] crcOutByte = BitConverter.GetBytes(crcOut); sendData[7] = crcOutByte[1]; sendData[8] = crcOutByte[0]; } }
public override void ExecuteCommand(SZLiquidSession session, StringRequestInfo requestInfo) { session.Logger.Info("LSDATOVER!"); try { //数据格式处理 string[] data = requestInfo.Body.Split(','); String dataStr = ""; for (int i = 0; i < data.Length - 1; i++) { dataStr += data[i]; if (i != data.Length - 2) { dataStr += ','; } } String header9received = data[data.Length - 1]; //数据应用 String macId; if (string.IsNullOrEmpty(session.MacID)) { macId = "未知"; } else { macId = session.MacID; } session.Logger.Info("液位监测仪器:" + macId + ":液位数据上传完成帧: " + dataStr); byte[] batchSet = System.BitConverter.GetBytes(session.Batch); byte[] stateSet = { 0x50, 0x00, 0x09, 0x02, 0x00, 0x00, 0x34, 0x22, batchSet[3],batchSet[2], batchSet[1], batchSet[0], 0x03 }; byte[] set = new byte[stateSet.Length + 9]; for (int i = 9; i < set.Length; i++) { set[i] = stateSet[i - 9]; } set[0] = 0XAA; set[1] = 0X1D; set[2] = byte.Parse(header9received.Substring(4, 2), System.Globalization.NumberStyles.HexNumber); set[3] = byte.Parse(header9received.Substring(6, 2), System.Globalization.NumberStyles.HexNumber); set[4] = 0X03; set[5] = 0X00; set[6] = System.BitConverter.GetBytes(set.Length - 9)[0]; set[7] = 0X00; set[8] = 0X00; String crcIn = ""; for (int i = 0; i < set.Length; i++) { crcIn += set[i].ToString("X2"); } ushort crcOut = CodeUtils.QuickCRC16(crcIn, 0, crcIn.Length); byte[] crcOutByte = BitConverter.GetBytes(crcOut); set[7] = crcOutByte[1]; set[8] = crcOutByte[0]; session.Send(set, 0, set.Length); session.Logger.Info("成功下发液位数据上传完成帧:" + BitConverter.ToString(set, 0)); } catch (Exception e) { session.Logger.Error("液位数据上传完成帧下发失败" + requestInfo.Body); session.Logger.Error(e.ToString()); } finally { } }
protected override StringRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length) { string set = "heartbeat"; String com = ""; if (length == set.Length + 15) { com = Encoding.UTF8.GetString(readBuffer.CloneRange(offset + 1, set.Length)); } if ((length == set.Length + 15) && (com == set)) { String command = Encoding.UTF8.GetString(readBuffer.CloneRange(offset + 1, set.Length)); String body = Encoding.UTF8.GetString(readBuffer.CloneRange(offset, length)); String mBody = body.Substring(0, body.Length - 2); String[] param = mBody.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); //Console.WriteLine("液位监测仪器指令头:" + Encoding.UTF8.GetString(header.Array, header.Offset, 9)); //shao debug return(new StringRequestInfo(command, mBody, param)); } else if (length > 19 && 170 == readBuffer[offset]) { byte[] readBufferLocal = readBuffer.CloneRange(offset, length); //检查数据正确性 //1.检查数据中间是否有AA for (int i = 1; i < readBufferLocal.Length; i++) { if (170 == readBufferLocal[i]) { readBufferLocal = readBufferLocal.CloneRange(0, i); break; } } //2.检查数据长度 if (readBufferLocal[6] + 9 > readBufferLocal.Length) { String bodyError = ""; for (int i = 19; i < readBufferLocal.Length; i++) { bodyError += readBufferLocal[i].ToString("X2"); } String mBodyError = "数据长度不够," + bodyError.Substring(0, bodyError.Length); String[] paramError = mBodyError.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); return(new StringRequestInfo("ERROR:", mBodyError, paramError)); } if (readBufferLocal[6] + 9 < readBufferLocal.Length) { readBufferLocal = readBufferLocal.CloneRange(0, readBufferLocal[6] + 9); } //3.CRC校验 String crcIn = ""; byte crcHigh = readBufferLocal[7]; byte crcLow = readBufferLocal[8]; readBufferLocal[7] = 0; readBufferLocal[8] = 0; for (int i = 0; i < readBufferLocal.Length; i++) { crcIn += readBufferLocal[i].ToString("X2"); } ushort crcOut = CodeUtils.QuickCRC16(crcIn, 0, crcIn.Length); byte[] crcOutByte = BitConverter.GetBytes(crcOut); if (crcHigh != crcOutByte[1] || crcLow != crcOutByte[0]) { String bodyError = ""; for (int i = 19; i < readBufferLocal.Length; i++) { bodyError += readBufferLocal[i].ToString("X2"); } String mBodyError = "CRC校验错误," + bodyError.Substring(0, bodyError.Length); String[] paramError = mBodyError.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); return(new StringRequestInfo("ERROR:", mBodyError, paramError)); } //处理信息 String header9 = ""; for (int i = 0; i < 9; i++) { header9 += readBufferLocal[i].ToString("X2"); } String command = Encoding.UTF8.GetString(readBufferLocal.CloneRange(9, 10)); String body = Encoding.UTF8.GetString(readBufferLocal.CloneRange(19, readBufferLocal[6] - 10)); String mBody = body.Substring(0, body.Length - 2) + ',' + header9; String[] param = mBody.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries); return(new StringRequestInfo(command, mBody, param)); } return(new StringRequestInfo("NULL", string.Empty, Encoding.ASCII.GetString(Convert.FromBase64String(String.Empty)).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries))); //return null; }