Exemple #1
0
        /// <summary>
        /// 非卫星报文数据解析过程
        /// </summary>
        /// <param name="msg">原始报文数据</param>
        /// <param name="report">报文最终解析出的结果数据结构</param>
        /// <returns>是否解析成功</returns>
        public bool Parse(String msg, out CReportStruct report)
        {
            //$      1900020919060308101G21??????????1066@@  QTFM0164        11     0.163     1.171     2.546     0.000     0.334     0.000     0.000    -999.0      -0.2       0.0       0.0      14.7       1.5        99**
            report = new CReportStruct();
            List <CReportData> dataList = new List <CReportData>();

            try
            {
                string data = string.Empty;
                //去除起始符'$'
                if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data))
                {
                    return(false);
                }
                //站号(4位)
                string StationId = data.Substring(0, 10).Trim();
                //类别(长度):1G
                string length = data.Substring(10, 4);

                DateTime recvTime;
                recvTime = new DateTime(
                    year: Int32.Parse("20" + data.Substring(14, 2)),
                    month: Int32.Parse(data.Substring(16, 2)),
                    day: Int32.Parse(data.Substring(18, 2)),
                    hour: Int32.Parse(data.Substring(20, 2)),
                    minute: Int32.Parse(data.Substring(22, 2)),
                    second: 0
                    );
                //上下行标志
                string type = data.Substring(24, 2);

                //报类(2位):22-定时报
                string reportTypeString = data.Substring(26, 2);

                //水位
                string  waterStr = data.Substring(28, 6);
                Decimal?water;
                if (waterStr.Contains("?"))
                {
                    water = null;
                }
                else
                {
                    water = Decimal.Parse(waterStr) / 100;
                }

                //雨量
                string  rainStr = data.Substring(34, 4);
                Decimal?rain;
                if (rainStr.Contains("?"))
                {
                    rain = null;
                }
                else
                {
                    rain = Decimal.Parse(rainStr) / 100;
                }
                //电压
                string  voltageStr = data.Substring(38, 4);
                Decimal?voltage;
                if (voltageStr.Contains("?"))
                {
                    voltage = null;
                }
                else
                {
                    voltage = Decimal.Parse(voltageStr) / 100;
                }

                //报类
                EMessageType reportType;
                reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                //站类
                EStationType stationType;
                stationType = EStationType.EO;
                string allElmtData = data.Substring(42);

                //1.处理时差法数据
                CReportData speedData = new CReportData();
                int         flagIndex = allElmtData.IndexOf("@@  QTFM");
                if (flagIndex >= 0)
                {
                    int    keyLength = int.Parse(allElmtData.Substring(8, 4));
                    string elmtData  = allElmtData.Substring(flagIndex, keyLength);
                    //判定要素1的开始符号和结束符号
                    if (elmtData.StartsWith("@@  QTFM") && elmtData.EndsWith("**"))
                    {
                        elmtData = elmtData.Substring(12, keyLength - 14).Trim();
                        //判定时差法数据的开始符号和接受符号
                        if (elmtData.StartsWith("11") && elmtData.EndsWith("99"))
                        {
                            try
                            {
                                elmtData = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(elmtData, " ");
                                string[] elmtDataList = elmtData.Split(' ');
                                speedData.Voltge    = voltage;
                                speedData.Vm        = Decimal.Parse(elmtDataList[1]);
                                speedData.W1        = Decimal.Parse(elmtDataList[2]);
                                speedData.Q         = Decimal.Parse(elmtDataList[3]);
                                speedData.v1        = Decimal.Parse(elmtDataList[4]);
                                speedData.v2        = Decimal.Parse(elmtDataList[5]);
                                speedData.v3        = Decimal.Parse(elmtDataList[6]);
                                speedData.v4        = Decimal.Parse(elmtDataList[7]);
                                speedData.beta1     = Decimal.Parse(elmtDataList[8]);
                                speedData.beta2     = Decimal.Parse(elmtDataList[9]);
                                speedData.beta3     = Decimal.Parse(elmtDataList[10]);
                                speedData.beta4     = Decimal.Parse(elmtDataList[11]);
                                speedData.W2        = Decimal.Parse(elmtDataList[12]);
                                speedData.errorCode = elmtDataList[13];
                                dataList.Add(speedData);
                            }
                            catch (Exception ee)
                            {
                                //解析失败
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                            //11开头  99结束
                        }
                        report.Stationid   = StationId;
                        report.Type        = type;
                        report.ReportType  = reportType;
                        report.StationType = stationType;
                        report.RecvTime    = recvTime;
                        report.Datas       = dataList;
                    }
                    else
                    {
                        return(false);
                        //TODO 要素1开始符号和结束符合不匹配
                    }
                }
            }
            catch (Exception eee)
            {
                return(false);
            }
            return(true);
        }
