/// <summary>
        /// Open Velodyne reader object
        /// </summary>
        /// <param name="type">Type of the sensor</param>
        /// <param name="returnMode">Define return type for VLP-16, this variable is neglected for HDL-32E</param>
        /// <param name="indexFile">Index file path</param>
        /// <param name="pointFile">Point file path</param>
        /// <returns></returns>
        public static VelodyneReader Open(VelodyneSensorType type, ReturnMode returnMode, String indexFile, String pointFile)
        {
            VelodyneReader obj      = new VelodyneReader(type, returnMode, indexFile, pointFile);
            FileInfo       fi       = new FileInfo(indexFile);
            long           fileSize = fi.Length;

            obj.Indeces.Clear();

            byte[]       idxData = File.ReadAllBytes(obj.IndexFile);
            MemoryStream mem     = new MemoryStream(idxData);
            long         idx     = 0;

            using (BinaryReader reader = new BinaryReader(mem))
            {
                while (reader.BaseStream.Position != reader.BaseStream.Length)
                {
                    long     internalTimeTicks    = reader.ReadInt64();
                    DateTime internalTime         = new DateTime(internalTimeTicks);
                    long     packetTimeStampTicks = reader.ReadInt64();
                    DateTime packetTimeStamp      = new DateTime(packetTimeStampTicks);

                    long position = reader.ReadInt64();

                    byte[] nmeBytes = reader.ReadBytes(IndexData.NMEA_LENGTH);
                    String nmea     = Encoding.ASCII.GetString(nmeBytes);

                    obj.Indeces.Add(new IndexData(packetTimeStamp, internalTime, position, nmea));
                    idx++;

                    if (idx % 500 == 0)
                    {
                        ProgressReportEventArgs args = new ProgressReportEventArgs((((double)reader.BaseStream.Position / (double)fileSize) * 100.0), reader.BaseStream.Position, packetTimeStamp.ToUniversalTime());
                        OnReportProgress(args);
                    }
                }
            }

            obj.pointReader = new BinaryReader(File.Open(obj.PointFile, FileMode.Open));

            return(obj);
        }
Beispiel #2
0
        public void Convert(CancellationToken cancellationToken = default(CancellationToken))
        {
            FileInfo     fi        = new FileInfo(PcapFile);
            long         fileSize  = fi.Length;
            BinaryWriter idxWriter = new BinaryWriter(File.Open(GetDefaultIndexFile(PcapFile), FileMode.Create));

            OfflinePacketDevice selectedDevice = new OfflinePacketDevice(PcapFile);
            PacketCommunicator  communicator   =
                selectedDevice.Open(65536,                                  // portion of the packet to capture
                                                                            // 65536 guarantees that the whole packet will be captured on all the link layers
                                    PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                                    1000);                                  // read timeout


            using (BinaryWriter pointWriter = new BinaryWriter(File.Open(GetDefaultPointFile(PcapFile), FileMode.Create)))
            {
                Packet             packet;
                bool               isEof    = false;
                VelodyneNmeaPacket lastNmea = null;
                long               sumBytes = 0;
                long               indexId  = 0;

                while (!isEof)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        idxWriter.Close();
                        cancellationToken.ThrowIfCancellationRequested();
                    }

                    PacketCommunicatorReceiveResult result = communicator.ReceivePacket(out packet);
                    switch (result)
                    {
                    case PacketCommunicatorReceiveResult.Timeout:

                    case PacketCommunicatorReceiveResult.Ok:

                        sumBytes += packet.Length;

                        if (packet.Length == 554)
                        {
                            lastNmea = PacketInterpreter.ReadRecordNMEA(packet.Buffer);
                            indexId++;
                        }
                        else
                        {
                            pointWriter.Write(packet.Timestamp.Ticks);
                            pointWriter.Write(packet.Buffer);

                            if (lastNmea != null)
                            {
                                int      l                  = packet.Length - 6; // this is the end of the pack!
                                double   internal_time      = BitConverter.ToUInt32(new byte[] { packet[l], packet[l + 1], packet[l + 2], packet[l + 3] }, 0) / 1000000.00;
                                DateTime utcPacketTimestamp = packet.Timestamp.ToUniversalTime();
                                DateTime time               = (new DateTime(utcPacketTimestamp.Year, utcPacketTimestamp.Month, utcPacketTimestamp.Day, utcPacketTimestamp.Hour, 0, 0)).AddSeconds(internal_time);

                                idxWriter.Write(time.Ticks);
                                idxWriter.Write(packet.Timestamp.Ticks);
                                idxWriter.Write(pointWriter.BaseStream.Position);
                                byte[] nmea_byte = Encoding.ASCII.GetBytes(lastNmea.NmeaString.PadRight(NMEA_LENGTH, ' '));
                                idxWriter.Write(nmea_byte);

                                if (indexId % 100 == 0)
                                {
                                    ProgressReportEventArgs args = new ProgressReportEventArgs((((double)sumBytes / (double)fileSize) * 100.0), sumBytes, utcPacketTimestamp);
                                    OnReportProgress(args);
                                }

                                lastNmea = null;
                            }
                        }
                        break;

                    case PacketCommunicatorReceiveResult.Eof:
                        isEof = true;
                        break;

                    default:
                        throw new InvalidOperationException("The result " + result + " should never be reached here");
                    }
                }
            }

            idxWriter.Close();
        }
Beispiel #3
0
 protected virtual void OnReportProgress(ProgressReportEventArgs e)
 {
     ProgressReport?.Invoke(this, e);
 }
 protected static void OnReportProgress(ProgressReportEventArgs e)
 {
     ProgressReport?.Invoke(null, e);
 }