Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("NXTLib File Upload Test\r\n");

            string filename = "version.ric"; //filename on disk (locally)
            string filenameonbrick = "version.ric"; //filename on remote NXT
            USB usbLink = new USB();
            Bluetooth blueLink = new Bluetooth();

            Brick brick;

            try
            {
                //Try connecting via USB
                Console.WriteLine("Searching for bricks via USB...");
                List<Brick> bricks = usbLink.Search();
                Console.WriteLine("Connecting to brick via USB...");
                usbLink.Connect(bricks[0]);

                brick = bricks[0];
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Failed to connect via USB.");

                try
                {
                    //Try Connecting via Bluetooth
                    Console.WriteLine("Searching for bricks via Bluetooth...");
                    blueLink.Initialize(); //required to prepare link, must be done BEFORE other functions
                    List<Brick> bricks = blueLink.Search();
                    Console.WriteLine("Connecting to brick via Bluetooth...");
                    bricks[0].Connect();

                    brick = bricks[0];
                }
                catch (NXTNoBricksFound)
                {
                    Error_NoBricks();
                    return;
                }
                catch (NXTLinkNotSupported)
                {
                    Console.WriteLine("Bluetooth not supported on this machine!");
                    Error_NoBricks();
                    return;
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.Message);
                    Error_NoBricks();
                    return;
                }
            }

            //Upload File
            Console.WriteLine("Uploading file...");
            brick.UploadFile(filename, filenameonbrick);

            //Disconnect
            Console.WriteLine("Disconnecting...");
            brick.Disconnect();

            Console.WriteLine("Success!");
            Console.WriteLine("Press any key to continue...");
            Console.ReadKey(true);
            return;
        }
Beispiel #2
0
        private void SearchForNXT()
        {
            List<Brick> usbbricks = new List<Brick>();
            List<Brick> bluebricks = new List<Brick>();
            USB usb = new USB();
            Bluetooth blue = new Bluetooth();

            try
            {
                WriteMessage("Searching for bricks via USB...");
                List<Brick> list = usb.Search();
                usbbricks = list;
                WriteMessage("Brick found via USB!");
                foreach (Brick item in usbbricks)
                {
                    AddItem(item, LinkType.USB);
                }
            }
            catch (NXTNoBricksFound) { WriteMessage("No bricks found via USB."); }
            catch (Exception ex)
            {
                WriteMessage("Error while searching via USB:\r\n");
                WriteMessage(ex.Message);
            }

            int index = 0;
            SearchVia.Invoke(new MethodInvoker(delegate { index = SearchVia.SelectedIndex; }));
            int timeindex = 0;
            Time.Invoke(new MethodInvoker(delegate { timeindex = Time.SelectedIndex; }));

            if (index == 1)
            {
                try
                {
                    WriteMessage("Searching for bricks via Bluetooth...");
                    blue.Initialize();
                    blue.wirelessTimeout = new TimeSpan(0, 0, 5 * (timeindex + 1));
                    List<Brick> list2 = blue.Search();
                    bluebricks = list2;
                    WriteMessage("Bricks found via Bluetooth!");
                    foreach (Brick item in bluebricks)
                    {
                        AddItem(item, LinkType.Bluetooth);
                    }
                }
                catch (NXTNoBricksFound) { WriteMessage("No bricks found via Bluetooth."); }
                catch (Exception ex)
                {
                    WriteMessage("Error while searching via Bluetooth:");
                    WriteMessage(ex.Message);
                }
            }

            if ((usbbricks.Count == 0) && (bluebricks.Count == 0)) { WriteMessage("No bricks found!"); }

            this.Invoke(new MethodInvoker(delegate {
                SearchVia.Enabled = true;
                Search.Enabled = true;
                Time.Enabled = true;
                List.Enabled = true;
                Disconnect.Enabled = false;
                Disconnect.Text = " Disconnect";
                Disconnect.Width = 93;
            }));
        }