Exemple #2
0
        public bool Parse(string msg, out CReportStruct report)
        {
            report = new CReportStruct();
            List <CReportData>    dataList     = new List <CReportData>();
            List <CReportPwdData> dataPwdList  = new List <CReportPwdData>();
            List <CReportFsfx>    dataFsfxList = new List <CReportFsfx>();

            try
            {
                string data = string.Empty;
                //去除起始符'$'
                if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data))
                {
                    return(false);
                }
                //站号(4位)
                string StationId = data.Substring(0, 10).Trim();
                //长度:1G
                string length = data.Substring(10, 4);

                DateTime recvTime;
                recvTime = new DateTime(
                    year: Int32.Parse("20" + data.Substring(14, 2)),
                    month: Int32.Parse(data.Substring(16, 2)),
                    day: Int32.Parse(data.Substring(18, 2)),
                    hour: Int32.Parse(data.Substring(20, 2)),
                    minute: Int32.Parse(data.Substring(22, 2)),
                    second: 0
                    );
                //上下行标志
                string type = data.Substring(24, 2);

                //报类(2位):22-定时报
                string reportTypeString = data.Substring(26, 2);

                //站类
                string stationTypeString = data.Substring(28, 2);

                //水位
                string  waterStr = data.Substring(30, 6);
                Decimal?water;
                if (waterStr.Contains("?"))
                {
                    water = null;
                }
                else
                {
                    water = Decimal.Parse(waterStr) / 100;
                }

                //雨量
                string  rainStr = data.Substring(36, 4);
                Decimal?rain;
                if (rainStr.Contains("?"))
                {
                    rain = null;
                }
                else
                {
                    rain = Decimal.Parse(rainStr) / 100;
                }
                //电压
                string  voltageStr = data.Substring(40, 4);
                Decimal?voltage;
                if (voltageStr.Contains("?"))
                {
                    voltage = null;
                }
                else
                {
                    voltage = Decimal.Parse(voltageStr) / 100;
                }

                //报类
                EMessageType reportType;
                reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                //站类
                EStationType stationType;
                stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString);

                report.Stationid   = StationId;
                report.Type        = type;
                report.ReportType  = reportType;
                report.StationType = stationType;
                report.RecvTime    = recvTime;

                string allElmtData = data.Substring(44);
                //1.处理风速风向数据和散射仪数据
                CReportData    speedData = new CReportData();
                CReportPwdData pwdData   = new CReportPwdData();
                CReportFsfx    fsfxData  = new CReportFsfx();
                //1.1 风速风向信息
                int flagIndex = allElmtData.IndexOf("@@  EN2B");
                if (flagIndex >= 0)
                {
                    int    keyLength = int.Parse(allElmtData.Substring(flagIndex + 8, 4));
                    string elmtData  = allElmtData.Substring(flagIndex, keyLength);
                    //判定要素1的开始符号和结束符号
                    if (elmtData.StartsWith("@@  EN2B") && elmtData.EndsWith("**"))
                    {
                        elmtData = elmtData.Substring(12, keyLength - 14).Trim();
                        //判定时差法数据的开始符号和接受符号
                        if (elmtData.Length == (keyLength - 14))
                        {
                            CReportFsfx dataFsfx = new CReportFsfx();
                            try
                            {
                                string strtflag  = elmtData.Substring(0, 1);  //开始标志
                                string stationid = elmtData.Substring(1, 5);  //站点ID
                                string msgTime   = elmtData.Substring(6, 12); //时间
                                string shfx      = elmtData.Substring(18, 4); //瞬时风向
                                string shfs      = elmtData.Substring(22, 4); //顺时风速
                                string yxszdshfx = elmtData.Substring(26, 4); //一小时最大瞬时风向
                                string yxszdshfs = elmtData.Substring(30, 4); //一小时最大瞬时风速
                                string maxTime   = elmtData.Substring(34, 4); //一小时最大瞬时风速出现时间
                                string avg2fx    = elmtData.Substring(38, 4); //2分钟平均风向
                                string avg2fs    = elmtData.Substring(42, 4); //2分钟平均风速
                                string avg10fx   = elmtData.Substring(46, 4); //10分钟平均风向
                                string avg10fs   = elmtData.Substring(50, 4); //10分钟平均风速
                                string max10fx   = elmtData.Substring(54, 4); //10分钟平均最大风向
                                string max10fs   = elmtData.Substring(58, 4); //10分钟平均最大风速
                                string max10tm   = elmtData.Substring(62, 4); //10分钟最大风速出现时间

                                fsfxData.Water     = water;
                                fsfxData.Time      = recvTime;
                                fsfxData.shfx      = decimal.Parse(shfx);
                                fsfxData.shfs      = decimal.Parse(shfx) / 10;
                                fsfxData.yxszdshfx = decimal.Parse(yxszdshfx);
                                fsfxData.yxszdshfs = decimal.Parse(shfx) / 10;
                                fsfxData.maxTime   = new DateTime(recvTime.Year, recvTime.Month, recvTime.Day, int.Parse(maxTime.Substring(0, 2)), int.Parse(maxTime.Substring(2, 2)), 0);
                                fsfxData.avg2fx    = decimal.Parse(avg2fx);
                                fsfxData.avg2fs    = decimal.Parse(avg2fs) / 10;
                                fsfxData.avg10fx   = decimal.Parse(avg10fx);
                                fsfxData.avg10fs   = decimal.Parse(avg10fs) / 10;
                                fsfxData.max10fx   = decimal.Parse(max10fx);
                                fsfxData.max10fs   = decimal.Parse(max10fs) / 10;
                                fsfxData.max10tm   = new DateTime(recvTime.Year, recvTime.Month, recvTime.Day, int.Parse(max10tm.Substring(0, 2)), int.Parse(max10tm.Substring(2, 2)), 0);
                                fsfxData.Voltge    = voltage;
                                dataFsfxList.Add(fsfxData);
                            }
                            catch (Exception e)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                            //11开头  99结束
                        }
                        report.fsfxDatas = dataFsfxList;
                    }
                    else
                    {
                        return(false);
                        //TODO 要素1开始符号和结束符合不匹配
                    }
                }
                //1.2 散射仪数据
                //015057203031023033202F2F2F2F2F2F202F2F2F2F2F030D0A
                int flagIndex2 = allElmtData.IndexOf("@@   PWD");
                if (flagIndex2 >= 0)
                {
                    int    keyLength = int.Parse(allElmtData.Substring(flagIndex2 + 8, 4));
                    string elmtData  = allElmtData.Substring(flagIndex2, keyLength);
                    if (elmtData.StartsWith("@@   PWD") && elmtData.EndsWith("**"))
                    {
                        elmtData = elmtData.Substring(12, keyLength - 14).Trim();
                        elmtData = elmtData.Substring(0, elmtData.Length - 1).Trim();
                        elmtData = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(elmtData, " ");
                        string[] elmtDataList = elmtData.Split(' ');
                        pwdData.Time = recvTime;
                        try
                        {
                            pwdData.Visi1min = decimal.Parse(elmtDataList[2].Trim());
                        }
                        catch
                        {
                            pwdData.Visi1min = null;
                        }
                        try
                        {
                            pwdData.Visi10min = decimal.Parse(elmtDataList[3]);
                        }
                        catch
                        {
                            pwdData.Visi10min = null;
                        }

                        pwdData.Voltge = voltage;
                        dataPwdList.Add(pwdData);
                    }
                    else
                    {
                        return(false);
                    }

                    report.pwdDatas = dataPwdList;
                    //report.Datas = dataList;
                }
            }
            catch (Exception eee)
            {
                return(false);
            }
            return(true);
        }
        public bool Parse(string msg, out CReportStruct report)
        {
            report = new CReportStruct();
            List <CReportData> dataList = new List <CReportData>();

            try
            {
                string data = string.Empty;
                //去除起始符'$'
                if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data))
                {
                    return(false);
                }
                //站号(4位)
                string StationId = data.Substring(0, 10).Trim();
                //长度:1G
                string length = data.Substring(10, 4);

                DateTime recvTime;
                recvTime = new DateTime(
                    year: Int32.Parse("20" + data.Substring(14, 2)),
                    month: Int32.Parse(data.Substring(16, 2)),
                    day: Int32.Parse(data.Substring(18, 2)),
                    hour: Int32.Parse(data.Substring(20, 2)),
                    minute: Int32.Parse(data.Substring(22, 2)),
                    second: 0
                    );
                //上下行标志
                string type = data.Substring(24, 2);

                //报类(2位):22-定时报
                string reportTypeString = data.Substring(26, 2);

                //站类
                string stationTypeString = data.Substring(28, 2);

                //水位
                string  waterStr = data.Substring(30, 6);
                Decimal?water;
                if (waterStr.Contains("?"))
                {
                    water = null;
                }
                else
                {
                    water = Decimal.Parse(waterStr) / 100;
                }

                //雨量
                string  rainStr = data.Substring(36, 4);
                Decimal?rain;
                if (rainStr.Contains("?"))
                {
                    rain = null;
                }
                else
                {
                    rain = Decimal.Parse(rainStr) / 100;
                }
                //电压
                string  voltageStr = data.Substring(40, 4);
                Decimal?voltage;
                if (voltageStr.Contains("?"))
                {
                    voltage = null;
                }
                else
                {
                    voltage = Decimal.Parse(voltageStr) / 100;
                }

                //报类
                EMessageType reportType;
                reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                //站类
                EStationType stationType;
                stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString);

                string allElmtData = data.Substring(44);

                //1.处理风速风向数据和散射仪数据
                CReportData speedData = new CReportData();
                //1.1 风速风向信息
                int flagIndex = allElmtData.IndexOf("@@  EN2B");
                if (flagIndex >= 0)
                {
                    int    keyLength = int.Parse(allElmtData.Substring(8, 4));
                    string elmtData  = allElmtData.Substring(flagIndex, keyLength);
                    //判定要素1的开始符号和结束符号
                    if (elmtData.StartsWith("@@  EN2B") && elmtData.EndsWith("**"))
                    {
                        elmtData = elmtData.Substring(12, keyLength - 14).Trim();
                        //判定时差法数据的开始符号和接受符号
                        if (elmtData.Length == (keyLength - 14))
                        {
                            try
                            {
                                string strtflag  = elmtData.Substring(0, 1);  //开始标志
                                string stationid = elmtData.Substring(1, 5);  //站点ID
                                string msgTime   = elmtData.Substring(6, 12); //时间
                                string shfx      = elmtData.Substring(18, 4); //瞬时风向
                                string shfs      = elmtData.Substring(22, 4); //顺时风速
                                string yxszdshfx = elmtData.Substring(26, 4); //一小时最大瞬时风向
                                string yxszdshfs = elmtData.Substring(30, 4); //一小时最大瞬时风速
                                string maxTime   = elmtData.Substring(34, 4); //一小时最大瞬时风速出现时间
                                string avg2fx    = elmtData.Substring(38, 4); //2分钟平均风向
                                string avg2fs    = elmtData.Substring(42, 4); //2分钟平均风速
                                string avg10fx   = elmtData.Substring(46, 4); //10分钟平均风向
                                string avg10fs   = elmtData.Substring(50, 4); //10分钟平均风速
                                string max10fx   = elmtData.Substring(54, 4); //10分钟平均最大风向
                                string max10fs   = elmtData.Substring(58, 4); //10分钟平均最大风速
                                string max10tm   = elmtData.Substring(62, 4); //10分钟最大风速出现时间
                            }
                            catch (Exception e)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                            //11开头  99结束
                        }
                        report.Stationid   = StationId;
                        report.Type        = type;
                        report.ReportType  = reportType;
                        report.StationType = stationType;
                        report.RecvTime    = recvTime;
                        report.Datas       = dataList;
                    }
                    else
                    {
                        return(false);
                        //TODO 要素1开始符号和结束符合不匹配
                    }
                }
                //1.2 散射仪数据
                int flagIndex2 = allElmtData.IndexOf("@@   PWD");
                if (flagIndex >= 0)
                {
                    int    keyLength = int.Parse(allElmtData.Substring(8, 4));
                    string elmtData  = allElmtData.Substring(flagIndex, keyLength);
                    if (elmtData.StartsWith("@@  EN2B") && elmtData.EndsWith("**"))
                    {
                    }
                }
            }
            catch (Exception eee)
            {
                return(false);
            }
            return(true);
        }
