//private byte[] nextPacket = null;

        /// <summary>
        /// Create 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>
        private VelodyneReader(VelodyneSensorType type, ReturnMode returnMode, String indexFile, String pointFile)
        {
            this.IndexFile = indexFile;
            this.PointFile = pointFile;
            this.Sensor    = type;
            Indeces        = new List <IndexData>();

            if (!File.Exists(indexFile))
            {
                throw new FileNotFoundException("Index file does not exist!");
            }

            if (!File.Exists(pointFile))
            {
                throw new FileNotFoundException("Point file does not exist!");
            }

            if (type == VelodyneSensorType.HDL32E)
            {
                PacketInterpreter = new PacketInterpreterHDL32E();
            }
            else if (type == VelodyneSensorType.VLP16)
            {
                PacketInterpreter = new PacketInterpreterVLP16(returnMode);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
Example #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();
        }