예제 #1
0
        /// <summary>
        /// Data received event to decode packet and call associated data object received event. Decoding exceptions handled and ignored.
        /// </summary>
        private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int numBytesRead;

            byte[] serialBuffer;

            // Fetch bytes from serial buffer
            try
            {
                numBytesRead = serialPort.BytesToRead;                              // get the number of bytes in the serial buffer
                serialBuffer = new byte[numBytesRead];                              // local array to hold bytes in the serial buffer
                serialPort.Read(serialBuffer, 0, numBytesRead);                     // transfer bytes
            }
            catch
            {
                return;                                                             // return on exception; e.g. serial port closed unexpectedly
            }

            // Process each byte one-by-one
            for (int i = 0; i < numBytesRead; i++)
            {
                receiveBuffer[receiveBufferIndex++] = serialBuffer[i];              // add new byte to buffer
                if ((serialBuffer[i] & 0x80) == 0x80)                               // if new byte was packet framing char
                {
                    xIMUdata dataObject = null;
                    try
                    {
                        byte[] encodedPacket = new byte[receiveBufferIndex];
                        Array.Copy(receiveBuffer, encodedPacket, encodedPacket.Length);
                        dataObject = PacketConstruction.DeconstructPacket(encodedPacket);//解码
                    }
                    catch
                    {
                        ReceptionErrors++;                                          // invalid packet
                    }
                    if (dataObject != null)                                         // if packet successfully deconstructed
                    {
                        OnxIMUdataReceived(dataObject);
                        if (dataObject is ErrorData)
                        {
                            OnErrorDataReceived((ErrorData)dataObject); PacketsReadCounter.ErrorPackets++;
                        }
                        else if (dataObject is CommandData)
                        {
                            OnCommandDataReceived((CommandData)dataObject); PacketsReadCounter.CommandPackets++;
                        }
                        else if (dataObject is RegisterData)
                        {
                            OnRegisterDataReceived((RegisterData)dataObject); PacketsReadCounter.WriteRegisterPackets++;
                        }
                        else if (dataObject is DateTimeData)
                        {
                            OnDateTimeDataReceived((DateTimeData)dataObject); PacketsReadCounter.WriteDateTimePackets++;
                        }
                        else if (dataObject is RawBatteryAndThermometerData)
                        {
                            OnRawBatteryAndThermometerDataReceived((RawBatteryAndThermometerData)dataObject); PacketsReadCounter.RawBatteryAndThermometerDataPackets++;
                        }
                        else if (dataObject is CalBatteryAndThermometerData)
                        {
                            OnCalBatteryAndThermometerDataReceived((CalBatteryAndThermometerData)dataObject); PacketsReadCounter.CalBatteryAndThermometerDataPackets++;
                        }
                        else if (dataObject is RawInertialAndMagneticData)
                        {
                            OnRawInertialAndMagneticDataReceived((RawInertialAndMagneticData)dataObject); PacketsReadCounter.RawInertialAndMagneticDataPackets++;
                        }
                        else if (dataObject is CalInertialAndMagneticData)
                        {
                            OnCalInertialAndMagneticDataReceived((CalInertialAndMagneticData)dataObject);
                            PacketsReadCounter.CalInertialAndMagneticDataPackets++;
                        }
                        else if (dataObject is QuaternionData)
                        {
                            OnQuaternionDataReceived((QuaternionData)dataObject); PacketsReadCounter.QuaternionDataPackets++;
                        }
                        else if (dataObject is DigitalIOdata)
                        {
                            OnDigitalIODataReceived((DigitalIOdata)dataObject); PacketsReadCounter.DigitalIOdataPackets++;
                        }
                        else if (dataObject is RawAnalogueInputData)
                        {
                            OnRawAnalogueInputDataReceived((RawAnalogueInputData)dataObject); PacketsReadCounter.RawInertialAndMagneticDataPackets++;
                        }
                        else if (dataObject is CalAnalogueInputData)
                        {
                            OnCalAnalogueInputDataReceived((CalAnalogueInputData)dataObject); PacketsReadCounter.CalAnalogueInputDataPackets++;
                        }
                        else if (dataObject is PWMoutputData)
                        {
                            OnPWMoutputDataReceived((PWMoutputData)dataObject); PacketsReadCounter.PWMoutputDataPackets++;
                        }
                        else if (dataObject is RawADXL345busData)
                        {
                            OnRawADXL345busDataReceived((RawADXL345busData)dataObject); PacketsReadCounter.RawADXL345busDataPackets++;
                        }
                        else if (dataObject is CalADXL345busData)
                        {
                            OnCalADXL345busDataReceived((CalADXL345busData)dataObject); PacketsReadCounter.CalADXL345busDataPackets++;
                        }

                        //csvfileWriter.WriteData(dataObject);
                    }
                    receiveBufferIndex = 0;                                         // reset buffer.
                }
            }
        }