Exemple #4
0
        public bool Parse(string msg, out CReportStruct report)
        {
            report = new CReportStruct();
            List <CReportData> dataList = new List <CReportData>();

            try
            {
                string data = string.Empty;
                //去除起始符'$'
                if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data))
                {
                    return(false);
                }
                //站号(4位)
                string StationId = data.Substring(0, 10).Trim();
                //长度:1G
                string length = data.Substring(10, 4);

                DateTime recvTime;
                recvTime = new DateTime(
                    year: Int32.Parse("20" + data.Substring(14, 2)),
                    month: Int32.Parse(data.Substring(16, 2)),
                    day: Int32.Parse(data.Substring(18, 2)),
                    hour: Int32.Parse(data.Substring(20, 2)),
                    minute: Int32.Parse(data.Substring(22, 2)),
                    second: 0
                    );
                //上下行标志
                string type = data.Substring(24, 2);

                //报类(2位):22-定时报
                string reportTypeString = data.Substring(26, 2);

                //站类
                string stationTypeString = data.Substring(28, 2);

                //水位
                string  waterStr = data.Substring(30, 6);
                Decimal?water;
                if (waterStr.Contains("?"))
                {
                    water = null;
                }
                else
                {
                    water = Decimal.Parse(waterStr) / 100;
                }

                //雨量
                string  rainStr = data.Substring(36, 4);
                Decimal?rain;
                if (rainStr.Contains("?"))
                {
                    rain = null;
                }
                else
                {
                    rain = Decimal.Parse(rainStr) / 100;
                }
                //电压
                string  voltageStr = data.Substring(40, 4);
                Decimal?voltage;
                if (voltageStr.Contains("?"))
                {
                    voltage = null;
                }
                else
                {
                    voltage = Decimal.Parse(voltageStr) / 100;
                }

                //报类
                EMessageType reportType;
                reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                //站类
                EStationType stationType;
                stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString);

                string allElmtData = data.Substring(44);

                //1.处理SM100H数据
                CReportData speedData = new CReportData();
                int         flagIndex = allElmtData.IndexOf("@@SM100H");
                if (flagIndex >= 0)
                {
                    int    keyLength = int.Parse(allElmtData.Substring(8, 4));
                    string elmtData  = allElmtData.Substring(flagIndex, keyLength);
                    //判定要素1的开始符号和结束符号
                    if (elmtData.StartsWith("@@SM100H") && elmtData.EndsWith("**"))
                    {
                        elmtData = elmtData.Substring(12, keyLength - 14).Trim();
                        //判定时差法数据的开始符号和接受符号
                        if (elmtData.Length == 78)
                        {
                            try
                            {
                                string waterSpeedStr = elmtData.Substring(38, 8);
                                string waterFlowStr  = elmtData.Substring(48, 8);
                                string waterFlow2Str = elmtData.Substring(58, 8);

                                //字符串转16进制32位无符号整数
                                UInt32 waterSpeedInt = Convert.ToUInt32(waterSpeedStr, 16);
                                UInt32 waterFlowInt  = Convert.ToUInt32(waterFlowStr, 16);
                                UInt32 waterFlow2Int = Convert.ToUInt32(waterFlow2Str, 16);

                                //IEEE754 字节转换float
                                float waterSpeed = BitConverter.ToSingle(BitConverter.GetBytes(waterSpeedInt), 0);
                                float waterFlow  = BitConverter.ToSingle(BitConverter.GetBytes(waterFlowInt), 0);
                                float waterFlow2 = BitConverter.ToSingle(BitConverter.GetBytes(waterFlow2Int), 0);

                                //UInt32 x = Convert.ToUInt32(waterFlowStr, 16);//字符串转16进制32位无符号整数
                                //float fy = BitConverter.ToSingle(BitConverter.GetBytes(x), 0);//IEEE754 字节转换float
                                //UInt32 x2 = Convert.ToUInt32(waterFlow2Str, 16);//字符串转16进制32位无符号整数
                                //float fy2 = BitConverter.ToSingle(BitConverter.GetBytes(x2), 0);//IEEE754 字节转换float
                                speedData.v1     = (decimal?)waterSpeed;
                                speedData.Q      = (decimal?)waterFlow;
                                speedData.Q2     = (decimal?)waterFlow2;
                                speedData.Voltge = voltage;
                                dataList.Add(speedData);
                            }
                            catch (Exception e)
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                            //11开头  99结束
                        }
                        report.Stationid   = StationId;
                        report.Type        = type;
                        report.ReportType  = reportType;
                        report.StationType = stationType;
                        report.RecvTime    = recvTime;
                        report.Datas       = dataList;
                    }
                    else
                    {
                        return(false);
                        //TODO 要素1开始符号和结束符合不匹配
                    }
                }
            }
            catch (Exception eee)
            {
                return(false);
            }
            return(true);
        }
