Exemplo n.º 1
0
        /// <summary>
        /// Syncronously obtains a directory of the log sessions on a device,
        /// including file format information. Will throw exceptions if there is
        /// an error in communication or gaining access to the com port.
        /// </summary>
        /// <param name="portName">The comport string to use to communicate withe the device.</param>
        /// <returns>A session directory structure representing the device.</returns>
        public static SessionDirectory SyncGetSessionDirectory(string portName)
        {
            string         fileVersion = string.Empty;
            List <Session> list        = new List <Session>();
            Session        session     = null;

            try
            {
                Open(portName);
                bool ok = true;

                ValidChunk lastValidChunk = null;
                bool       error          = false;
                bool       reportError    = false;
                int        offset         = 0;
                var        riff           = Read(offset, 12, out ok);

                if (ok == false)
                {
                    // Retry
                    nakRetryCount++;
                    riff = Read(offset, 12, out ok);
                    if (ok == false)
                    {
                        throw new Exception("Cant read bioharness");
                    }
                }

                var strRiff = Encoding.ASCII.GetString(riff, 0, 4);
                if (strRiff.ToUpper().Equals("RIFF") == false)
                {
                    throw new Exception("Not a RIFF Chunk");
                }

                var tLength = BitConverter.ToInt32(riff, 4);
                /* DateTimeFormatInfo to parse DateTime */
                IFormatProvider culture = new CultureInfo("en-NZ", false);

                bool cancel = false;
                offset = 12;
                while (cancel == false)
                {
                    if (offset >= tLength)
                    {
                        break;
                    }

                    riff = Read(offset, 8, out ok);
                    if (ok == false)
                    {
                        nakRetryCount++;
                        riff = Read(offset, 8, out ok);
                        if (ok == false)
                        {
                            throw new Exception("Cant read Bioharness");
                        }
                    }

                    offset += 8;
                    int ckLength = BitConverter.ToInt32(riff, 4);

                    string chunk;
                    try
                    {
                        error = false;
                        chunk = Encoding.ASCII.GetString(riff, 0, 4);

                        switch (chunk)
                        {
                        case "JUNK":     /* JUNK */
                            lastValidChunk = new ValidChunk(offset, "JUNK");
                            offset        += ckLength;
                            /* We can have JUNK between logh and logr, so don't reset session to null here */
                            break;

                        case "zphr":     /* zphr */
                            lastValidChunk = new ValidChunk(offset, "zphr");
                            var zphr = Read(offset, ckLength, out ok);
                            if (ok == false)
                            {
                                nakRetryCount++;
                                zphr = Read(offset, ckLength, out ok);
                                if (ok == false)
                                {
                                    throw new Exception("Cant read bioharness");
                                }
                            }

                            fileVersion = Encoding.ASCII.GetString(zphr, 4, 4);
                            offset     += ckLength;
                            session     = null;
                            break;

                        case "fms ":     /* fms */
                            lastValidChunk = new ValidChunk(offset, "fms");
                            offset        += ckLength;
                            session        = null;
                            break;

                        case "logr":     /* logr */
                        {
                            lastValidChunk = new ValidChunk(offset, "logr");
                            if (session == null)
                            {
                                error = true;
                            }
                            else
                            {
                                /* Calculate and round milliseconds */
                                session.Duration = TimeSpan.FromMilliseconds((((ckLength - session.PadBytes) / (session.Channels * 2)) - 1) * session.Period);

                                /* Because we have nothing to compare too, a single element comes up as zero */
                                if (session.Duration == TimeSpan.FromSeconds(0))
                                {
                                    session.Duration = TimeSpan.FromSeconds(1);
                                }

                                session.Offset = offset + session.PadBytes;
                                session.Length = ckLength - session.PadBytes;
                                list.Add(session);
                            }

                            offset += ckLength;
                        }

                            session = null;
                            break;

                        case "logh":
                        case "log1":
                        case "log2":     /* logh */
                        {
                            lastValidChunk = new ValidChunk(offset, "logh");
                            var logh = Read(offset, ckLength, out ok);
                            if (ok == false)
                            {
                                nakRetryCount++;
                                logh = Read(offset, ckLength, out ok);
                                if (ok == false)
                                {
                                    throw new Exception("Cant read bioharness");
                                }
                            }

                            int period = BitConverter.ToInt16(logh, 8);
                            period = (period == 0) ? 1008 : period;
                            int channels = BitConverter.ToInt16(logh, 12);
                            int padRaw   = BitConverter.ToInt16(logh, 14);
                            /* DateTime format should be invariant from the culture in USA Month and Day are reverse. */
                            DateTime timestamp = DateTime.Parse(
                                string.Format(
                                    "{0}/{1}/{2} {3}",
                                    logh[3],
                                    logh[2],
                                    logh[1] * 256 + logh[0],
                                    TimeSpan.FromMilliseconds(BitConverter.ToInt32(logh, 4)).ToString()),
                                culture);
                            session        = new Session(timestamp, channels, padRaw);
                            session.Period = period;
                            offset        += ckLength;
                        }

                        break;

                        default:
                            error   = true;
                            session = null;
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        /* This usually means device unplugged */
                        error = true;
                        throw new IOException("Sorry problem reading data.", ex);
                    }

                    if (error == true)
                    {
                        /* Only report each block of errors once */
                        if (reportError == false)
                        {
                            System.Diagnostics.Debug.WriteLine(string.Format("Error at: 0x{0:x8}, continuing ...", offset));
                            reportError = true;
                            offset      = lastValidChunk.Offset;
                        }

                        offset -= 8;
                        int length = 2048 - (offset % 2048);

                        offset += length;
                    }
                    else
                    {
                        reportError = false;
                    }
                }

                Close();
            }
            catch (Exception ex)
            {
                try
                {
                    Close();
                }
                catch
                {
                }

                throw new IOException("Sorry problem reading data.", ex);
            }

            return(new SessionDirectory(list.ToArray(), fileVersion));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Syncronously obtains a directory of the log sessions on a device,
        /// including file format information. Will throw exceptions if there is
        /// an error in communication or gaining access to the com port.
        /// </summary>
        /// <param name="portName">The comport string to use to communicate withe the device.</param>
        /// <returns>A session directory structure representing the device.</returns>
        public static SessionDirectory SyncGetSessionDirectory(string portName)
        {
            string fileVersion = string.Empty;
            List<Session> list = new List<Session>();
            Session session = null;

            try
            {
                Open(portName);
                bool ok = true;

                ValidChunk lastValidChunk = null;
                bool error = false;
                bool reportError = false;
                int offset = 0;
                var riff = Read(offset, 12, out ok);

                if (ok == false)
                {
                    // Retry
                    nakRetryCount++;
                    riff = Read(offset, 12, out ok);
                    if (ok == false)
                    {
                        throw new Exception("Cant read bioharness");
                    }
                }

                var strRiff = Encoding.ASCII.GetString(riff, 0, 4);
                if (strRiff.ToUpper().Equals("RIFF") == false)
                {
                    throw new Exception("Not a RIFF Chunk");
                }

                var tLength = BitConverter.ToInt32(riff, 4);
                /* DateTimeFormatInfo to parse DateTime */
                IFormatProvider culture = new CultureInfo("en-NZ", false);

                bool cancel = false;
                offset = 12;
                while (cancel == false)
                {
                    if (offset >= tLength)
                    {
                        break;
                    }

                    riff = Read(offset, 8, out ok);
                    if (ok == false)
                    {
                        nakRetryCount++;
                        riff = Read(offset, 8, out ok);
                        if (ok == false)
                        {
                            throw new Exception("Cant read Bioharness");
                        }
                    }

                    offset += 8;
                    int ckLength = BitConverter.ToInt32(riff, 4);

                    string chunk;
                    try
                    {
                        error = false;
                        chunk = Encoding.ASCII.GetString(riff, 0, 4);

                        switch (chunk)
                        {
                            case "JUNK": /* JUNK */
                                lastValidChunk = new ValidChunk(offset, "JUNK");
                                offset += ckLength;
                                /* We can have JUNK between logh and logr, so don't reset session to null here */
                                break;
                            case "zphr": /* zphr */
                                lastValidChunk = new ValidChunk(offset, "zphr");
                                var zphr = Read(offset, ckLength, out ok);
                                if (ok == false)
                                {
                                    nakRetryCount++;
                                    zphr = Read(offset, ckLength, out ok);
                                    if (ok == false)
                                    {
                                        throw new Exception("Cant read bioharness");
                                    }
                                }

                                fileVersion = Encoding.ASCII.GetString(zphr, 4, 4);
                                offset += ckLength;
                                session = null;
                                break;
                            case "fms ": /* fms */
                                lastValidChunk = new ValidChunk(offset, "fms");
                                offset += ckLength;
                                session = null;
                                break;
                            case "logr": /* logr */
                                {
                                    lastValidChunk = new ValidChunk(offset, "logr");
                                    if (session == null)
                                    {
                                        error = true;
                                    }
                                    else
                                    {
                                        /* Calculate and round milliseconds */
                                        session.Duration = TimeSpan.FromMilliseconds((((ckLength - session.PadBytes) / (session.Channels * 2)) - 1) * session.Period);

                                        /* Because we have nothing to compare too, a single element comes up as zero */
                                        if (session.Duration == TimeSpan.FromSeconds(0))
                                        {
                                            session.Duration = TimeSpan.FromSeconds(1);
                                        }

                                        session.Offset = offset + session.PadBytes;
                                        session.Length = ckLength - session.PadBytes;
                                        list.Add(session);
                                    }

                                    offset += ckLength;
                                }

                                session = null;
                                break;
                            case "logh":
                            case "log1":
                            case "log2": /* logh */
                                {
                                    lastValidChunk = new ValidChunk(offset, "logh");
                                    var logh = Read(offset, ckLength, out ok);
                                    if (ok == false)
                                    {
                                        nakRetryCount++;
                                        logh = Read(offset, ckLength, out ok);
                                        if (ok == false)
                                        {
                                            throw new Exception("Cant read bioharness");
                                        }
                                    }

                                    int period = BitConverter.ToInt16(logh, 8);
                                    period = (period == 0) ? 1008 : period;
                                    int channels = BitConverter.ToInt16(logh, 12);
                                    int padRaw = BitConverter.ToInt16(logh, 14);
                                    /* DateTime format should be invariant from the culture in USA Month and Day are reverse. */
                                    DateTime timestamp = DateTime.Parse(
                                        string.Format(
                                        "{0}/{1}/{2} {3}",
                                        logh[3],
                                        logh[2],
                                        logh[1] * 256 + logh[0],
                                        TimeSpan.FromMilliseconds(BitConverter.ToInt32(logh, 4)).ToString()),
                                        culture);
                                    session = new Session(timestamp, channels, padRaw);
                                    session.Period = period;
                                    offset += ckLength;
                                }

                                break;
                            default:
                                error = true;
                                session = null;
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        /* This usually means device unplugged */
                        error = true;
                        throw new IOException("Sorry problem reading data.", ex);
                    }

                    if (error == true)
                    {
                        /* Only report each block of errors once */
                        if (reportError == false)
                        {
                            System.Diagnostics.Debug.WriteLine(string.Format("Error at: 0x{0:x8}, continuing ...", offset));
                            reportError = true;
                            offset = lastValidChunk.Offset;
                        }

                        offset -= 8;
                        int length = 2048 - (offset % 2048);

                        offset += length;
                    }
                    else
                    {
                        reportError = false;
                    }
                }

                Close();
            }
            catch (Exception ex)
            {
                try
                {
                    Close();
                }
                catch
                {
                }

                throw new IOException("Sorry problem reading data.", ex);
            }

            return new SessionDirectory(list.ToArray(), fileVersion);
        }