Ejemplo n.º 1
0
        /// <summary>
        /// What to do when a complete block has arrived.
        /// Provided by derived class.
        /// </summary>
        /// <returns></returns>
        protected virtual bool BlockCompleted()
        {
            try
            {
                CNXLog.InfoFormat("Testing {0} length {1}", (Block)mBlockId, mMemStream.Length);
                if (mSize == 0)
                {
                    mCRCOk = true;
                }
                else if (mMemStream.Length >= mSize && mBytesWritten >= mSize)
                {
                    mMemStream.Seek(0, SeekOrigin.Begin);
                    ushort crc = CRC16.CRCStream(mMemStream, (int)mSize);
                    CNXLog.InfoFormat("CRC check calculated {0:X4} should be {1:X4}.", crc, mCRC);
                    mCRCOk = (crc == mCRC);
                }
                if (mCRCOk)
                {
                    CNXLog.InfoFormat("{0} complete.", (Block)mBlockId);
                }
                SendBlockQueryResponce();
            }
            catch (Exception e)
            {
                CNXLog.ErrorFormat("BlockComplete {0} {1}.", (Block)mBlockId, e.Message);
            }

            return(mCRCOk);
        }
Ejemplo n.º 2
0
        protected override bool BlockCompleted()
        {
            try
            {
                //CNXLog.InfoFormat("Testing block {0}", mBlockId);
                //if (mMemStream.Length == mSize && mBytesWritten >= mSize)
                //{
                //    mMemStream.Seek(0, SeekOrigin.Begin);
                //    ushort crc = CRC16.CRCStream(mMemStream);
                //    CNXLog.InfoFormat("CRC check calculated {0} should be {1}.", crc, mCRC);
                //    mCRCOk = (crc == mCRC);
                if (base.BlockCompleted() && mSize > 0)
                {
                    //CNXLog.InfoFormat("Block {0} complete.", mBlockId);
                    // transfere to permanat storage
                    CreateBlockFile();
                    mMemStream.WriteTo(mFileStream);
                    mFileStream.Flush();
                    mFileStream.Close();
                }
                //}
            }
            catch (Exception e)
            {
                CNXLog.ErrorFormat("BlockComplete {0} {1}.", (Block)mBlockId, e.Message);
            }

            return(mCRCOk);
        }
Ejemplo n.º 3
0
        private void CreateBlockFile()
        {
            try
            {
                mPath = String.Format("{0}blk{1}", ((Environment.OSVersion.Platform == System.PlatformID.Unix) ? LinuxBlockFilePath : MSBlockFilePath), mBlockId);
                //string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData

                // delete any old file from a previous instance and overwrite with a new one.
                //mFileStream = new FileStream(mPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, 8, FileOptions.RandomAccess);
                mFileStream = new FileStream(mPath, FileMode.Create, FileAccess.ReadWrite, FileShare.Read, 8192, FileOptions.RandomAccess);
            }
            catch (Exception e)
            {
                CNXLog.ErrorFormat("Block Reciever failed for path {0}. {1}", mPath, e.Message);
            }
        }
        /// <summary>
        /// Event firing method.
        /// </summary>
        /// <param name="frame">The CAN frame just received.</param>
        //protected virtual void OnRaiseFrameReceivedEvent(FrameReceivedEventArgs frameEvent)
        //{
        //    // copy the event handler to avoid mid process subscribe/un-subscribe
        //    EventHandler<FrameReceivedEventArgs> handler = RaiseFrameReceivedEvent;

        //    // Check if there are any Subscribers
        //    if (handler != null)
        //        // Call the Event
        //        handler(this, frameEvent);
        //}

        private void ReceiveFrames()
        {
            byte[]   buffer = new byte[13];
            EndPoint ep     = mRxEndPoint;
            CANFrame frame  = new CANFrame();

            while (mKeepReceiving)
            {
                try
                {
                    if (mRxSocket.ReceiveFrom(buffer, ref ep) > 0)
                    {
                        CNXLog.Debug(BitConverter.ToString(buffer));
                        // populate a CAN frame
                        frame.WireFormatArray = buffer;
                        CNXLog.Debug(BitConverter.ToString(frame.Data));
                        CNXLog.Debug(frame.MailboxId.ToString("X"));
                        OnRaiseFrameReceivedEvent(new FrameReceivedEventArgs(frame));
                    }
                }
                catch (SocketException se)
                {
                    // may be OK to continue.
                    CNXLog.WarnFormat("ReceiveFrames {0}.", se.Message);
                }
                catch (Exception e)
                {
                    // stuffed.
                    mKeepReceiving = false;
                    CNXLog.ErrorFormat("ReceiveFrames {0}.", e.Message);
                    break;
                }
            }

            mRxSocket.Close();
        }