Exemple #5
0
        public bool Parse(string msg, out CReportStruct report)
        {
            report = new CReportStruct();
            //List<CReportData> dataList = new List<CReportData>();
            List <CReportObs> dataList = new List <CReportObs>();

            try
            {
                string data = string.Empty;
                //去除起始符'$'
                if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data))
                {
                    return(false);
                }
                //站号(4位)
                string StationId = data.Substring(0, 10).Trim();
                //长度:1G
                string length = data.Substring(10, 4);

                DateTime recvTime;
                recvTime = new DateTime(
                    year: Int32.Parse("20" + data.Substring(14, 2)),
                    month: Int32.Parse(data.Substring(16, 2)),
                    day: Int32.Parse(data.Substring(18, 2)),
                    hour: Int32.Parse(data.Substring(20, 2)),
                    minute: Int32.Parse(data.Substring(22, 2)),
                    second: 0
                    );
                //上下行标志
                string type = data.Substring(24, 2);

                //报类(2位):22-定时报
                string reportTypeString = data.Substring(26, 2);

                //站类
                string stationTypeString = data.Substring(28, 2);

                //水位
                string  waterStr = data.Substring(30, 6);
                Decimal?water;
                if (waterStr.Contains("?"))
                {
                    water = null;
                }
                else
                {
                    water = Decimal.Parse(waterStr) / 100;
                }

                //雨量
                string  rainStr = data.Substring(36, 4);
                Decimal?rain;
                if (rainStr.Contains("?"))
                {
                    rain = null;
                }
                else
                {
                    rain = Decimal.Parse(rainStr) / 100;
                }
                //电压
                string  voltageStr = data.Substring(40, 4);
                Decimal?voltage;
                if (voltageStr.Contains("?"))
                {
                    voltage = null;
                }
                else
                {
                    voltage = Decimal.Parse(voltageStr) / 100;
                }

                //报类
                EMessageType reportType;
                reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                //站类
                EStationType stationType;
                stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString);

                string allElmtData = data.Substring(44);

                //1.处理obs信息
                //CReportData speedData = new CReportData();
                CReportObs obsData = new CReportObs();
                //1.1 obs信息
                int flagIndex = allElmtData.IndexOf("@@ OBS3A");

                int    keyLength = int.Parse(allElmtData.Substring(8, 4));
                string elmtData  = allElmtData.Substring(flagIndex, keyLength);
                //判定要素1的开始符号和结束符号
                if (elmtData.StartsWith("@@ OBS3A") && elmtData.EndsWith("**"))
                {
                    elmtData = elmtData.Substring(12, keyLength - 14).Trim();
                    //判定时差法数据的开始符号和接受符号

                    try
                    {
                        elmtData = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(elmtData, " ");
                        string[] elmtDataList = elmtData.Split(' ');
                        if (elmtDataList.Length == 10)
                        {
                            decimal?deepth   = decimal.Parse(elmtDataList[2]);
                            decimal?ntu      = decimal.Parse(elmtDataList[3]);
                            decimal?mud      = decimal.Parse(elmtDataList[4]);
                            decimal?tmp      = decimal.Parse(elmtDataList[5]);
                            decimal?cndcty   = decimal.Parse(elmtDataList[6]);
                            decimal?salinity = decimal.Parse(elmtDataList[7]);
                            decimal?batt     = decimal.Parse(elmtDataList[8]);
                            obsData.Depth    = deepth;
                            obsData.Ntu      = ntu;
                            obsData.Mud      = mud;
                            obsData.Tmp      = tmp;
                            obsData.Cndcty   = cndcty;
                            obsData.Salinity = salinity;
                            obsData.Batt     = batt;
                            dataList.Add(obsData);
                        }
                        else
                        {
                            return(false);
                        }
                    }
                    catch (Exception e)
                    {
                        return(false);
                    }



                    report.Stationid   = StationId;
                    report.Type        = type;
                    report.ReportType  = reportType;
                    report.StationType = stationType;
                    report.RecvTime    = recvTime;
                    report.obsDatas    = dataList;
                }
                else
                {
                    return(false);
                    //TODO 要素1开始符号和结束符合不匹配
                }
            }
            catch (Exception eee)
            {
                return(false);
            }
            return(true);
        }
