protected override bool BlockCompleted() { bool result = false; try { if (base.BlockCompleted()) { mBlockData = new byte[mSize]; if (mSize > 0) { Array.Copy(mMemStream.ToArray(), mBlockData, mSize); } else { OnRaiseBlockStatusEvent(new BlockStatusEventArgs(mBlockId, BlockState.COMPLETE)); } CNXLog.InfoFormat("TransientBlockReciever {0} Completed:\n\r{1}", (Block)mBlockId, HexDump.Dump(mBlockData)); result = true; } } catch (Exception e) { CNXLog.Error("TransientBlockReciever.BlockCompleted:", e); } return(result); }
///// <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 ReceiveFrame() { byte[] buffer = new byte[8]; CANFrame frame = new CANFrame(); int rxLen = 0; byte length = 0; uint canId = 0; while (mKeepReceiving) { try { rxLen = can_recieve(mCanFd, ref canId, ref length, buffer); if (rxLen > 0) { // populate a CAN frame frame.MailboxId = canId; frame.DataFromArray(buffer, 0, length); base.OnRaiseFrameReceivedEvent(new FrameReceivedEventArgs(frame)); } } catch (Exception e) { // stuffed. mKeepReceiving = false; CNXLog.Error("CANNative.ReceiveFrame", e); break; } } Console.WriteLine("CAN native end recieve loop"); }
private void ReceiveFrames() { const int length = 16; byte[] buffer = new byte[length]; CANFrame frame = new CANFrame(); int rxLen = 0; while (mKeepReceiving) { try { rxLen = can_read(mCanFd, buffer, length); if (rxLen > 0) { frame.WireFormatArray = buffer; base.OnRaiseFrameReceivedEvent(new FrameReceivedEventArgs(frame)); } } catch (Exception e) { // stuffed. mKeepReceiving = false; CNXLog.Error("CANNative.ReceiveFrames", e); break; } } Console.WriteLine("CAN native end recieve loop"); }
/// <summary> /// Sends the frame on the CAN bus. /// </summary> /// <param name="frame">The frame to send.</param> /// <returns>Total number of bytes put onto the CAN bus.</returns> public override int Send(CANFrame frame) { // synchronise sends //int sent = 0; //lock (sendLock) //{ // int retries = 3; // for (sent = can_send(mCanFd, frame.MailboxId, (byte)frame.DataLength, frame.Data); sent < 4 && --retries > 0; sent = can_send(mCanFd, frame.MailboxId, (byte)frame.DataLength, frame.Data)) // Thread.Sleep(1); //} //return sent; // synchronise sends int sent = -1; uint canId = frame.MailboxId; // test 11 or 29 bit addressing if (canId > 0x7ff) { canId |= (uint)CANFrame.IDFlags.CAN_EFF_FLAG; } lock (sendLock) { try { sent = can_send(mCanFd, canId, (byte)frame.DataLength, frame.Data); } catch (Exception e) { CNXLog.Error("CANNative.Send", e); } } return(sent); }
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); } }