Ejemplo n.º 1
0
         public void ParseResult(ref SensorAcqResult rawData)
         {
             rawData.ErrorCode = IsValid(rawData.Response);
             if ((int)Errors.SUCCESS != rawData.ErrorCode)
                 return;
             int module = (int) ((rawData.Response[IDX_ADRESS] << 8) | (rawData.Response[1 + IDX_ADRESS]));
             if (module != rawData.Sensor.ModuleNo)
             {
                 rawData.ErrorCode = (int)Errors.ERR_INVALID_MODULE;
                 return;
             }
             int channel = Convert.ToInt16(rawData.Response[IDX_Req]);
             if (channel != rawData.Sensor.ChannelNo)
             {
                 rawData.ErrorCode = (int)Errors.ERR_RECEIVED_CH;
                 return;
             }
             try
             {
                 var capvol = (rawData.Response[IDX_CAPVOL] << 8) | rawData.Response[IDX_CAPVOL + 1];
                 var voltage = ValueHelper.GetFloat(rawData.Response, IDX_VOLTAGE);
                 var temp = ValueHelper.GetFloat(rawData.Response, IDX_TEMP);
                 double force = 0;
                 if (rawData.Sensor.FormulaID == 4 && rawData.Sensor.Parameters != null &&
                     rawData.Sensor.Parameters.Count == 7)
                 {
                     double kt = rawData.Sensor.Parameters[0].Value;
                     double t0 = rawData.Sensor.Parameters[1].Value;
                     double v0 = rawData.Sensor.Parameters[2].Value;
                     double v0B = rawData.Sensor.Parameters[3].Value;
                     double c0 = rawData.Sensor.Parameters[4].Value;
                     double c1 = rawData.Sensor.Parameters[5].Value;
                     double c2 = rawData.Sensor.Parameters[6].Value;

                     double vol = voltage - kt*(temp - t0) - (v0 - v0B);
                     double phy = c2*Math.Pow(vol, 2) + c1*vol + c0;
                     force = phy;
                 }
                 var raws = new double[] { voltage, temp };
                 var phys = new double[] { force };
                 
                 rawData.Data = new SensorData(raws, phys, phys)
                 {
                     JsonResultData = string.Format("{0}\"sensorId\":{1},\"data\":\"电压:{2} V, 温度:{3} ℃\"{4}", '{', rawData.Sensor.SensorID, voltage, temp, '}')
                 }; 
                 //rawData.Data = new MagneticFluxData(voltage, temp, force)
                 //{
                 //    JsonResultData =
                 //        string.Format("{0}\"sensorId\":{1},\"data\":\"电压:{2} V, 温度:{3} ℃\"{4}", '{',
                 //            rawData.Sensor.SensorID, voltage, temp, '}')
                 //};
             }
             catch (Exception ex)
             {
                 rawData.ErrorCode = (int)Errors.ERR_DATA_PARSEFAILED;
                 _logger.ErrorFormat("ctl sensor [Id:{0} m: {1} ch:{2}] parsedfailed,received bytes{3}, ERROR: {4}",
                         rawData.Sensor.SensorID, rawData.Sensor.ModuleNo, rawData.Sensor.ChannelNo,
                         ValueHelper.BytesToHexStr(rawData.Response), ex.Message);
             }
         }
Ejemplo n.º 2
0
        public void ParseResult(ref SensorAcqResult rawData)
        {
            byte[] data = rawData.Response;
            rawData.ErrorCode = IsValid(data);
            if ((int)Errors.SUCCESS != rawData.ErrorCode)
            {
                return;
            }
            int module = ValueHelper.GetShort_BE(data, IDX_SOURCE);

            if (module != rawData.Sensor.ModuleNo)
            {
                rawData.ErrorCode = (int)Errors.ERR_INVALID_MODULE;
                return;
            }
            int channel = Convert.ToInt16(data[IDX_CONTENT]);

            if (channel != rawData.Sensor.ChannelNo)
            {
                rawData.ErrorCode = (int)Errors.ERR_RECEIVED_CH;
                return;
            }
            try
            {
                float  frequency        = ValueHelper.GetFloat(data, IDX_FREDATA);
                float  temperature      = ValueHelper.GetFloat(data, IDX_TEMPDATA);
                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:
                    physicalQuantity = (k * (Math.Pow(frequency, 2) - Math.Pow(f0, 2)) + kt * (temperature - t0));
                    colphy           = physicalQuantity;
                    break;

                case 7:

                    double H = rawData.Sensor.Parameters[4].Value;
                    var    P = (k * (Math.Pow(f0, 2) - Math.Pow(frequency, 2)) + kt * (temperature - t0));
                    var    h = P * 1000 / 9.8f;
                    physicalQuantity = (H + h);
                    colphy           = P;
                    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 };
                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("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);
            }
        }