Exemple #6
0
        /// <summary>
        /// 非卫星报文数据解析过程
        /// </summary>
        /// <param name="msg">原始报文数据</param>
        /// <param name="report">报文最终解析出的结果数据结构</param>
        /// <returns>是否解析成功</returns>
        public bool Parse(String msg, out CReportStruct report)
        {
            //2019年07月04日08时53分24秒(流速中,最高位为符号位,正数最高位为0,负数最高位为 -,小数位为三位!!!)
            //$      0001010919070408401G2172??????????1432@@  RG300062000195000000000000000000........................**
            //$      0001010919070408401G2172??????????1431@@  RG300062000195000000000000000000........................**
            //$      0001010919070408451G2172??????????1432@@  RG300062000000000000000000000000........................**
            //$      0001010919070408451G2172??????????1432@@  RG300062000000000000000000000000........................**
            //$      0001010919070408501G2172??????????1430@@  RG300062000000000000000000000000........................**
            report = new CReportStruct();
            List <CReportData> dataList = new List <CReportData>();

            try
            {
                string data = string.Empty;
                //去除起始符'$'
                if (!ProtocolHelpers.DeleteSpecialCharLong(msg, out data))
                {
                    return(false);
                }
                //站号(4位)
                string StationId = data.Substring(0, 10).Trim();
                //长度:1G
                string length = data.Substring(10, 4);

                DateTime recvTime;
                recvTime = new DateTime(
                    year: Int32.Parse("20" + data.Substring(14, 2)),
                    month: Int32.Parse(data.Substring(16, 2)),
                    day: Int32.Parse(data.Substring(18, 2)),
                    hour: Int32.Parse(data.Substring(20, 2)),
                    minute: Int32.Parse(data.Substring(22, 2)),
                    second: 0
                    );
                //上下行标志
                string type = data.Substring(24, 2);

                //报类(2位):22-定时报
                string reportTypeString = data.Substring(26, 2);

                //站类
                string stationTypeString = data.Substring(28, 2);

                //水位
                string  waterStr = data.Substring(30, 6);
                Decimal?water;
                if (waterStr.Contains("?"))
                {
                    water = null;
                }
                else
                {
                    water = Decimal.Parse(waterStr) / 100;
                }

                //雨量
                string  rainStr = data.Substring(36, 4);
                Decimal?rain;
                if (rainStr.Contains("?"))
                {
                    rain = null;
                }
                else
                {
                    rain = Decimal.Parse(rainStr) / 100;
                }
                //电压
                string  voltageStr = data.Substring(40, 4);
                Decimal?voltage;
                if (voltageStr.Contains("?"))
                {
                    voltage = null;
                }
                else
                {
                    voltage = Decimal.Parse(voltageStr) / 100;
                }

                //报类
                EMessageType reportType;
                reportType = ProtocolMaps.MessageTypeMap.FindKey(reportTypeString);
                //站类
                EStationType stationType;
                stationType = ProtocolMaps.StationTypeMap.FindKey(stationTypeString);

                string allElmtData = data.Substring(44);

                //1.处理RG30数据
                CReportData speedData = new CReportData();
                int         flagIndex = allElmtData.IndexOf("@@  RG30");
                if (flagIndex >= 0)
                {
                    int    keyLength = int.Parse(allElmtData.Substring(8, 4));
                    string elmtData  = allElmtData.Substring(flagIndex, keyLength);
                    //判定要素1的开始符号和结束符号
                    if (elmtData.StartsWith("@@  RG30") && elmtData.EndsWith("**"))
                    {
                        elmtData = elmtData.Substring(12, keyLength - 14).Trim();
                        //判定时差法数据的开始符号和接受符号
                        if (elmtData.Length % 6 == 0)
                        {
                            List <string> elmtDataList           = new List <string>();
                            List <Nullable <Decimal> > speedList = new List <decimal?>();
                            for (int i = 0; i < elmtData.Length / 6; i++)
                            {
                                elmtDataList.Add(elmtData.Substring(i * 6, 6));
                            }
                            try
                            {
                                for (int i = 0; i < elmtDataList.Count; i++)
                                {
                                    if (elmtDataList[i].Contains(".") || elmtDataList[i].Contains("-"))
                                    {
                                        speedList.Add(null);
                                    }
                                    else
                                    {
                                        try
                                        {
                                            speedList.Add(decimal.Parse(elmtDataList[i]));
                                        }
                                        catch (Exception e)
                                        {
                                            speedList.Add(null);
                                        }
                                    }
                                }
                                try
                                {
                                    speedData.Rain   = rain;
                                    speedData.Water  = water;
                                    speedData.Voltge = voltage;
                                    speedData.v1     = speedList[0];
                                    speedData.v2     = speedList[1];
                                    speedData.v3     = speedList[2];
                                    speedData.v4     = speedList[3];
                                    speedData.v5     = speedList[4];
                                    speedData.v6     = speedList[5];
                                    speedData.v7     = speedList[6];
                                    speedData.v8     = speedList[7];
                                }catch (Exception ee)
                                {
                                }
                                dataList.Add(speedData);
                            }
                            catch (Exception ee)
                            {
                                //解析失败
                                return(false);
                            }
                        }
                        else
                        {
                            return(false);
                            //11开头  99结束
                        }
                        report.Stationid   = StationId;
                        report.Type        = type;
                        report.ReportType  = reportType;
                        report.StationType = stationType;
                        report.RecvTime    = recvTime;
                        report.Datas       = dataList;
                    }
                    else
                    {
                        return(false);
                        //TODO 要素1开始符号和结束符合不匹配
                    }
                }
            }
            catch (Exception eee)
            {
                return(false);
            }
            return(true);
        }