예제 #2
0
        /// <summary>
        /// Decodes a complete packet
        /// </summary>
        /// <param name="packet"></param>
        public void DecodePacket(byte[] packet)
        {
            xIMUdata dataObject = null;

            try
            {
                dataObject = PacketConstruction.DeconstructPacket(packet);
            }
            catch
            {
                ReadErrors++;                                               // invalid packet
            }
            if (dataObject != null)                                         // if packet successfully deconstructed
            {
                OnxIMUdataRead(dataObject);
                if (dataObject is ErrorData)
                {
                    OnErrorDataRead((ErrorData)dataObject); PacketsReadCounter.ErrorPackets++;
                }
                else if (dataObject is CommandData)
                {
                    OnCommandDataRead((CommandData)dataObject); PacketsReadCounter.CommandPackets++;
                }
                else if (dataObject is RegisterData)
                {
                    OnRegisterDataRead((RegisterData)dataObject); PacketsReadCounter.WriteRegisterPackets++;
                }
                else if (dataObject is DateTimeData)
                {
                    OnDateTimeDataRead((DateTimeData)dataObject); PacketsReadCounter.WriteDateTimePackets++;
                }
                else if (dataObject is RawBatteryAndThermometerData)
                {
                    OnRawBatteryAndThermometerDataRead((RawBatteryAndThermometerData)dataObject); PacketsReadCounter.RawBatteryAndThermometerDataPackets++;
                }
                else if (dataObject is CalBatteryAndThermometerData)
                {
                    OnCalBatteryAndThermometerDataRead((CalBatteryAndThermometerData)dataObject); PacketsReadCounter.CalBatteryAndThermometerDataPackets++;
                }
                else if (dataObject is RawInertialAndMagneticData)
                {
                    OnRawInertialAndMagneticDataRead((RawInertialAndMagneticData)dataObject); PacketsReadCounter.RawInertialAndMagneticDataPackets++;
                }
                else if (dataObject is CalInertialAndMagneticData)
                {
                    OnCalInertialAndMagneticDataRead((CalInertialAndMagneticData)dataObject); PacketsReadCounter.CalInertialAndMagneticDataPackets++;
                }
                else if (dataObject is QuaternionData)
                {
                    OnQuaternionDataRead((QuaternionData)dataObject); PacketsReadCounter.QuaternionDataPackets++;
                }
                else if (dataObject is DigitalIOdata)
                {
                    OnDigitalIODataRead((DigitalIOdata)dataObject); PacketsReadCounter.DigitalIOdataPackets++;
                }
                else if (dataObject is RawAnalogueInputData)
                {
                    OnRawAnalogueInputDataRead((RawAnalogueInputData)dataObject); PacketsReadCounter.RawInertialAndMagneticDataPackets++;
                }
                else if (dataObject is CalAnalogueInputData)
                {
                    OnCalAnalogueInputDataRead((CalAnalogueInputData)dataObject); PacketsReadCounter.CalAnalogueInputDataPackets++;
                }
                else if (dataObject is PWMoutputData)
                {
                    OnPWMoutputDataRead((PWMoutputData)dataObject); PacketsReadCounter.PWMoutputDataPackets++;
                }
                else if (dataObject is RawADXL345busData)
                {
                    OnRawADXL345busDataRead((RawADXL345busData)dataObject); PacketsReadCounter.RawADXL345busDataPackets++;
                }
                else if (dataObject is CalADXL345busData)
                {
                    OnCalADXL345busDataRead((CalADXL345busData)dataObject); PacketsReadCounter.CalADXL345busDataPackets++;
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Reads file. Interpreted packets are provided in individual events.
        /// </summary>
        /// <param name="isAsync">
        /// Enables OnAsyncReadProgressChanged event for use when called within background worker.
        /// </param>
        private void DoRead(bool isAsync)
        {
            int previousProgressPercentage = 0;

            byte[] readBuffer      = new byte[256];
            byte   readBufferIndex = 0;

            // Read the source file into a byte array.
            byte[] fileBuffer     = new byte[fileStream.Length];
            int    numBytesToRead = (int)fileStream.Length;
            int    numBytesRead   = 0;

            if (isAsync)
            {
                OnAsyncReadProgressChanged(new AsyncReadProgressChangedEventArgs(0, "Reading file..."));
            }
            while (numBytesToRead > 0)
            {
                int n = fileStream.Read(fileBuffer, numBytesRead, numBytesToRead);  // Read may return anything from 0 to numBytesToRead.
                if (n == 0)
                {
                    break;                                                          // Break when the end of the file is reached.
                }
                numBytesRead   += n;
                numBytesToRead -= n;
            }

            // Process each byte one-by-one
            for (int i = 0; i < numBytesRead; i++)
            {
                if (isAsync)
                {
                    int newProgressPercentage = (int)(Math.Round(100d * ((double)i / (double)numBytesRead)));
                    if (backgroundWorker.CancellationPending)
                    {
                        break;
                    }
                    if (newProgressPercentage != previousProgressPercentage)
                    {
                        OnAsyncReadProgressChanged(new AsyncReadProgressChangedEventArgs(newProgressPercentage, "Processed " + string.Format("{0:n0}", i / 1024) + " KB of " + string.Format("{0:n0}", numBytesRead / 1024) + " KB"));
                        previousProgressPercentage = newProgressPercentage;
                    }
                }

                // Process byte
                readBuffer[readBufferIndex++] = fileBuffer[i];                      // add to buffer
                if ((fileBuffer[i] & 0x80) == 0x80)                                 // if packet framing char
                {
                    xIMUdata dataObject = null;
                    try
                    {
                        byte[] encodedPacket = new byte[readBufferIndex];
                        Array.Copy(readBuffer, encodedPacket, encodedPacket.Length);
                        dataObject = PacketConstruction.DeconstructPacket(encodedPacket);
                    }
                    catch
                    {
                        ReadErrors++;                                               // invalid packet
                    }
                    if (dataObject != null)                                         // if packet successfully deconstructed
                    {
                        OnxIMUdataRead(dataObject);
                        if (dataObject is ErrorData)
                        {
                            OnErrorDataRead((ErrorData)dataObject); PacketsReadCounter.ErrorPackets++;
                        }
                        else if (dataObject is CommandData)
                        {
                            OnCommandDataRead((CommandData)dataObject); PacketsReadCounter.CommandPackets++;
                        }
                        else if (dataObject is RegisterData)
                        {
                            OnRegisterDataRead((RegisterData)dataObject); PacketsReadCounter.WriteRegisterPackets++;
                        }
                        else if (dataObject is DateTimeData)
                        {
                            OnDateTimeDataRead((DateTimeData)dataObject); PacketsReadCounter.WriteDateTimePackets++;
                        }
                        else if (dataObject is RawBatteryAndThermometerData)
                        {
                            OnRawBatteryAndThermometerDataRead((RawBatteryAndThermometerData)dataObject); PacketsReadCounter.RawBatteryAndThermometerDataPackets++;
                        }
                        else if (dataObject is CalBatteryAndThermometerData)
                        {
                            OnCalBatteryAndThermometerDataRead((CalBatteryAndThermometerData)dataObject); PacketsReadCounter.CalBatteryAndThermometerDataPackets++;
                        }
                        else if (dataObject is RawInertialAndMagneticData)
                        {
                            OnRawInertialAndMagneticDataRead((RawInertialAndMagneticData)dataObject); PacketsReadCounter.RawInertialAndMagneticDataPackets++;
                        }
                        else if (dataObject is CalInertialAndMagneticData)
                        {
                            OnCalInertialAndMagneticDataRead((CalInertialAndMagneticData)dataObject); PacketsReadCounter.CalInertialAndMagneticDataPackets++;
                        }
                        else if (dataObject is QuaternionData)
                        {
                            OnQuaternionDataRead((QuaternionData)dataObject); PacketsReadCounter.QuaternionDataPackets++;
                        }
                        else if (dataObject is DigitalIOdata)
                        {
                            OnDigitalIODataRead((DigitalIOdata)dataObject); PacketsReadCounter.DigitalIOdataPackets++;
                        }
                        else if (dataObject is RawAnalogueInputData)
                        {
                            OnRawAnalogueInputDataRead((RawAnalogueInputData)dataObject); PacketsReadCounter.RawInertialAndMagneticDataPackets++;
                        }
                        else if (dataObject is CalAnalogueInputData)
                        {
                            OnCalAnalogueInputDataRead((CalAnalogueInputData)dataObject); PacketsReadCounter.CalAnalogueInputDataPackets++;
                        }
                        else if (dataObject is PWMoutputData)
                        {
                            OnPWMoutputDataRead((PWMoutputData)dataObject); PacketsReadCounter.PWMoutputDataPackets++;
                        }
                        else if (dataObject is RawADXL345busData)
                        {
                            OnRawADXL345busDataRead((RawADXL345busData)dataObject); PacketsReadCounter.RawADXL345busDataPackets++;
                        }
                        else if (dataObject is CalADXL345busData)
                        {
                            OnCalADXL345busDataRead((CalADXL345busData)dataObject); PacketsReadCounter.CalADXL345busDataPackets++;
                        }
                    }
                    readBufferIndex = 0;                                         // reset buffer.
                }
            }
        }