Ejemplo n.º 5
0
        private void ProcessNewSentence(string sentence)
        {
            if (sentence.Length < 6)
            {
                OnRaiseGPSStatusChangedEvent(new GPSStatusEvent(GPS_STATUS.OFFLINE));
                CNXLog.ErrorFormat("NMEAClient short sentence {0}", sentence);
                return;
            }

            try
            {
                //CNXLog.InfoFormat("NMEAClient ProcessNewSentence {0}", sentence);
                bool   updateAvailable = false;
                string sentenceType    = "";

                if (sentence.Contains("RMC"))
                {
                    sentenceType = "RMC";
                }
                else if (sentence.Contains("GGA"))
                {
                    sentenceType = "GGA";
                }
                else if (sentence.Contains("GSA"))
                {
                    sentenceType = "GSA";
                }

                // Determine the type of sentence.
                // The NMEA specification states that the first two letters of a sentence may change.
                // For example, for "$GPGSV" there may be variations such as "$__GSV" where the first two letters change.
                // As a result, we need only test the last three characters.

                // Is this a GPRMC sentence?
                if (sentenceType.Equals("RMC", StringComparison.Ordinal))
                {
                    updateAvailable = mGPSPosition.ParseRMC(sentence);
                    if (!updateAvailable)
                    {
                        OnRaiseGPSPositionChangedEvent(new GPSPositionEvent(mGPSPosition));
                    }
                }
                else if (sentenceType.Equals("GGA", StringComparison.Ordinal))
                {
                    // dont update on GGA, only augment error estimates.
                    updateAvailable = mGPSPosition.ParseGGA(sentence);
                }
                else if (sentenceType.Equals("GSA", StringComparison.Ordinal))
                {
                    updateAvailable = mGPSPosition.ParseGSA(sentence);
                }

                if (updateAvailable)
                {
                    mLastNMEASentenceType = sentenceType;

                    // report any status changes
                    GPS_STATUS status = GPS_STATUS.OFFLINE;
                    switch (mGPSPosition.NMEAMode)
                    {
                    case GPSPosition.NMEAMODE.NO_FIX:
                        status           = GPS_STATUS.Connected;
                        mTravellingState = MovingState.Unknown;
                        break;

                    case GPSPosition.NMEAMODE.NO_MODE:
                        status           = GPS_STATUS.Connected | GPS_STATUS.GPSResponding;
                        mTravellingState = MovingState.Unknown;
                        break;

                    case GPSPosition.NMEAMODE.TWO_DIMENSION:
                    case GPSPosition.NMEAMODE.THREE_DIMENSION:
                        status = GPS_STATUS.Connected | GPS_STATUS.GPSResponding | GPS_STATUS.Fix;
                        // set the travelling state
                        mAverageVelocity.Value = mGPSPosition.SpeedOverGround;

                        //lat = mG
                        mTravellingState = (mAverageVelocity.Value > mMovingThreshold) ? MovingState.Moving : MovingState.Stationary;
                        break;
                    }
                    if (status != mStatus)
                    {
                        mStatus = status;
                        OnRaiseGPSStatusChangedEvent(new GPSStatusEvent(mStatus));
                        //Console.WriteLine("Fired Status Changed.");
                    }
                    // only update from RMC sentences as other sentences dont have date & time.
                    if (sentenceType.Equals("RMC", StringComparison.Ordinal))
                    {
                        //Console.WriteLine("Firing Position Changed.");
                        //CNXLog.InfoFormat("GPS State {0}, {1}", mStatus, mGPSPosition.CurrentPosition.ToString());
                        //if (mGPSPosition.Tag.EndsWith("RMC", StringComparison.Ordinal))
                        if ((mStatus & GPS_STATUS.Fix) == GPS_STATUS.Fix)
                        {
                            //CNXLog.InfoFormat(mGPSPosition.CurrentPosition.ToString());
                            OnRaiseGPSPositionChangedEvent(new GPSPositionEvent(mGPSPosition));
                            //CNXLog.InfoFormat("OnRaiseGPSPositionChangedEvent completed.");
                            //CNXLog.InfoFormat(mGPSPosition.CurrentPosition.ToString());
                            //Console.WriteLine("Fired Position Changed.");
                        }
                    }
                }
                //CNXLog.InfoFormat("GPS State {0}, {1}", mStatus, mGPSPosition.CurrentPosition.ToString());
            }
            catch (Exception e)
            {
                CNXLog.Error(String.Format("NMEAClient ProcessNewSentence {0}", sentence), e);
            }
        }