Example #1
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;
            }));
        }
Example #2
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;
        }
Example #3
0
        private void Item_Click(object sender, EventArgs e)
        {
            Button s = (Button)sender;
            s.Enabled = false;
            Brick brick = new Brick();
            if (s.Name.StartsWith("USB"))
            {
                USB usb = new USB();
                Protocol.BrickInfo info = new Protocol.BrickInfo();
                info.address = new byte[] { 0, 0, 0, 0, 0, 0 };
                info.name = s.Text.Trim();
                brick = new Brick(usb, info);
                myLinkType = LinkType.USB;
            }
            if (s.Name.StartsWith("BLU"))
            {
                Bluetooth blue = new Bluetooth();
                Protocol.BrickInfo info = new Protocol.BrickInfo();
                info.address = Utils.AddressString2Byte(s.Name.Substring(3), true);
                info.name = s.Text.Trim();
                brick = new Brick(blue, info);
                myLinkType = LinkType.Bluetooth;
            }
            /*try
            {
                brick.Connect();
                brick.Disconnect();
            }
            catch (Exception ex)
            {
                WriteMessage("Error while connecting to brick:");
                WriteMessage(ex.Message);
                Disconnect_Click(null, null);
                return;
            }*/
            myBrick = brick;

            Disconnect.Enabled = true;
            s.Enabled = true;
            List.Controls.Clear();
            List.Visible = false;
            SearchVia.Enabled = false;
            Search.Enabled = true;
            Time.Enabled = false;
            Search.Image = global::NXTLibTesterGUI.Properties.Resources.StatusAnnotations_Complete_and_ok_32xLG_color;

            NXTPanel.Visible = true;
            NXT.Text = "       Connected to " + myBrick.brickinfo.name;
            NXTConn.Text = "Connection Type: " + myLinkType.ToString();
            if (myLinkType != LinkType.USB) { NXTAdd.Text = "Address: " + Utils.AddressByte2String(myBrick.brickinfo.address, true); }
            else { NXTAdd.Text = ""; }

            CheckUpdateStatus();
            Search.Text = " Update Version Info";
            WriteMessage("Connection successful!");
        }