コード例 #1
0
        private void ReadEV3Directory(bool resetposition)
        {
            MemoryStream data = new MemoryStream();

//            if (!ev3path.Equals("/proc/"))       // avoid locking up the brick
            {
                // get data from brick
                BinaryBuffer b = new BinaryBuffer();
                b.Append16(500);  // expect max 500 bytes per packet
                b.AppendZeroTerminated(internalPath(ev3path));
                byte[] response = connection.SystemCommand(EV3Connection.LIST_FILES, b);

                if (response == null)
                {
                    throw new Exception("No response to LIST_FILES");
                }
                if (response.Length < 6)
                {
                    throw new Exception("Response too short for LIST_FILES");
                }
                if (response[0] != EV3Connection.SUCCESS && response[0] != EV3Connection.END_OF_FILE)
                {
                    throw new Exception("Unexpected status at LIST_FILES: " + response[0]);
                }
                int handle = response[5] & 0xff;
                data.Write(response, 6, response.Length - 6);

                // continue reading until have total buffer
                while (response[0] != EV3Connection.END_OF_FILE)
                {
                    b.Clear();
                    b.Append8(handle);
                    b.Append16(500);  // expect max 500 bytes per packet
                    response = connection.SystemCommand(EV3Connection.CONTINUE_LIST_FILES, b);
                    //                    Console.WriteLine("follow-up response length: " + response.Length);

                    if (response == null)
                    {
                        throw new Exception("No response to CONTINUE_LIST_FILES");
                    }
                    if (response.Length < 2)
                    {
                        throw new Exception("Too short response to CONTINUE_LIST_FILES");
                    }
                    if (response[0] != EV3Connection.SUCCESS && response[0] != EV3Connection.END_OF_FILE)
                    {
                        throw new Exception("Unexpected status at CONTINUE_LIST_FILES: " + response[0]);
                    }
                    //                    Console.WriteLine("subsequent response length: " + response.Length);
                    data.Write(response, 2, response.Length - 2);
                }
            }

            List <DirectoryEntry> list = new List <DirectoryEntry>();

            data.Position = 0;  // start reading at beginning
            StreamReader tr = new StreamReader(data, Encoding.GetEncoding("iso-8859-1"));
            String       l;

            while ((l = tr.ReadLine()) != null)
            {
                if (l.EndsWith("/"))
                {
                    String n = l.Substring(0, l.Length - 1);
                    if ((!n.Equals(".")) && (!n.Equals("..")))
                    {
                        list.Add(new DirectoryEntry(n, 0, true));
                    }
                }
                else
                {
                    int firstspace = l.IndexOf(' ');
                    if (firstspace < 0)
                    {
                        continue;
                    }
                    int secondspace = l.IndexOf(' ', firstspace + 1);
                    if (secondspace < 0)
                    {
                        continue;
                    }
                    int size = int.Parse(l.Substring(firstspace, secondspace - firstspace).Trim(), System.Globalization.NumberStyles.HexNumber);

                    list.Add(new DirectoryEntry(l.Substring(secondspace + 1), size, false));
                }
            }

            // sort list
            list.Sort((x, y) => x.FileName.CompareTo(y.FileName));

            // put data into listview
            EV3Directory.Items.Clear();
            foreach (DirectoryEntry de in list)
            {
                EV3Directory.Items.Add(de);
            }

            // let the WPF system re-calculate all column widths so everthing fits as good as possible
            foreach (var gvc in EV3DirectoryGridView.Columns)
            {
                gvc.Width = gvc.ActualWidth;
                gvc.Width = Double.NaN;
            }

            // move the controls scroller to top position
            if (resetposition)
            {
                if (EV3Directory.Items.Count > 0)
                {
                    EV3Directory.ScrollIntoView(EV3Directory.Items[0]);
                }
            }
        }