经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Scala » 查看文章
Scala解析MDF文件
来源:cnblogs  作者:精彩一生  时间:2018/10/22 16:26:19  对本文有异议
   1 import java.io._
   2 import java.nio.ByteBuffer
   3 
   4 import scala.collection.mutable.ListBuffer
   5 
   6 object Hello_mdf {
   7 
   8   def main(args: Array[String]) {
   9     var datFile = "D:\\Scala\\Dat\\00006004_251-4416EN12-ECU FI 0_1212_Knock and Pre-ignition control check on vehicle (Dynamic and diagnosis)_cold start 30C_tianmashan_00006004_2018-10-17_09-10-11_2018-10-17_09-21-22_sorted.dat";
  10     var fis = new FileInputStream(new File(datFile))
  11     val bytes = new Array[Byte](fis.available().toInt)
  12     fis.read(bytes)
  13     fis.close()
  14 
  15     var mdf = new MDF(bytes)
  16 
  17     var dict: Map[String, Channel] = Map()
  18     var channelTmep = new Channel;
  19     
  20     channelTmep = mdf.get("DFES_ctEntry")
  21     if(channelTmep == null)
  22       println("Signal is not found")
  23     else
  24       dict += ("DFES_ctEntry" -> mdf.get("DFES_ctEntry"))
  25 
  26     channelTmep = mdf.get("nmot_w")
  27     if(channelTmep == null)
  28       println("Signal is not found")
  29     else
  30       dict += ("nmot_w" -> mdf.get("nmot_w"))
  31 
  32     channelTmep = mdf.get("rl_w")
  33     if(channelTmep == null)
  34       println("Signal is not found")
  35     else
  36       dict += ("rl_w" -> mdf.get("rl_w"))
  37 
  38     channelTmep = mdf.get("EpmCaS_phiCaSOfsAvg_[0]")
  39     if(channelTmep == null)
  40       println("Signal is not found")
  41     else
  42       dict += ("EpmCaS_phiCaSOfsAvg_[0]" -> mdf.get("EpmCaS_phiCaSOfsAvg_[0]"))
  43     channelTmep = mdf.get("EpmCaS_phiCaSOfsAvg_[1]")
  44     if(channelTmep == null)
  45       println("Signal is not found")
  46     else
  47       dict += ("EpmCaS_phiCaSOfsAvg_[1]" -> mdf.get("EpmCaS_phiCaSOfsAvg_[1]"))
  48 
  49     channelTmep = mdf.get("fkmsdk_w")
  50     if(channelTmep == null)
  51       println("Signal is not found")
  52     else
  53       dict += ("fkmsdk_w" -> mdf.get("fkmsdk_w")) // 节气门自学习
  54 
  55     channelTmep = mdf.get("fra_w")
  56     if(channelTmep == null)
  57       println("Signal is not found")
  58     else
  59       dict += ("fra_w" -> mdf.get("fra_w"))
  60 
  61     channelTmep = mdf.get("uprm_w")
  62     if(channelTmep == null)
  63       println("Signal is not found")
  64     else
  65       dict += ("uprm_w" -> mdf.get("uprm_w"))
  66 
  67     channelTmep = mdf.get("MoF_rTrqInrAct")
  68     if(channelTmep == null)
  69       println("Signal is not found")
  70     else
  71       dict += ("MoF_rTrqInrAct" -> mdf.get("MoF_rTrqInrAct"))
  72 
  73     channelTmep = mdf.get("miist_w")
  74     if(channelTmep == null)
  75       println("Signal is not found")
  76     else
  77       dict += ("miist_w" -> mdf.get("miist_w"))
  78 
  79     channelTmep = mdf.get("rkrnv6_w_[0]")
  80     if(channelTmep == null)
  81       println("Signal is not found")
  82     else
  83       dict += ("rkrnv6_w_[0]" -> mdf.get("rkrnv6_w_[0]"))
  84     channelTmep = mdf.get("rkrnv6_w_[1]")
  85     if(channelTmep == null)
  86       println("Signal is not found")
  87     else
  88       dict += ("rkrnv6_w_[1]" -> mdf.get("rkrnv6_w_[1]"))
  89     channelTmep = mdf.get("rkrnv6_w_[2]")
  90     if(channelTmep == null)
  91       println("Signal is not found")
  92     else
  93     dict += ("rkrnv6_w_[2]" -> mdf.get("rkrnv6_w_[2]"))
  94     channelTmep = mdf.get("rkrnv6_w_[3]")
  95     if(channelTmep == null)
  96       println("Signal is not found")
  97     else
  98       dict += ("rkrnv6_w_[3]" -> mdf.get("rkrnv6_w_[3]"))
  99 
 100     channelTmep = mdf.get("B_kl")
 101     if(channelTmep == null)
 102       println("Signal is not found")
 103     else
 104       dict += ("B_kl" -> mdf.get("B_kl"))
 105 
 106 
 107     dict.keys.foreach {
 108       i => {
 109         println("*************" + i + "*******************")
 110         for (j <- 0 until 30) {
 111           println("Count: "+ j, " time: "+dict(i).time(j), " data: "+dict(i).data(j))
 112         }
 113         println("*****************************************")
 114       }
 115     }
 116 
 117     // make fault counter
 118     var faultCounter = makeEmptySeries(dict("nmot_w"))
 119 
 120     // var dict_nmotrl = p1
 121     var nmotSeries =makeSeries(dict("nmot_w"))
 122     var rlSeries =makeSeries(dict("rl_w"))
 123     var nmotRlSeries = makeXYSeries(dict("nmot_w"), dict("rl_w"))
 124     // epm
 125     var epm0Series =makeSeries(dict("EpmCaS_phiCaSOfsAvg_[0]"))
 126     var epm1Series =makeSeries(dict("EpmCaS_phiCaSOfsAvg_[1]"))
 127 
 128     var (epm0StatisX, epm0StatisY)  =makeDistribution(-10,10, 30, dict("EpmCaS_phiCaSOfsAvg_[0]"))
 129     var (epm1StatisX, epm1StatisY)  =makeDistribution(-10,10, 30, dict("EpmCaS_phiCaSOfsAvg_[0]"))
 130     // throttle
 131     var fkmsdkSeries =makeSeries(dict("fkmsdk_w"))
 132     var (fkmsdkStatisX, fkmsdkStatisY)  =makeDistribution(-10,10, 30, dict("fkmsdk_w"))
 133     // lambda
 134     var lambdaSeries = makeSeries(dict("fra_w"))
 135     var (lambdaSeriesStatisX, lambdaSeriesStatisY) = makeDistribution(-10,10, 30, dict("fra_w"))
 136     // hdfs
 137     var uprmSeries = makeSeries(dict("uprm_w"))
 138     var (uprmSeriesStatisX, uprmSeriesStatisY)  =makeDistribution(-10,10, 30, dict("uprm_w"))
 139     // safetyTorque
 140     var minusArray = makeMinusSeries(dict("MoF_rTrqInrAct"), dict("miist_w"))
 141     var (minusArrayX, minusArrayY) = makeDistributionMinus(-10,10, 30, dict("MoF_rTrqInrAct"), dict("miist_w"))
 142     // knock OverView
 143     // Noise Ref
 144     var noiseRef0 = makeSeries(dict("rkrnv6_w_[0]"))
 145     var noiseRef1 = makeSeries(dict("rkrnv6_w_[1]"))
 146     var noiseRef2 = makeSeries(dict("rkrnv6_w_[2]"))
 147     var noiseRef3 = makeSeries(dict("rkrnv6_w_[3]"))
 148 
 149 //    println(epm0StatisY.mkString(","))
 150     println("--------------------------------------done--------------------------------------")
 151   }
 152 
 153   def makeEmptySeries(_channel: Channel): List[Array[Double]]={
 154 
 155     var _series= new ListBuffer[Array[Double]]
 156 
 157     for (i <- 0 until _channel.time.length) {
 158 
 159       var _arr = new Array[Double](2)
 160       _arr(0) = _channel.time(i)
 161       _arr(1) = 0.0
 162       _series.append(_arr)
 163     }
 164 
 165     _series.toList
 166   }
 167 
 168   def makeSeries(_channel: Channel): List[Array[Double]]={
 169 
 170     var _series= new ListBuffer[Array[Double]]
 171 
 172     for (i <- 0 until _channel.time.length) {
 173 
 174       var _arr = new Array[Double](2)
 175       _arr(0) = _channel.time(i)
 176       _arr(1) = _channel.data(i)
 177       _series.append(_arr)
 178     }
 179 
 180     _series.toList
 181   }
 182 
 183   def makeMinusSeries(_channel: Channel, _channel2: Channel): List[Array[Double]]={
 184 
 185     var _series= new ListBuffer[Array[Double]]
 186 
 187     for (i <- 0 until _channel.time.length) {
 188 
 189       var _arr = new Array[Double](2)
 190       _arr(0) = _channel.time(i)
 191       _arr(1) = _channel.data(i)-_channel2.data(i)
 192       _series.append(_arr)
 193     }
 194 
 195     _series.toList
 196   }
 197 
 198   def makeXYSeries(_channel: Channel, _channel2: Channel): List[Array[Double]]={
 199 
 200     var _series= new ListBuffer[Array[Double]]
 201 
 202     for (i <- 0 until _channel.time.length) {
 203 
 204       var _arr = new Array[Double](2)
 205       _arr(0) = _channel.data(i)
 206       _arr(1) = _channel2.data(i)
 207       _series.append(_arr)
 208     }
 209 
 210     _series.toList
 211   }
 212 
 213   def makeDistribution(_min:Double, _max:Double, _bin:Int, _chan: Channel)={
 214 
 215     var _seriesX = new Array[Double](_bin)
 216     var _seriesY = new Array[Double](_bin)
 217 
 218     var x_delta = (_max-_min)/(_bin-1)
 219 
 220     for(i<-0 until _bin-1){
 221       _seriesX(i) = _min+i*x_delta
 222     }
 223     _seriesX(_seriesX.length-1) = _max
 224 
 225     for(i<-0 until _chan.data.length){
 226       for (j<-0 until _bin){
 227         if(_chan.data(i)>_seriesX(j)-x_delta/2 & _chan.data(i)<=_seriesX(j)+x_delta/2){
 228           _seriesY(j)+=1
 229         }
 230       }
 231     }
 232 
 233     (_seriesX, _seriesY)
 234 
 235   }
 236 
 237   def makeDistributionMinus(_min:Double, _max:Double, _bin:Int, _chan: Channel, _chan2:Channel) ={
 238 
 239     var _seriesX = new Array[Double](_bin)
 240     var _seriesY = new Array[Double](_bin)
 241 
 242     var x_delta = (_max-_min)/(_bin-1)
 243 
 244     for(i<-0 until _bin-1){
 245       _seriesX(i) = _min+i*x_delta
 246     }
 247     _seriesX(_seriesX.length-1) = _max
 248 
 249     for(i<-0 until _chan.data.length){
 250       for (j<-0 until _bin){
 251         if((_chan.data(i)-_chan2.data(i))>_seriesX(j)-x_delta/2 & (_chan.data(i)-_chan2.data(i))<=_seriesX(j)+x_delta/2){
 252           _seriesY(j)+=1
 253         }
 254       }
 255     }
 256     (_seriesX, _seriesY)
 257   }
 258 
 259   class MDF {
 260 
 261     var allBytes: Array[Byte] = _
 262     var idBlock: IDBlock = _
 263     var hdBlock: HDBlock = _
 264     var dgBlocks: List[DGBlock] = _
 265 
 266     def this(_bytes: Array[Byte]) {
 267 
 268       this()
 269       this.allBytes = _bytes
 270       this.idBlock = new IDBlock(_bytes)
 271       this.hdBlock = new HDBlock(_bytes)
 272 
 273       this.dgBlocks = List()
 274       var dg = new DGBlock(_bytes, hdBlock.pointerFirstDGBlock)
 275       dgBlocks = dgBlocks :+ dg
 276       while (dg.pointerNextDGBlock != 0) {
 277         dg = new DGBlock(_bytes, dg.pointerNextDGBlock)
 278         dgBlocks = dgBlocks :+ dg
 279       }
 280     }
 281 
 282     def get(_channelName: String): Channel = {
 283 
 284       for {
 285         i <- 0 until this.dgBlocks.length
 286         j <- 0 until this.dgBlocks(i).cgBlocks.length
 287         k <- 0 until this.dgBlocks(i).cgBlocks(j).cnBlocks.length
 288       } {
 289 
 290                 /*println("i: "+i)
 291                 println("j: "+j)
 292                 println("k: "+k)*/
 293           
 294         var shortSignalName = this.dgBlocks(i).cgBlocks(j).cnBlocks(k).shortSignalName
 295         var index = 0
 296         if(shortSignalName.contains("\\")){
 297             index = shortSignalName.indexOf("\\")
 298             shortSignalName = shortSignalName.substring(0,index)
 299         }
 300         else  if(shortSignalName.contains(":")){
 301             index = shortSignalName.indexOf(":")
 302             shortSignalName = shortSignalName.substring(0,index)
 303         }
 304         
 305         if (shortSignalName.equals(_channelName)) {
 306 //          println("shortSignalName: "+this.dgBlocks(i).cgBlocks(j).cnBlocks(k).shortSignalName)
 307           var _channel = new Channel()
 308 
 309           _channel.idxCGBlock = j
 310 
 311           _channel.cCBlock = new CCBlock(this.allBytes, this.dgBlocks(i).cgBlocks(j).cnBlocks(k).pointerCCBlock)
 312 
 313           var numberOfRecord = this.dgBlocks(i).cgBlocks(j).numberRecords
 314           //          println("numberOfRecord", numberOfRecord)
 315 
 316           var recordSizeInByte = this.dgBlocks(i).cgBlocks(j).sizeDataRecord
 317           //          println("recordSizeInByte", recordSizeInByte)
 318 
 319           var pointerDataBlock = this.dgBlocks(i).pointerDataBlock
 320           //          println("pointerDataBlock", pointerDataBlock)
 321           
 322           var signalDataType = this.dgBlocks(i).cgBlocks(j).cnBlocks(k).signalDataType
 323           var signalDataType0 = this.dgBlocks(i).cgBlocks(j).cnBlocks(0).signalDataType
 324           //          println("signalDataType", signalDataType)
 325 
 326           var numberOfBits = this.dgBlocks(i).cgBlocks(j).cnBlocks(k).numberOfBits
 327           var numberOfBits0 = this.dgBlocks(i).cgBlocks(j).cnBlocks(0).numberOfBits
 328           //          println("numberOfBits", numberOfBits)
 329 
 330           var startOffsetInBits = this.dgBlocks(i).cgBlocks(j).cnBlocks(k).startOffsetInBits
 331           var startOffsetInBits0 = this.dgBlocks(i).cgBlocks(j).cnBlocks(0).startOffsetInBits
 332           //          println("startOffsetInBits", startOffsetInBits)
 333 
 334           var shortSignalName = this.dgBlocks(i).cgBlocks(j).cnBlocks(k).shortSignalName
 335           var shortSignalName0 = this.dgBlocks(i).cgBlocks(j).cnBlocks(0).shortSignalName
 336           //          println("shortSignalName", shortSignalName)
 337 
 338           var buffer = new Array[Byte](numberOfRecord * recordSizeInByte)
 339           for (i <- 0 until buffer.length) {
 340             // data block from dg
 341             buffer(i) = this.allBytes(i + pointerDataBlock)
 342           }
 343           
 344           var rawData = new Array[Double](numberOfRecord)
 345           //          println(this.dgBlocks(i).cgBlocks(j).cnBlocks(0).shortSignalName)  // get time channel signal name
 346           var rawTime = new Array[Double](numberOfRecord)
 347 
 348           signalDataType match {
 349             case 0 => {
 350               //              println("signalDataType",signalDataType)
 351               if (numberOfBits == 1) {
 352                 //                println("numberOfBits",numberOfBits)
 353                 rawData = DataReaderBit(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 354               }
 355 
 356               if (numberOfBits / 8 == 1) {
 357                 if (shortSignalName.contains("$")) {
 358                 }
 359                 else {
 360                   //                  println("numberOfBits",numberOfBits,"OK")
 361                   rawData = DataReaderBit(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 362                 }
 363               }
 364 
 365               if (numberOfBits / 8 == 2) {
 366                 //                println("numberOfBits",numberOfBits)
 367                 rawData = DataReaderUInt16(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 368               }
 369 
 370               if (numberOfBits / 8 == 4) {
 371                 //                println("numberOfBits",numberOfBits)
 372                 rawData = DataReaderUInt32(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 373               }
 374               if (numberOfBits0 / 8 == 8) {
 375                 //                println("8")
 376                 rawTime = DataReaderUInt64(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 377               }
 378             }
 379 
 380             case 1 => {
 381               println("signalDataType"+signalDataType)
 382               if (numberOfBits / 8 == 1) {
 383                 if (shortSignalName.contains("$")) {
 384                 }
 385                 else {
 386                   rawData = DataReaderBit(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 387                 }
 388               }
 389               if (numberOfBits / 8 == 2) {
 390                 rawData = DataReaderUInt16(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 391               }
 392               if (numberOfBits / 8 == 4) {
 393                 rawData = DataReaderUInt32(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 394               }
 395             }
 396 
 397             case 2 => {
 398               println("signalDataType"+signalDataType)
 399               if (numberOfBits / 8 == 4) {
 400                 rawData = DataReaderFloat(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 401               }
 402             }
 403 
 404             case 3 => {
 405               println("signalDataType"+signalDataType)
 406               rawData = DataReaderDouble(buffer, startOffsetInBits, numberOfRecord, recordSizeInByte)
 407             }
 408 
 409             case _ => {
 410               println("signalDataType Error")
 411             }
 412           }
 413 
 414           signalDataType0 match {
 415             case 0 => {
 416               if (numberOfBits0 == 1) {
 417                 //                println("1")
 418                 rawTime = DataReaderBit(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 419               }
 420 
 421               if (numberOfBits0 / 8 == 1) {
 422                 if (shortSignalName.contains("$")) {
 423                 }
 424                 else {
 425                   //                  println("2")
 426                   rawTime = DataReaderBit(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 427                 }
 428               }
 429 
 430               if (numberOfBits0 / 8 == 2) {
 431                 //                println("3")
 432                 rawTime = DataReaderUInt16(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 433               }
 434 
 435               if (numberOfBits0 / 8 == 4) {
 436                 //                println("4")
 437                 rawTime = DataReaderUInt32(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 438               }
 439               
 440               if (numberOfBits0 / 8 == 8) {
 441                 //                println("8")
 442                 rawTime = DataReaderUInt64(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 443               }
 444             }
 445 
 446             case 1 => {
 447               if (numberOfBits0 / 8 == 1) {
 448                 if (shortSignalName.contains("$")) {
 449                 }
 450                 else {
 451                   println("5")
 452                   rawTime = DataReaderBit(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 453                 }
 454               }
 455               if (numberOfBits0 / 8 == 2) {
 456                 println("6")
 457                 rawTime = DataReaderUInt16(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 458               }
 459               if (numberOfBits0 / 8 == 4) {
 460                 println("7")
 461                 rawTime = DataReaderUInt32(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 462               }
 463             }
 464 
 465             case 2 => {
 466               if (numberOfBits0 / 8 == 4) {
 467                 println("8")
 468                 rawTime = DataReaderFloat(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 469               }
 470 //              if (numberOfBits0 / 8 == 8) {
 471 //                //                println("4")
 472 //                rawTime = DataReaderUInt64(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 473 //              }
 474             }
 475 
 476             case 3 => {
 477               println("9","startOffset: "+startOffsetInBits0,"Record: " + numberOfRecord,"recordSize: "+recordSizeInByte)
 478               rawTime = DataReaderDouble(buffer, startOffsetInBits0, numberOfRecord, recordSizeInByte)
 479             }
 480             case _ => {
 481               println("signalDataType0")
 482             }
 483           }
 484 
 485           //      case 4 =>{println("lack of signal data type 4")}
 486           //      case 5 =>{println("lack of signal data type 5")}
 487           //      case 6 =>{println("lack of signal data type 6")}
 488           //      case 7 =>{
 489           //        // null
 490           //      }
 491           //      case 8 =>{println("lack of signal data type 8")}
 492           //      case 9 =>{println("lack of signal data type 9")}
 493           //      case 10 =>{println("lack of signal data type 10")}
 494           //      case 11 =>{println("lack of signal data type 11")}
 495           //      case 12 =>{println("lack of signal data type 12")}
 496           //      case 13 =>{println("lack of signal data type 13")}
 497           //      case 14 =>{println("lack of signal data type 14")}
 498           //      case 15 =>{println("lack of signal data type 15")}
 499           //      case 16 =>{println("lack of signal data type 16")}
 500           //
 501           //          println("_channel.cCBlock.conversionType",_channel.cCBlock.conversionType)
 502           //          println("_channel.cCBlock.sizeInformation",_channel.cCBlock.sizeInformation)
 503           //          println("_channel.cCBlock.p1",_channel.cCBlock.p1)
 504           //          println("_channel.cCBlock.p2",_channel.cCBlock.p2)
 505 
 506           _channel.cCBlock.conversionType match {
 507             case 0 => {
 508               if (_channel.cCBlock.sizeInformation > 0) {
 509                 for (i <- 0 until rawData.length) {
 510                   rawData(i) = _channel.cCBlock.p1 + _channel.cCBlock.p2 * rawData(i)
 511                 }
 512               }
 513             }
 514 
 515             case 1 => {
 516               println("lack of cc type 1")
 517             }
 518             case 2 => {
 519               println("lack of cc type 2")
 520             }
 521             case 6 => {
 522               println("lack of cc type 6")
 523             }
 524             case 7 => {
 525               println("lack of cc type 7")
 526             }
 527             case 8 => {
 528               println("lack of cc type 8")
 529             }
 530             case 9 => {
 531               println("lack of cc type 9")
 532             }
 533             case 10 => {
 534               println("lack of cc type 10")
 535             }
 536             case 11 => {
 537               //              println(this.dgBlocks(i).cgBlocks(j).cnBlocks(k).shortSignalName)
 538               println("lack of cc type 11")
 539             }
 540             case 12 => {
 541               println("lack of cc type 12")
 542             }
 543             case 132 => {
 544               println("lack of cc type 132")
 545             }
 546             case 133 => {
 547               println("lack of cc type 133")
 548             }
 549             case _ => {
 550 
 551             }
 552           }
 553 
 554           var time_cc = new CCBlock(this.allBytes, this.dgBlocks(i).cgBlocks(j).cnBlocks(0).pointerCCBlock)
 555           //          println("time_cc.conversionType", time_cc.conversionType)
 556           //          println("time_cc.sizeInformation", time_cc.sizeInformation)
 557           //          println("time_cc.p1", time_cc.p1)
 558           //          println("time_cc.p2", time_cc.p2)
 559 
 560           time_cc.conversionType match {
 561             case 0 => {
 562               if (time_cc.sizeInformation > 0) {
 563                 for (i <- 0 until rawTime.length) {
 564                   rawTime(i) = time_cc.p1 + time_cc.p2 * rawTime(i)
 565                 }
 566 
 567               }
 568             }
 569           }
 570 
 571           //          for(i<-0 until 1000){
 572           //            println("time", i, rawTime(i))
 573           //          }
 574           _channel.time = rawTime
 575           _channel.data = rawData
 576           return _channel
 577         }
 578       }
 579       return null
 580     }
 581   }
 582 
 583   class Channel {
 584     var arrayBuffer: Array[Byte] = _
 585 
 586     var channelName = ""
 587     var idxCGBlock = 0
 588     var cNBlock: CNBlock = _
 589     var cCBlock: CCBlock = _
 590     var time: Array[Double] = _
 591     var data: Array[Double] = _
 592 
 593   }
 594 
 595   class IDBlock(var _bytes: Array[Byte]) {
 596 
 597     var FileIdentifier = ""
 598     var formatIdentifier = ""
 599     var programIdentifier = ""
 600     var defaultByteOrder = 0
 601     var defaultFloatingPointFormat = 0
 602     var versionNumber = 0
 603 
 604     this.makeIDBlock()
 605 
 606     def makeIDBlock(): Unit = {
 607 
 608       var offset = 0
 609       var len = 8
 610       this.FileIdentifier = bytes2str(_bytes, offset, len)
 611       offset += len
 612 
 613       len = 8
 614       this.formatIdentifier = bytes2str(_bytes, offset, len)
 615       //      println("this.formatIdentifier", this.formatIdentifier)
 616       offset += len
 617 
 618       len = 8
 619       this.programIdentifier = bytes2str(_bytes, offset, len)
 620       offset += len
 621 
 622       len = 2
 623       this.defaultByteOrder = bytes2uint16(_bytes, offset)
 624       offset += len
 625 
 626       len = 2
 627       this.defaultFloatingPointFormat = bytes2uint16(_bytes, offset)
 628       offset += len
 629 
 630       len = 2
 631       this.versionNumber = bytes2uint16(_bytes, offset)
 632     }
 633   }
 634 
 635   class HDBlock(var _bytes: Array[Byte]) {
 636     var blockTypeIdentifier = ""
 637     var blockSize = 0
 638     var pointerFirstDGBlock = 0
 639     var pointerFileComment = 0
 640     var pointerProgramBlock = 0
 641     var numberDGroups = 0
 642     var date = ""
 643     var time = ""
 644     var author = ""
 645     var organization = ""
 646     var project = ""
 647     var measureObject = ""
 648 
 649     this.makeHDBlock()
 650 
 651     def makeHDBlock(): Unit = {
 652 
 653       var offset = 64
 654       var len = 2
 655       this.blockTypeIdentifier = bytes2str(_bytes, offset, len)
 656       offset += len
 657 
 658       len = 2
 659       this.blockSize = bytes2uint16(_bytes, offset)
 660       offset += len
 661 
 662       len = 4
 663       this.pointerFirstDGBlock = bytes2uint32(_bytes, offset)
 664       offset += len
 665 
 666       len = 4
 667       this.pointerFileComment = bytes2uint32(_bytes, offset)
 668       offset += len
 669 
 670       len = 4
 671       this.pointerProgramBlock = bytes2uint32(_bytes, offset)
 672       offset += len
 673 
 674       len = 2
 675       this.numberDGroups = bytes2uint16(_bytes, offset)
 676       offset += len
 677 
 678       len = 10
 679       this.date = bytes2str(_bytes, offset, len)
 680       offset += len
 681 
 682       len = 8
 683       this.time = bytes2str(_bytes, offset, len)
 684       offset += len
 685 
 686       len = 32
 687       this.author = bytes2str(_bytes, offset, len)
 688       offset += len
 689 
 690       len = 32
 691       this.organization = bytes2str(_bytes, offset, len)
 692       offset += len
 693 
 694       len = 32
 695       this.project = bytes2str(_bytes, offset, len)
 696       offset += len
 697 
 698       len = 32
 699       this.measureObject = bytes2str(_bytes, offset, len)
 700       offset += len
 701 
 702     }
 703   }
 704 
 705   class TXBlock(var _bytes: Array[Byte], var _offset: Int) {
 706     var blockTypeIdentifier = ""
 707     var blockSize = 0
 708     var Text = ""
 709 
 710     this.makeTXBlock()
 711 
 712     def makeTXBlock(): Unit = {
 713       var offset = _offset
 714 
 715       var len = 2
 716 
 717       this.blockTypeIdentifier = bytes2str(_bytes, offset, len)
 718       offset += len
 719 
 720       len = 2
 721 
 722       this.blockSize = bytes2uint16(_bytes, offset)
 723       offset += len
 724 
 725       len = this.blockSize - 4
 726       this.Text = bytes2str(_bytes, offset, len)
 727       offset += len
 728     }
 729   }
 730     
 731     class CEBlock(var _bytes: Array[Byte], var _offset: Int) {
 732         var blockTypeIdentifier = ""
 733         var blockSize = 0
 734         var extensionType = 0
 735         var moduleNumber = 0
 736         var address = 0
 737         var description = ""
 738         var ECUIdentification = ""
 739         var CANMessage = 0
 740         var CANChannelIndex = 0
 741         var messageName = ""
 742         var senderName = ""
 743         
 744         this.makeCEBlock()
 745         
 746         def makeCEBlock(): Unit = {
 747             var offset = _offset
 748             
 749             var len = 2
 750             this.blockTypeIdentifier = bytes2str(_bytes, offset, len)
 751             offset += len
 752             
 753             len = 2
 754             this.blockSize = bytes2uint16(_bytes, offset)
 755             offset += len
 756     
 757             len = 2
 758             this.extensionType = bytes2uint16(_bytes, offset)
 759             offset += len
 760             
 761             if(this.extensionType == 2){
 762                 len = 2
 763                 this.moduleNumber = bytes2uint16(_bytes, offset)
 764                 offset += len
 765     
 766                 len = 4
 767                 this.address = bytes2uint32(_bytes, offset)
 768                 offset += len
 769     
 770                 len = 80
 771                 this.description = bytes2str(_bytes, offset, len)
 772                 offset += len
 773     
 774                 len = 32
 775                 this.ECUIdentification = bytes2str(_bytes, offset, len)
 776                 offset += len
 777             
 778             }
 779             else if(this.extensionType == 19){
 780                 len = 4
 781                 this.moduleNumber = bytes2uint32(_bytes, offset)
 782                 offset += len
 783     
 784                 len = 4
 785                 this.address = bytes2uint32(_bytes, offset)
 786                 offset += len
 787     
 788                 len = 36
 789                 this.description = bytes2str(_bytes, offset, len)
 790                 offset += len
 791     
 792                 len = 36
 793                 this.ECUIdentification = bytes2str(_bytes, offset, len)
 794                 offset += len
 795             }
 796         }
 797     }
 798 
 799   class DGBlock(var _bytes: Array[Byte], var _offset: Int) {
 800     var blockTypeIdentifier = ""
 801     var blockSize = 0
 802     var pointerNextDGBlock = 0
 803     var pointerFirstCGBlock = 0
 804     var pointerTriggerBlock = 0
 805     var pointerDataBlock = 0
 806     var numberCGs = 0
 807     var numberRecordIDs = 0
 808 
 809     var cgBlocks: List[CGBlock] = List()
 810 
 811     this.makeDGBlock()
 812 
 813     def makeDGBlock(): Unit = {
 814 
 815       var offset = _offset
 816       var len = 2
 817       this.blockTypeIdentifier = bytes2str(_bytes, offset, len)
 818       offset += len
 819 
 820       len = 2
 821       this.blockSize = bytes2uint16(_bytes, offset)
 822       offset += len
 823 
 824       len = 4
 825       this.pointerNextDGBlock = bytes2uint32(_bytes, offset)
 826       offset += len
 827 
 828       len = 4
 829       this.pointerFirstCGBlock = bytes2uint32(_bytes, offset)
 830       offset += len
 831 
 832       len = 4
 833       this.pointerTriggerBlock = bytes2uint32(_bytes, offset)
 834       offset += len
 835 
 836       len = 4
 837       this.pointerDataBlock = bytes2uint32(_bytes, offset)
 838       offset += len
 839 
 840       len = 2
 841       this.numberCGs = bytes2uint16(_bytes, offset)
 842       offset += len
 843 
 844       len = 10
 845       this.numberRecordIDs = bytes2uint16(_bytes, offset)
 846       offset += len
 847 
 848       var cg = new CGBlock(_bytes, this.pointerFirstCGBlock)
 849       cgBlocks = cgBlocks :+ cg
 850 
 851       while (cg.pointerNextCGBlock != 0) {
 852         cg = new CGBlock(_bytes, cg.pointerNextCGBlock)
 853         cgBlocks = cgBlocks :+ cg
 854       }
 855 
 856     }
 857   }
 858 
 859   class CGBlock(var _bytes: Array[Byte], var _offset: Int) {
 860     var blockTypeIdentifier = ""
 861     var blockSize = 0
 862     var pointerNextCGBlock = 0
 863     var pointerFirstCNBlock = 0
 864     var pointerComment = 0
 865     var recordID = 0
 866     var numberChannels = 0
 867     var sizeDataRecord = 0
 868     var numberRecords = 0
 869 
 870     var cnBlocks: List[CNBlock] = List()
 871 
 872     this.makeCGBlock()
 873 
 874     def makeCGBlock(): Unit = {
 875 
 876       var offset = _offset
 877       var len = 2
 878       this.blockTypeIdentifier = bytes2str(_bytes, offset, len)
 879       offset += len
 880 
 881       len = 2
 882       this.blockSize = bytes2uint16(_bytes, offset)
 883       offset += len
 884 
 885       len = 4
 886       this.pointerNextCGBlock = bytes2uint32(_bytes, offset)
 887       offset += len
 888 
 889       len = 4
 890       this.pointerFirstCNBlock = bytes2uint32(_bytes, offset)
 891       offset += len
 892 
 893       len = 4
 894       this.pointerComment = bytes2uint32(_bytes, offset)
 895       offset += len
 896 
 897       len = 2
 898       this.recordID = bytes2uint16(_bytes, offset)
 899       offset += len
 900 
 901       len = 2
 902       this.numberChannels = bytes2uint16(_bytes, offset)
 903       offset += len
 904 
 905       len = 2
 906       this.sizeDataRecord = bytes2uint16(_bytes, offset)
 907       offset += len
 908 
 909       len = 4
 910       this.numberRecords = bytes2uint32(_bytes, offset)
 911       offset += len
 912 
 913 
 914       var cn = new CNBlock(_bytes, this.pointerFirstCNBlock)
 915       cnBlocks = cnBlocks :+ cn
 916 
 917       while (cn.pointerNextCNBlock != 0) {
 918         cn = new CNBlock(_bytes, cn.pointerNextCNBlock)
 919         cnBlocks = cnBlocks :+ cn
 920       }
 921     }
 922   }
 923 
 924   class CNBlock(var _bytes: Array[Byte], var _offset: Int) {
 925     var blockTypeIdentifier = ""
 926     var blockSize = 0
 927     var pointerNextCNBlock = 0
 928     var pointerCCBlock = 0
 929     var pointerCEBlock = 0
 930     var pointerCDBlock = 0
 931     var pointerComment = 0
 932     var channelType = 0
 933     var shortSignalName = ""
 934     var signalDescription = ""
 935     var startOffsetInBits = 0
 936     var numberOfBits = 0
 937     var signalDataType = 0
 938     var valueRangeValid = false
 939     var minSignalValue = 0.0
 940     var maxSignalValue = 0.0
 941     var sampleRate = 0.0
 942     var pointerLongSignalName = 0
 943     var pointerDisplayName = 0
 944     var additionalByteOffset = 0
 945 
 946     this.makeCNBlock()
 947 
 948     var ccBlock = new CCBlock(_bytes, this.pointerCCBlock)
 949     var ceBlock = new CEBlock(_bytes, this.pointerCEBlock)
 950     //var txBlock = new TXBlock(_bytes, this.pointerComment)
 951 
 952     def makeCNBlock() = {
 953 
 954       var offset = _offset
 955       var len = 2
 956       this.blockTypeIdentifier = bytes2str(_bytes, offset, len)
 957       offset += len
 958 
 959       len = 2
 960       this.blockSize = bytes2uint16(_bytes, offset)
 961       offset += len
 962 
 963       len = 4
 964       this.pointerNextCNBlock = bytes2uint32(_bytes, offset)
 965       offset += len
 966 
 967       len = 4
 968       this.pointerCCBlock = bytes2uint32(_bytes, offset)
 969       offset += len
 970 
 971       len = 4
 972       this.pointerCEBlock = bytes2uint32(_bytes, offset)
 973       offset += len
 974 
 975       len = 4
 976       this.pointerCDBlock = bytes2uint32(_bytes, offset)
 977       offset += len
 978 
 979       len = 4
 980       this.pointerComment = bytes2uint32(_bytes, offset)
 981       offset += len
 982 
 983       len = 2
 984       this.channelType = bytes2uint16(_bytes, offset)
 985       offset += len
 986 
 987       len = 32
 988       this.shortSignalName = bytes2str(_bytes, offset, len)
 989       offset += len
 990 
 991       len = 128
 992       this.signalDescription = bytes2str(_bytes, offset, len)
 993       offset += len
 994 
 995       len = 2
 996       this.startOffsetInBits = bytes2uint16(_bytes, offset)
 997       offset += len
 998 
 999       len = 2
1000       this.numberOfBits = bytes2uint16(_bytes, offset)
1001       offset += len
1002 
1003       len = 2
1004       this.signalDataType = bytes2uint16(_bytes, offset)
1005       offset += len
1006 
1007       len = 2
1008       this.valueRangeValid = bytes2bool(_bytes, offset)
1009       offset += len
1010 
1011       len = 64
1012       this.minSignalValue = bytes2real(_bytes, offset)
1013       offset += len
1014 
1015       len = 64
1016       this.maxSignalValue = bytes2real(_bytes, offset)
1017       offset += len
1018 
1019       len = 64
1020       this.sampleRate = bytes2real(_bytes, offset)
1021       offset += len
1022 
1023       len = 4
1024       this.pointerLongSignalName = bytes2uint32(_bytes, offset)
1025       offset += len
1026 
1027       len = 4
1028       this.pointerDisplayName = bytes2uint32(_bytes, offset)
1029       offset += len
1030 
1031       len = 2
1032       this.additionalByteOffset = bytes2uint16(_bytes, offset)
1033       offset += len
1034     }
1035   }
1036 
1037   class CCBlock(var _bytes: Array[Byte], var _offset: Int) {
1038     var arrayBuffer: Array[Byte] = _
1039     var blockTypeIdentifier = ""
1040     var blockSize = 0
1041     var physicalValueRangeValid = false
1042     var minPhysicalSignalValue = 0.0
1043     var maxPhysicalSignalValue = 0.0
1044     var physicalUnit = ""
1045     var conversionType = 0
1046     var sizeInformation = 0
1047     var p1 = 0.0
1048     var p2 = 0.0
1049 
1050     //    var additionalConversionData
1051 
1052     this.makeCCBlock()
1053 
1054     def makeCCBlock() = {
1055 
1056       arrayBuffer = _bytes
1057       var offset = _offset
1058       var len = 2
1059       this.blockTypeIdentifier = bytes2str(_bytes, offset, len)
1060       //      println("this.blockTypeIdentifier",this.blockTypeIdentifier)
1061       offset += len
1062 
1063       len = 2
1064       this.blockSize = bytes2uint16(_bytes, offset)
1065       //      println("this.blockSize",this.blockSize)
1066       offset += len
1067 
1068       len = 2
1069       this.physicalValueRangeValid = bytes2bool(_bytes, offset)
1070       //      println("this.physicalValueRangeValid",this.physicalValueRangeValid)
1071       offset += len
1072 
1073       len = 8
1074       this.minPhysicalSignalValue = bytes2real(_bytes, offset)
1075       //      println("this.minPhysicalSignalValue",this.minPhysicalSignalValue)
1076       offset += len
1077 
1078       len = 8
1079       this.maxPhysicalSignalValue = bytes2real(_bytes, offset)
1080       //      println("this.maxPhysicalSignalValue",this.maxPhysicalSignalValue)
1081       offset += len
1082 
1083       len = 20
1084       this.physicalUnit = bytes2str(_bytes, offset, len)
1085       //      println("this.physicalUnit",this.physicalUnit)
1086       offset += len
1087 
1088       len = 2
1089       this.conversionType = bytes2uint16(_bytes, offset)
1090       //      println("this.conversionType",this.conversionType)
1091       offset += len
1092 
1093       len = 2
1094       this.sizeInformation = bytes2uint16(_bytes, offset)
1095       //      println("this.sizeInformation",this.sizeInformation)
1096       offset += len
1097 
1098       len = 8
1099       this.p1 = bytes2real(_bytes, offset)
1100       //      println("this.p1",this.p1)
1101       offset += len
1102 
1103       len = 8
1104       this.p2 = bytes2real(_bytes, offset)
1105       //      println("this.p2",this.p2)
1106       offset += len
1107 
1108     }
1109   }
1110 
1111 
1112   def bytes2str(_bytes: Array[Byte], _offset: Int, _len: Int): String = {
1113     var temp_bytes = new Array[Byte](_len)
1114     for (i <- 0 to _len - 1) {
1115       temp_bytes(i) = _bytes(i + _offset)
1116     }
1117 
1118     return new String(temp_bytes).trim()
1119   }
1120 
1121   def bytes2uint8(_bytes: Array[Byte], _offset: Int): Int = {
1122     var b0 = _bytes(_offset) & 0xff
1123 
1124     return b0
1125   }
1126 
1127   def bytes2uint16(_bytes: Array[Byte], _offset: Int): Int = {
1128     var b0 = _bytes(_offset) & 0xff
1129     var b1 = _bytes(_offset + 1) & 0xff
1130 
1131     return (b1 << 8) | b0
1132   }
1133 
1134   def bytes2uint32(_bytes: Array[Byte], _offset: Int): Int = {
1135 
1136     var b0 = _bytes(_offset) & 0xff
1137     var b1 = _bytes(_offset + 1) & 0xff
1138     var b2 = _bytes(_offset + 2) & 0xff
1139     var b3 = _bytes(_offset + 3) & 0xff
1140 
1141     return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0
1142   }
1143 
1144   def bytes2bool(_bytes: Array[Byte], _offset: Int): Boolean = {
1145     var b0 = _bytes(_offset) & 0xff
1146     var b1 = _bytes(_offset + 1) & 0xff
1147 
1148     return if (((b1 << 8) | b0) != 0) true else false
1149   }
1150 
1151   def bytes2float(_bytes: Array[Byte], _offset: Int): Double = {
1152 
1153     var _array: Array[Byte] = new Array[Byte](4)
1154 
1155     for (i <- 0 until 4) {
1156       _array(i) = _bytes(-i + _offset + 4)
1157     }
1158 
1159     return ByteBuffer.wrap(_array).getFloat()
1160 
1161     //    var b0 = _bytes(_offset) & 0xff
1162     //    var b1 = _bytes(_offset + 1) & 0xff
1163     //    var b2 = _bytes(_offset + 2) & 0xff
1164     //    var b3 = _bytes(_offset + 3) & 0xff
1165 
1166     //    return ((b3 << 24) | (b2 << 16) | (b1 << 8) | b0).toDouble
1167   }
1168 
1169   def bytes2real(_bytes: Array[Byte], _offset: Int): Double = {
1170 
1171     var _array: Array[Byte] = new Array[Byte](8)
1172 
1173     for (i <- 0 until 8) {
1174       _array(i) = _bytes(-i + _offset + 7)
1175     }
1176 
1177     //    var b0 = _bytes(_offset) & 0xff
1178     //    var b1 = _bytes(_offset + 1) & 0xff
1179     //    var b2 = _bytes(_offset + 2) & 0xff
1180     //    var b3 = _bytes(_offset + 3) & 0xff
1181     //    var b4 = _bytes(_offset + 4) & 0xff
1182     //    var b5 = _bytes(_offset + 5) & 0xff
1183     //    var b6 = _bytes(_offset + 6) & 0xff
1184     //    var b7 = _bytes(_offset + 7) & 0xff
1185 
1186     return ByteBuffer.wrap(_array).getDouble
1187 
1188     //    return ((b7 << 56) |(b6 << 48)|(b5 << 40)|(b4 << 32)|(b3 << 24)|(b2 << 16)|(b1 << 8)| b0).toDouble
1189   }
1190   
1191   def bytes2long(_bytes: Array[Byte], _offset: Int): Double = {
1192     
1193     var _array: Array[Byte] = new Array[Byte](8)
1194     
1195     for (i <- 0 until 8) {
1196       _array(i) = _bytes(-i + _offset + 7)
1197     }
1198     return ByteBuffer.wrap(_array).getLong
1199     
1200     //    return ((b7 << 56) |(b6 << 48)|(b5 << 40)|(b4 << 32)|(b3 << 24)|(b2 << 16)|(b1 << 8)| b0).toDouble
1201   }
1202 
1203   def DataReaderBit(_byte: Array[Byte], _startOffsetInBits: Int, _numberOfRecord: Int, _recordSizeInByte: Int): Array[Double] = {
1204     var startposition = _startOffsetInBits / 8
1205     var curPos = startposition
1206 
1207     var result: Array[Double] = new Array[Double](_numberOfRecord)
1208     for (i <- 0 until _numberOfRecord - 1) {
1209       result(i) = bytes2uint8(_byte, curPos)
1210       curPos += _recordSizeInByte
1211     }
1212     return result
1213   }
1214 
1215   def DataReaderUInt(_byte: Array[Byte], _startOffsetInBits: Int, _numberOfRecord: Int, _recordSizeInByte: Int): Array[Double] = {
1216     var startposition = _startOffsetInBits / 8
1217     var curPos = startposition
1218 
1219     var result: Array[Double] = new Array[Double](_numberOfRecord)
1220     for (i <- 0 until _numberOfRecord - 1) {
1221       result(i) = bytes2uint8(_byte, curPos)
1222       curPos += _recordSizeInByte
1223     }
1224 
1225     return result
1226   }
1227 
1228   def DataReaderUInt16(_byte: Array[Byte], _startOffsetInBits: Int, _numberOfRecord: Int, _recordSizeInByte: Int): Array[Double] = {
1229     var startposition = _startOffsetInBits / 8
1230     var curPos = startposition
1231 
1232     var result: Array[Double] = new Array[Double](_numberOfRecord)
1233 
1234     for (i <- 0 until _numberOfRecord - 1) {
1235       result(i) = bytes2uint16(_byte, curPos)
1236       curPos += _recordSizeInByte
1237     }
1238     return result
1239   }
1240   
1241   def DataReaderUInt32(_byte: Array[Byte], _startOffsetInBits: Int, _numberOfRecord: Int, _recordSizeInByte: Int): Array[Double] = {
1242     var startposition = _startOffsetInBits / 8
1243     var curPos = startposition
1244 
1245     var result: Array[Double] = new Array[Double](_numberOfRecord)
1246 
1247     for (i <- 0 until _numberOfRecord - 1) {
1248       result(i) = bytes2uint32(_byte, curPos)
1249       curPos += _recordSizeInByte
1250     }
1251     return result
1252   }
1253 
1254   def DataReaderUInt64(_byte: Array[Byte], _startOffsetInBits: Int, _numberOfRecord: Int, _recordSizeInByte: Int): Array[Double] = {
1255     var startposition = _startOffsetInBits / 8
1256     var curPos = startposition
1257 
1258     var result: Array[Double] = new Array[Double](_numberOfRecord)
1259 
1260     for (i <- 0 until _numberOfRecord - 1) {
1261       result(i) = bytes2long(_byte, curPos)
1262       curPos += _recordSizeInByte
1263     }
1264     return result
1265   }
1266 
1267   def DataReaderFloat(_byte: Array[Byte], _startOffsetInBits: Int, _numberOfRecord: Int, _recordSizeInByte: Int): Array[Double] = {
1268     var startposition = _startOffsetInBits / 8
1269     var curPos = startposition
1270     var result: Array[Double] = new Array[Double](_numberOfRecord)
1271 
1272     for (i <- 0 until _numberOfRecord - 1) {
1273       result(i) = bytes2float(_byte, curPos)
1274       curPos += _recordSizeInByte
1275     }
1276     return result
1277   }
1278 
1279   def DataReaderDouble(_byte: Array[Byte], _startOffsetInBits: Int, _numberOfRecord: Int, _recordSizeInByte: Int): Array[Double] = {
1280     var startposition = _startOffsetInBits / 8
1281     var curPos = startposition
1282 
1283     var result: Array[Double] = new Array[Double](_numberOfRecord)
1284 
1285     for (i <- 0 until _numberOfRecord - 1) {
1286       result(i) = bytes2real(_byte, curPos)
1287       curPos += _recordSizeInByte
1288     }
1289     return result
1290   }
1291 
1292 }
View Code

 友情链接: NPS  问卷模板