public void ParseResult(ref SensorAcqResult rawData) { rawData.ErrorCode = IsValid(rawData.Response); if (rawData.ErrorCode != (int)Errors.SUCCESS) { return; } short mno = ValueHelper.GetShort_BE(rawData.Response, 2); if (mno != rawData.Sensor.ModuleNo) { rawData.ErrorCode = (int)Errors.ERR_INVALID_MODULE; return; } switch (rawData.Response[IDX_CMD]) { case 0x81: // 采集命令回复 try { var surveyXAng = ValueHelper.GetInt_BE(rawData.Response, 13) / 1000000f; var surveyYAng = ValueHelper.GetInt_BE(rawData.Response, 17) / 1000000f; double changeX = 0; double changeY = 0; if (rawData.Sensor.Parameters != null && rawData.Sensor.Parameters.Count == 3) { double xinit = rawData.Sensor.Parameters[0].Value; double yinit = rawData.Sensor.Parameters[1].Value; double len = rawData.Sensor.Parameters[2].Value; var xvalue = (len * Math.Sin(surveyXAng * Math.PI / 180)) - (len * Math.Sin(xinit * Math.PI / 180)); var yvalue = (len * Math.Sin(surveyYAng * Math.PI / 180)) - (len * Math.Sin(yinit * Math.PI / 180)); changeX = xvalue; changeY = yvalue; } var raws = new double[] { surveyXAng, surveyYAng }; var phys = new double[] { changeX, changeY }; rawData.Data = new SensorData(raws, phys, raws) { JsonResultData = string.Format("{0}\"sensorId\":{1},\"data\":\"X方向角度:{2} °,Y方向角度:{3} °\"{4}", '{', rawData.Sensor.SensorID, surveyXAng, surveyYAng, '}') }; } catch (Exception ex) { rawData.ErrorCode = (int)Errors.ERR_DATA_PARSEFAILED; _logger.ErrorFormat( "rod Inclination sensor [Id:{0} m: {1}] parsedfailed,received bytes{2}, ERROR: {3}", rawData.Sensor.SensorID, rawData.Sensor.ModuleNo, ValueHelper.BytesToHexStr(rawData.Response), ex.Message); } break; case 0xC0: // 回复错误码 rawData.ErrorCode = ModbusErrorCode.GetErrorCode(rawData.Response[IDX_CMD + 1]); break; } }
public void ParseResult(ref SensorAcqResult rawData) { byte[] data = rawData.Response; rawData.ErrorCode = IsValid(data); if ((int)Errors.SUCCESS != rawData.ErrorCode) { return; } var module = ValueHelper.GetShort_BE(data, IDX_DevId); if (module != rawData.Sensor.ModuleNo) { rawData.ErrorCode = (int)Errors.ERR_INVALID_MODULE; return; } try { switch (data[IDX_CMD]) { case 0x81: var temp = ValueHelper.GetFloat_BE(data, IDX_DataField); var humidity = ValueHelper.GetFloat_BE(data, IDX_DataField + 4); var raws = new double[] { temp, humidity }; rawData.Data = new SensorData(raws, raws, raws) { JsonResultData = string.Format( "{0}\"sensorId\":{1},\"data\":\"温度:{2} ℃,湿度:{3} %\"{4}", '{', rawData.Sensor.SensorID, temp, humidity, '}') }; return; case 0xc0: rawData.ErrorCode = ModbusErrorCode.GetErrorCode(rawData.Response[IDX_CMD + 1]); return; } } catch (Exception ex) { rawData.ErrorCode = (int)Errors.ERR_DATA_PARSEFAILED; _logger.ErrorFormat( "ModBus temphumi sensor [id:{0},m:{1}] parsedfailed ,received bytes{3},ERROR : {2}", rawData.Sensor.SensorID, rawData.Sensor.ModuleNo, ex.Message, ValueHelper.BytesToHexStr(rawData.Response)); } }
public void ParseResult(ref SensorAcqResult rawData) { rawData.ErrorCode = IsValid(rawData.Response); if (rawData.ErrorCode != (int)Errors.SUCCESS) { return; } if (((rawData.Response[2] << 8) | rawData.Response[3]) != rawData.Sensor.ModuleNo) { rawData.ErrorCode = (int)Errors.ERR_INVALID_MODULE; return; } switch (rawData.Response[IDX_CMD]) { case 0x81: // 采集命令回复 try { var surveyXAng = ValueHelper.GetInt_BE(rawData.Response, 13) / 1000000f; var surveyYAng = ValueHelper.GetInt_BE(rawData.Response, 17) / 1000000f; double changedx = 0; double changedy = 0; double?r = null; if (rawData.Sensor.FormulaID == 6 && rawData.Sensor.Parameters != null && rawData.Sensor.Parameters.Count == 2) { double xinit = rawData.Sensor.Parameters[0].Value; double yinit = rawData.Sensor.Parameters[1].Value; var xvalue = surveyXAng - xinit; var yvalue = surveyYAng - yinit; changedx = xvalue; changedy = yvalue; } else if (rawData.Sensor.FormulaID == 30 && rawData.Sensor.Parameters != null && rawData.Sensor.Parameters.Count == 3) { double xinit = rawData.Sensor.Parameters[0].Value; double yinit = rawData.Sensor.Parameters[1].Value; double len = rawData.Sensor.Parameters[2].Value * 1000; var xvalue = (len * Math.Sin(surveyXAng * Math.PI / 180)) - (len * Math.Sin(xinit * Math.PI / 180)); var yvalue = (len * Math.Sin(surveyYAng * Math.PI / 180)) - (len * Math.Sin(yinit * Math.PI / 180)); r = Math.Sqrt(xvalue * xvalue + yvalue * yvalue); changedx = xvalue; changedy = yvalue; } var raws = new double[] { surveyXAng, surveyYAng }; List <double> phys = new List <double>(new double[] { changedx, changedy }); if (r != null) { phys.Add(r.Value); } rawData.Data = new SensorData(raws, phys.ToArray(), raws) { JsonResultData = string.Format("{0}\"sensorId\":{1},\"data\":\"X方向角度:{2} °,Y方向角度:{3} °\"{4}", '{', rawData.Sensor.SensorID, surveyXAng, surveyYAng, '}') }; } catch (Exception ex) { rawData.ErrorCode = (int)Errors.ERR_DATA_PARSEFAILED; _logger.ErrorFormat( "box Inclination sensor [Id:{0} m: {1}] parsedfailed,received bytes{2} ERROR: {3}", rawData.Sensor.SensorID, rawData.Sensor.ModuleNo, ValueHelper.BytesToHexStr(rawData.Response), ex.Message); } break; case 0xC0: // 回复错误码 rawData.ErrorCode = ModbusErrorCode.GetErrorCode(rawData.Response[IDX_CMD + 1]); break; } }
public void ParseResult(ref SensorAcqResult rawData) { byte[] data = rawData.Response; rawData.ErrorCode = IsValid(data); if ((int)Errors.SUCCESS != rawData.ErrorCode) { return; } var module = ValueHelper.GetShort_BE(data, IDX_DevId); if (module != rawData.Sensor.ModuleNo) { rawData.ErrorCode = (int)Errors.ERR_INVALID_MODULE; return; } if (data[IDX_CMD] == 0xC0) { rawData.ErrorCode = ModbusErrorCode.GetErrorCode(data[IDX_CMD + 1]); return; } var channel = data[IDX_CMD + 1]; if (channel == 0) { channel = 1; } if (channel != rawData.Sensor.ChannelNo) { rawData.ErrorCode = (int)Errors.ERR_RECEIVED_CH; return; } var freState = data[IDX_DataField + 2] & 0x000f; var tempState = data[IDX_DataField + 2] & 0xfff0; if (freState != 0 || tempState != 0) { if (freState != 0 && tempState != 0) { rawData.ErrorCode = (int)Errors.ERR_VIBRW_FREWIRE_TEMPWIRE; return; } if (freState != 0) { rawData.ErrorCode = (int)Errors.ERR_VIBRW_FREWIRE; return; } rawData.ErrorCode = (int)Errors.ERR_VIBRW_TEMPWIRE; return; } try { // TODO 大字序 FLOAT var frequency = ValueHelper.GetFloat_BE(data, IDX_DataField + 3); var am = ValueHelper.GetFloat_BE(data, IDX_DataField + 7); var temperature = ValueHelper.GetFloat_BE(data, IDX_DataField + 11); double physicalQuantity = 0; double colphy = 0; double k = 0; double f0 = 0; double kt = 0; double t0 = 0; if (rawData.Sensor.Parameters.Count >= 4) { k = rawData.Sensor.Parameters[0].Value; f0 = rawData.Sensor.Parameters[1].Value; kt = rawData.Sensor.Parameters[2].Value; t0 = rawData.Sensor.Parameters[3].Value; } switch (rawData.Sensor.FormulaID) { case 1: colphy = physicalQuantity = (k * (Math.Pow(frequency, 2) - Math.Pow(f0, 2)) + kt * (temperature - t0)); break; case 7: double H = rawData.Sensor.Parameters[4].Value; colphy = (k * (Math.Pow(f0, 2) - Math.Pow(frequency, 2)) + kt * (temperature - t0)); var h = colphy * 1000 / 9.8f; physicalQuantity = (H + h); break; case 17: physicalQuantity = (k * (Math.Pow(f0, 2) - Math.Pow(frequency, 2)) + kt * (temperature - t0)); colphy = physicalQuantity; break; default: physicalQuantity = (k * (Math.Pow(frequency, 2) - Math.Pow(f0, 2)) + kt * (temperature - t0)); colphy = physicalQuantity; break; } var raws = new double[] { frequency, temperature, am }; var phys = new double[] { physicalQuantity }; var colphys = new double[] { colphy }; rawData.Data = new SensorData(raws, phys, colphys) { JsonResultData = string.Format("{0}\"sensorId\":{1},\"data\":\"频率:{2} Hz,温度:{3} ℃\"{4}", '{', rawData.Sensor.SensorID, frequency, temperature, '}') }; } catch (Exception ex) { rawData.ErrorCode = (int)Errors.ERR_DATA_PARSEFAILED; _logger.ErrorFormat("ModBus VibratingWire sensor [id:{0},m:{1},ch:{4}] parsedfailed ,received bytes{3},ERROR : {2}", rawData.Sensor.SensorID, rawData.Sensor.ModuleNo, ex.Message, ValueHelper.BytesToHexStr(rawData.Response), rawData.Sensor.ChannelNo); } }