Пример #1
0
        /// <summary>
        /// Do full update - get firmware from internet
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="historyhash"></param>
        public bool update(string comport, software temp, string historyhash)
        {
            BoardDetect.boards board = BoardDetect.boards.none;

            try
            {
                updateProgress(-1, "Detecting Board Version");

                board = BoardDetect.DetectBoard(comport);

                if (board == BoardDetect.boards.none)
                {
                    CustomMessageBox.Show("Cant detect your Board version. Please check your cabling");
                    return(false);
                }

                int apmformat_version = -1; // fail continue

                if (board != BoardDetect.boards.px4 && board != BoardDetect.boards.px4v2 && board != BoardDetect.boards.vrbrainv40 && board != BoardDetect.boards.vrbrainv45 && board != BoardDetect.boards.vrbrainv50 && board != BoardDetect.boards.vrbrainv51 && board != BoardDetect.boards.vrherov10 && board != BoardDetect.boards.vrubrainv51)
                {
                    try
                    {
                        apmformat_version = BoardDetect.decodeApVar(comport, board);
                    }
                    catch { }

                    if (apmformat_version != -1 && apmformat_version != temp.k_format_version)
                    {
                        if (DialogResult.No == CustomMessageBox.Show("Epprom changed, all your setting will be lost during the update,\nDo you wish to continue?", "Epprom format changed (" + apmformat_version + " vs " + temp.k_format_version + ")", MessageBoxButtons.YesNo))
                        {
                            CustomMessageBox.Show("Please connect and backup your config in the configuration tab.");
                            return(false);
                        }
                    }
                }


                log.Info("Detected a " + board);

                updateProgress(-1, "Detected a " + board);

                string baseurl = "";
                if (board == BoardDetect.boards.b2560)
                {
                    baseurl = temp.url2560.ToString();
                }
                else if (board == BoardDetect.boards.b1280)
                {
                    baseurl = temp.url.ToString();
                }
                else if (board == BoardDetect.boards.b2560v2)
                {
                    baseurl = temp.url2560_2.ToString();
                }
                else if (board == BoardDetect.boards.px4)
                {
                    baseurl = temp.urlpx4v1.ToString();
                }
                else if (board == BoardDetect.boards.px4v2)
                {
                    baseurl = temp.urlpx4v2.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv40)
                {
                    baseurl = temp.urlvrbrainv40.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv45)
                {
                    baseurl = temp.urlvrbrainv45.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv50)
                {
                    baseurl = temp.urlvrbrainv50.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv51)
                {
                    baseurl = temp.urlvrbrainv51.ToString();
                }
                else if (board == BoardDetect.boards.vrherov10)
                {
                    baseurl = temp.urlvrherov10.ToString();
                }
                else if (board == BoardDetect.boards.vrubrainv51)
                {
                    baseurl = temp.urlvrubrainv51.ToString();
                }
                else
                {
                    CustomMessageBox.Show("Invalid Board Type");
                    return(false);
                }

                if (historyhash != "")
                {
                    baseurl = getUrl(historyhash, baseurl);
                }

                log.Info("Using " + baseurl);

                // Create a request using a URL that can receive a post.
                WebRequest request = WebRequest.Create(baseurl);
                request.Timeout = 10000;
                // Set the Method property of the request to POST.
                request.Method = "GET";
                // Get the request stream.
                Stream dataStream; //= request.GetRequestStream();
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                log.Info(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();

                long bytes   = response.ContentLength;
                long contlen = bytes;

                byte[] buf1 = new byte[1024];

                FileStream fs = new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"firmware.hex", FileMode.Create);

                updateProgress(0, "Downloading from Internet");

                dataStream.ReadTimeout = 30000;

                while (dataStream.CanRead)
                {
                    try
                    {
                        updateProgress(50, "Downloading from Internet");
                    }
                    catch { }
                    int len = dataStream.Read(buf1, 0, 1024);
                    if (len == 0)
                    {
                        break;
                    }
                    bytes -= len;
                    fs.Write(buf1, 0, len);
                }

                fs.Close();
                dataStream.Close();
                response.Close();

                updateProgress(100, "Downloaded from Internet");
                log.Info("Downloaded");
            }
            catch (Exception ex) { updateProgress(50, "Failed download"); CustomMessageBox.Show("Failed to download new firmware : " + ex.ToString()); return(false); }

            MissionPlanner.Utilities.Tracking.AddFW(temp.name, board.ToString());

            return(UploadFlash(comport, Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"firmware.hex", board));
        }
Пример #2
0
        /// <summary>
        /// Detect board version
        /// </summary>
        /// <param name="port"></param>
        /// <returns> boards enum value</returns>
        public static boards DetectBoard(string port)
        {
            SerialPort serialPort = new SerialPort();

            serialPort.PortName = port;

            if (!MainV2.MONO)
            {
                try
                {
                    var ports = Win32DeviceMgmt.GetAllCOMPorts();

                    foreach (var item in ports)
                    {
                        log.InfoFormat("{0}: {1} - {2}", item.name, item.description, item.board);

                        if (port.ToLower() == item.name.ToLower())
                        {
                            if (item.board == "PX4 FMU v4.x")
                            {
                                log.Info("is a px4v4 pixracer");
                                return(boards.px4v4);
                            }

                            /*if (item.board == "PX4 FMU v2.x")
                             * {
                             *  log.Info("is a px4v2");
                             *  return boards.px4v2;
                             * }
                             * if (item.board == "Arduino Mega 2560")
                             * {
                             *  log.Info("is a 2560v2");
                             *  return boards.b2560v2;
                             * }*/
                            if (item.board == "revo-mini")
                            {
                                log.Info("is a revo-mini");
                                return(boards.none);
                            }
                        }
                    }
                } catch { }

                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort"); // Win32_USBControllerDevice
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                foreach (ManagementObject obj2 in searcher.Get())
                {
                    log.InfoFormat("-----------------------------------");
                    log.InfoFormat("Win32_USBDevice instance");
                    log.InfoFormat("-----------------------------------");

                    foreach (var item in obj2.Properties)
                    {
                        log.InfoFormat("{0}: {1}", item.Name, item.Value);
                    }


                    // check vid and pid
                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
                    {
                        // check port name as well
                        if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
                        {
                            log.Info("is a 2560-2");
                            return(boards.b2560v2);
                        }
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0010"))
                    {
                        // check port name as well
                        //if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
                        {
                            log.Info("is a px4");
                            return(boards.px4);
                        }
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0011"))
                    {
                        CustomMessageBox.Show(Strings.PleaseUnplugTheBoardAnd);

                        DateTime DEADLINE = DateTime.Now.AddSeconds(30);

                        while (DateTime.Now < DEADLINE)
                        {
                            string[] allports = SerialPort.GetPortNames();

                            foreach (string port1 in allports)
                            {
                                log.Info(DateTime.Now.Millisecond + " Trying Port " + port1);
                                try
                                {
                                    using (var up = new Uploader(port1, 115200))
                                    {
                                        up.identify();
                                        Console.WriteLine(
                                            "Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}",
                                            up.board_type,
                                            up.board_rev, up.bl_rev, up.fw_maxsize, port1);

                                        if (up.fw_maxsize == 2080768 && up.board_type == 9 && up.bl_rev >= 5)
                                        {
                                            log.Info("is a px4v3");
                                            return(boards.px4v3);
                                        }
                                        else
                                        {
                                            log.Info("is a px4v2");
                                            return(boards.px4v2);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    log.Error(ex);
                                }
                            }
                        }

                        log.Info("Failed to detect px4 board type");
                        return(boards.none);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0021"))
                    {
                        log.Info("is a px4v3 X2.1");
                        return(boards.px4v3);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012"))
                    {
                        log.Info("is a px4v4 pixracer");
                        return(boards.px4v4);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0013"))
                    {
                        log.Info("is a px4v4pro pixhawk 3 pro");
                        return(boards.px4v4pro);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0001"))
                    {
                        log.Info("is a px4v2 bootloader");
                        return(boards.px4v2);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016"))
                    {
                        log.Info("is a px4v2 bootloader");
                        CustomMessageBox.Show(
                            "You appear to have a bootloader with a bad PID value, please update your bootloader.");
                        return(boards.px4v2);
                    }

                    //|| obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0013") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0014") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0015") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016")

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1140"))
                    {
                        log.Info("is a vrbrain 4.0 bootloader");
                        return(boards.vrbrainv40);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1145"))
                    {
                        log.Info("is a vrbrain 4.5 bootloader");
                        return(boards.vrbrainv45);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1150"))
                    {
                        log.Info("is a vrbrain 5.0 bootloader");
                        return(boards.vrbrainv50);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1151"))
                    {
                        log.Info("is a vrbrain 5.1 bootloader");
                        return(boards.vrbrainv51);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1152"))
                    {
                        log.Info("is a vrbrain 5.2 bootloader");
                        return(boards.vrbrainv52);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1154"))
                    {
                        log.Info("is a vrbrain 5.4 bootloader");
                        return(boards.vrbrainv54);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1910"))
                    {
                        log.Info("is a vrbrain core 1.0 bootloader");
                        return(boards.vrcorev10);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1351"))
                    {
                        log.Info("is a vrubrain 5.1 bootloader");
                        return(boards.vrubrainv51);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1352"))
                    {
                        log.Info("is a vrubrain 5.2 bootloader");
                        return(boards.vrubrainv52);
                    }
                }
            }
            else
            {
                // if its mono
                if ((int)DialogResult.Yes == CustomMessageBox.Show("Is this a APM 2+?", "APM 2+", MessageBoxButtons.YesNo))
                {
                    return(boards.b2560v2);
                }
                else
                {
                    if ((int)DialogResult.Yes ==
                        CustomMessageBox.Show("Is this a PX4/PIXHAWK/PIXRACER?", "PX4/PIXHAWK", MessageBoxButtons.YesNo))
                    {
                        if ((int)DialogResult.Yes ==
                            CustomMessageBox.Show("Is this a PIXRACER?", "PIXRACER", MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v4);
                        }
                        if ((int)DialogResult.Yes ==
                            CustomMessageBox.Show("Is this a CUBE?", "CUBE", MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v3);
                        }
                        if ((int)DialogResult.Yes ==
                            CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK", MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v2);
                        }
                        return(boards.px4);
                    }
                    else
                    {
                        return(boards.b2560);
                    }
                }
            }

            if ((int)DialogResult.Yes == CustomMessageBox.Show("Is this a Linux board?", "Linux", MessageBoxButtons.YesNo))
            {
                if ((int)DialogResult.Yes == CustomMessageBox.Show("Is this Bebop2?", "Bebop2", MessageBoxButtons.YesNo))
                {
                    return(boards.bebop2);
                }

                if ((int)DialogResult.Yes == CustomMessageBox.Show("Is this Disco?", "Disco", MessageBoxButtons.YesNo))
                {
                    return(boards.disco);
                }
            }

            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }

            serialPort.DtrEnable = true;
            serialPort.BaudRate  = 57600;
            serialPort.Open();

            Thread.Sleep(100);

            int a = 0;

            while (a < 20) // 20 * 50 = 1 sec
            {
                //Console.WriteLine("write " + DateTime.Now.Millisecond);
                serialPort.DiscardInBuffer();
                serialPort.Write(new byte[] { (byte)'0', (byte)' ' }, 0, 2);
                a++;
                Thread.Sleep(50);

                //Console.WriteLine("btr {0}", serialPort.BytesToRead);
                if (serialPort.BytesToRead >= 2)
                {
                    byte b1 = (byte)serialPort.ReadByte();
                    byte b2 = (byte)serialPort.ReadByte();
                    if (b1 == 0x14 && b2 == 0x10)
                    {
                        serialPort.Close();
                        log.Info("is a 1280");
                        return(boards.b1280);
                    }
                }
            }

            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }

            log.Warn("Not a 1280");

            Thread.Sleep(500);

            serialPort.DtrEnable = true;
            serialPort.BaudRate  = 115200;
            serialPort.Open();

            Thread.Sleep(100);

            a = 0;
            while (a < 4)
            {
                byte[] temp = new byte[] { 0x6, 0, 0, 0, 0 };
                temp = BoardDetect.genstkv2packet(serialPort, temp);
                a++;
                Thread.Sleep(50);

                try
                {
                    if (temp[0] == 6 && temp[1] == 0 && temp.Length == 2)
                    {
                        serialPort.Close();
                        //HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2341&PID_0010\640333439373519060F0\Device Parameters
                        if (!MainV2.MONO &&
                            !Thread.CurrentThread.CurrentCulture.IsChildOf(CultureInfoEx.GetCultureInfo("zh-Hans")))
                        {
                            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort");
                            // Win32_USBControllerDevice
                            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                            foreach (ManagementObject obj2 in searcher.Get())
                            {
                                //Console.WriteLine("Dependant : " + obj2["Dependent"]);

                                // all apm 1-1.4 use a ftdi on the imu board.

                                /*    obj2.Properties.ForEach(x =>
                                 * {
                                 *  try
                                 *  {
                                 *      log.Info(((PropertyData)x).Name.ToString() + " = " + ((PropertyData)x).Value.ToString());
                                 *  }
                                 *  catch { }
                                 * });
                                 */
                                // check vid and pid
                                if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
                                {
                                    // check port name as well
                                    if (
                                        obj2.Properties["Name"].Value.ToString()
                                        .ToUpper()
                                        .Contains(serialPort.PortName.ToUpper()))
                                    {
                                        log.Info("is a 2560-2");
                                        return(boards.b2560v2);
                                    }
                                }
                            }

                            log.Info("is a 2560");
                            return(boards.b2560);
                        }
                    }
                }
                catch
                {
                }
            }

            serialPort.Close();
            log.Warn("Not a 2560");

            if ((int)DialogResult.Yes == CustomMessageBox.Show("Is this a APM 2+?", "APM 2+", MessageBoxButtons.YesNo))
            {
                return(boards.b2560v2);
            }
            else
            {
                if ((int)DialogResult.Yes ==
                    CustomMessageBox.Show("Is this a PX4/PIXHAWK?", "PX4/PIXHAWK", MessageBoxButtons.YesNo))
                {
                    if ((int)DialogResult.Yes ==
                        CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK", MessageBoxButtons.YesNo))
                    {
                        return(boards.px4v2);
                    }
                    return(boards.px4);
                }
                else
                {
                    return(boards.b2560);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// upload to vrbrain standalone
        /// </summary>
        /// <param name="filename"></param>
        public bool UploadVRBRAIN(string filename)
        {
            px4uploader.Uploader up;
            updateProgress(0, "Reading Hex File");
            px4uploader.Firmware fw;
            try
            {
                fw = px4uploader.Firmware.ProcessFirmware(filename);
            }
            catch (Exception ex)
            {
                CustomMessageBox.Show(Strings.ErrorFirmwareFile + "\n\n" + ex.ToString(), Strings.ERROR);
                return(false);
            }

            try
            {
                // check if we are seeing heartbeats
                MainV2.comPort.BaseStream.Open();
                MainV2.comPort.giveComport = true;
                BoardDetect.boards board = BoardDetect.DetectBoard(MainV2.comPortName);

                if (MainV2.comPort.getHeartBeat().Length > 0)
                {
                    MainV2.comPort.doReboot(true);
                    MainV2.comPort.Close();

                    //specific action for VRBRAIN4 board that needs to be manually disconnected before uploading
                    if (board == BoardDetect.boards.vrbrainv40)
                    {
                        CustomMessageBox.Show("VRBRAIN 4 detected. Please unplug the board, and then press OK and plug back in.\n");
                    }
                }
                else
                {
                    MainV2.comPort.BaseStream.Close();
                    CustomMessageBox.Show("Please unplug the board, and then press OK and plug back in.\nMission Planner will look for 30 seconds to find the board");
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                CustomMessageBox.Show("Please unplug the board, and then press OK and plug back in.\nMission Planner will look for 30 seconds to find the board");
            }

            DateTime DEADLINE = DateTime.Now.AddSeconds(30);

            updateProgress(0, "Scanning comports");

            while (DateTime.Now < DEADLINE)
            {
                string[] allports = SerialPort.GetPortNames();

                foreach (string port in allports)
                {
                    log.Info(DateTime.Now.Millisecond + " Trying Port " + port);

                    updateProgress(-1, "Connecting");

                    try
                    {
                        up = new px4uploader.Uploader(port, 115200);
                    }
                    catch (Exception ex)
                    {
                        //System.Threading.Thread.Sleep(50);
                        Console.WriteLine(ex.Message);
                        continue;
                    }

                    try
                    {
                        up.identify();
                        updateProgress(-1, "Identify");
                        log.InfoFormat("Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}", up.board_type, up.board_rev, up.bl_rev, up.fw_maxsize, port);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Not There..");
                        //Console.WriteLine(ex.Message);
                        up.close();
                        continue;
                    }

                    up.skipotp = true;

                    try
                    {
                        up.currentChecksum(fw);
                    }
                    catch
                    {
                        up.__reboot();
                        up.close();
                        CustomMessageBox.Show("No need to upload. already on the board");
                        return(true);
                    }

                    try
                    {
                        up.ProgressEvent += new px4uploader.Uploader.ProgressEventHandler(up_ProgressEvent);
                        up.LogEvent      += new px4uploader.Uploader.LogEventHandler(up_LogEvent);

                        updateProgress(0, "Upload");
                        up.upload(fw);
                        updateProgress(100, "Upload Done");
                    }
                    catch (Exception ex)
                    {
                        updateProgress(0, "ERROR: " + ex.Message);
                        log.Info(ex);
                        Console.WriteLine(ex.ToString());
                        return(false);
                    }
                    finally
                    {
                        up.close();
                    }

                    if (up.board_type == 1140 || up.board_type == 1145 || up.board_type == 1150 || up.board_type == 1151 || up.board_type == 1152 || up.board_type == 1210 || up.board_type == 1351 || up.board_type == 1352 || up.board_type == 1411 || up.board_type == 1520)
                    {//VR boards have no tone alarm
                        if (up.board_type == 1140)
                        {
                            CustomMessageBox.Show("Upload complete! Please unplug and reconnect board.");
                        }
                        else
                        {
                            CustomMessageBox.Show("Upload complete!");
                        }
                    }
                    else
                    {
                        // wait for IO firmware upgrade and boot to a mavlink state
                        CustomMessageBox.Show("Please wait for the musical tones to finish before clicking OK");
                    }
                    return(true);
                }
            }

            updateProgress(0, "ERROR: No Response from board");
            return(false);
        }
Пример #4
0
        /// <summary>
        /// Do full update - get firmware from internet
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="historyhash"></param>
        public bool update(string comport, software temp, string historyhash)
        {
            BoardDetect.boards board = BoardDetect.boards.none;

            try
            {
                updateProgress(-1, Strings.DetectingBoardVersion);

                board = BoardDetect.DetectBoard(comport);

                if (board == BoardDetect.boards.none)
                {
                    CustomMessageBox.Show(Strings.CantDetectBoardVersion);
                    return(false);
                }

                log.Info("Detected a " + board);

                updateProgress(-1, Strings.DetectedA + board);

                string baseurl = "";
                if (board == BoardDetect.boards.b2560)
                {
                    baseurl = temp.url2560.ToString();
                }
                else if (board == BoardDetect.boards.b1280)
                {
                    baseurl = temp.url.ToString();
                }
                else if (board == BoardDetect.boards.b2560v2)
                {
                    baseurl = temp.url2560_2.ToString();
                }
                else if (board == BoardDetect.boards.px4)
                {
                    baseurl = temp.urlpx4v1.ToString();
                }
                else if (board == BoardDetect.boards.px4v2)
                {
                    baseurl = temp.urlpx4v2.ToString();
                }
                else if (board == BoardDetect.boards.px4v4)
                {
                    baseurl = temp.urlpx4v4.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv40)
                {
                    baseurl = temp.urlvrbrainv40.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv45)
                {
                    baseurl = temp.urlvrbrainv45.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv50)
                {
                    baseurl = temp.urlvrbrainv50.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv51)
                {
                    baseurl = temp.urlvrbrainv51.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv52)
                {
                    baseurl = temp.urlvrbrainv52.ToString();
                }
                else if (board == BoardDetect.boards.vrcorev10)
                {
                    baseurl = temp.urlvrcorev10.ToString();
                }
                else if (board == BoardDetect.boards.vrubrainv51)
                {
                    baseurl = temp.urlvrubrainv51.ToString();
                }
                else if (board == BoardDetect.boards.vrubrainv52)
                {
                    baseurl = temp.urlvrubrainv52.ToString();
                }
                else
                {
                    CustomMessageBox.Show(Strings.InvalidBoardType);
                    return(false);
                }

                if (board < BoardDetect.boards.px4)
                {
                    if (temp.name.ToLower().Contains("arducopter"))
                    {
                        CustomMessageBox.Show(Strings.ThisBoardHasBeenRetired, Strings.Note);
                    }
                }

                if (historyhash != "")
                {
                    baseurl = getUrl(historyhash, baseurl);
                }

                // update to use mirror url
                L10N.ReplaceMirrorUrl(ref baseurl);

                log.Info("Using " + baseurl);

                // Create a request using a URL that can receive a post.
                WebRequest request = WebRequest.Create(baseurl);
                request.Timeout = 10000;
                // Set the Method property of the request to POST.
                request.Method = "GET";
                // Get the request stream.
                Stream dataStream; //= request.GetRequestStream();
                // Get the response (using statement is exception safe)
                using (WebResponse response = request.GetResponse())
                {
                    // Display the status.
                    log.Info(((HttpWebResponse)response).StatusDescription);
                    // Get the stream containing content returned by the server.
                    using (dataStream = response.GetResponseStream())
                    {
                        long bytes   = response.ContentLength;
                        long contlen = bytes;

                        byte[] buf1 = new byte[1024];

                        using (FileStream fs = new FileStream(
                                   Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar +
                                   @"firmware.hex", FileMode.Create))
                        {
                            updateProgress(0, Strings.DownloadingFromInternet);

                            long length   = response.ContentLength;
                            long progress = 0;
                            dataStream.ReadTimeout = 30000;

                            while (dataStream.CanRead)
                            {
                                try
                                {
                                    updateProgress(length == 0 ? 50 : (int)((progress * 100) / length), Strings.DownloadingFromInternet);
                                }
                                catch
                                {
                                }
                                int len = dataStream.Read(buf1, 0, 1024);
                                if (len == 0)
                                {
                                    break;
                                }
                                progress += len;
                                bytes    -= len;
                                fs.Write(buf1, 0, len);
                            }

                            fs.Close();
                        }
                        dataStream.Close();
                    }
                    response.Close();
                }

                updateProgress(100, Strings.DownloadedFromInternet);
                log.Info("Downloaded");
            }
            catch (Exception ex)
            {
                updateProgress(50, Strings.FailedDownload);
                CustomMessageBox.Show("Failed to download new firmware : " + ex.ToString());
                return(false);
            }

            MissionPlanner.Utilities.Tracking.AddFW(temp.name, board.ToString());

            return(UploadFlash(comport,
                               Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"firmware.hex", board));
        }
Пример #5
0
        /// <summary>
        /// upload to px4 standalone
        /// </summary>
        /// <param name="filename"></param>
        public bool UploadPX4(string filename)
        {
            Uploader up;

            updateProgress(0, "Reading Hex File");
            px4uploader.Firmware fw;
            try
            {
                fw = px4uploader.Firmware.ProcessFirmware(filename);
            }
            catch (Exception ex)
            {
                CustomMessageBox.Show(Strings.ErrorFirmwareFile + "\n\n" + ex.ToString(), Strings.ERROR);
                return(false);
            }

            try
            {
                // check if we are seeing heartbeats
                MainV2.comPort.BaseStream.Open();
                MainV2.comPort.giveComport = true;
                BoardDetect.boards board = BoardDetect.DetectBoard(MainV2.comPortName);

                if (MainV2.comPort.getHeartBeat().Length > 0)
                {
                    MainV2.comPort.doReboot(true);
                    MainV2.comPort.Close();

                    //specific action for VRBRAIN4 board that needs to be manually disconnected before uploading
                    if (board == BoardDetect.boards.vrbrainv40)
                    {
                        CustomMessageBox.Show("VRBRAIN 4 detected. Please unplug the board then press OK and plug back in.\n");
                    }
                }
                else
                {
                    MainV2.comPort.BaseStream.Close();
                    CustomMessageBox.Show("Please unplug the board, and then press OK and plug back in.\nMission Planner will look for 30 seconds to find the board");
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                CustomMessageBox.Show("Please unplug the board, and then press OK and plug back in.\nMission Planner will look for 30 seconds to find the board");
            }

            DateTime DEADLINE = DateTime.Now.AddSeconds(30);

            updateProgress(0, "Scanning comports");

            while (DateTime.Now < DEADLINE)
            {
                string[] allports = SerialPort.GetPortNames();

                foreach (string port in allports)
                {
                    log.Info(DateTime.Now.Millisecond + " Trying Port " + port);

                    updateProgress(-1, "Connecting");

                    try
                    {
                        up = new Uploader(port, 115200);
                    }
                    catch (Exception ex)
                    {
                        //System.Threading.Thread.Sleep(50);
                        Console.WriteLine(ex.Message);
                        continue;
                    }

                    try
                    {
                        up.identify();
                        updateProgress(-1, "Identify");
                        log.InfoFormat("Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}", up.board_type, up.board_rev, up.bl_rev, up.fw_maxsize, port);

                        up.ProgressEvent += new Uploader.ProgressEventHandler(up_ProgressEvent);
                        up.LogEvent      += new Uploader.LogEventHandler(up_LogEvent);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Not There..");
                        //Console.WriteLine(ex.Message);
                        up.close();
                        continue;
                    }

                    // test if pausing here stops - System.TimeoutException: The write timed out.
                    System.Threading.Thread.Sleep(500);

                    try
                    {
                        up.verifyotp();

                        if (up.libre)
                        {
                            MissionPlanner.Utilities.Tracking.AddEvent("FWUpload", "verifyotp", "libre", "");
                        }
                        else
                        {
                            MissionPlanner.Utilities.Tracking.AddEvent("FWUpload", "verifyotp", "Pass", "");
                        }
                    }
                    catch (Org.BouncyCastle.Security.InvalidKeyException ex)
                    {
                        MissionPlanner.Utilities.Tracking.AddEvent("FWUpload", "verifyotp", "InvalidKeyException", "");
                        log.Error(ex);
                        CustomMessageBox.Show("You are using unsupported hardware.\nThis board does not contain a valid certificate of authenticity.\nPlease contact your hardware vendor about signing your hardware.", "Invalid Cert");
                        up.skipotp = true;
                    }
                    catch (FormatException ex)
                    {
                        MissionPlanner.Utilities.Tracking.AddEvent("FWUpload", "verifyotp", "FormatException", "");
                        log.Error(ex);
                        CustomMessageBox.Show("You are using unsupported hardware.\nThis board does not contain a valid certificate of authenticity.\nPlease contact your hardware vendor about signing your hardware.", "Invalid Cert");
                        up.skipotp = true;
                    }
                    catch (IOException ex)
                    {
                        MissionPlanner.Utilities.Tracking.AddEvent("FWUpload", "verifyotp", "IOException", "");
                        log.Error(ex);
                        CustomMessageBox.Show("lost communication with the board.", "lost comms");
                        up.close();
                        return(false);
                    }
                    catch (TimeoutException ex)
                    {
                        MissionPlanner.Utilities.Tracking.AddEvent("FWUpload", "verifyotp", "TimeoutException", "");
                        log.Error(ex);
                        CustomMessageBox.Show("lost communication with the board.", "lost comms");
                        up.close();
                        return(false);
                    }
                    catch (Exception ex)
                    {
                        MissionPlanner.Utilities.Tracking.AddEvent("FWUpload", "verifyotp", "Exception", "");
                        log.Error(ex);
                        CustomMessageBox.Show("lost communication with the board. " + ex.ToString(), "lost comms");
                        up.close();
                        return(false);
                    }

                    try
                    {
                        up.currentChecksum(fw);
                    }
                    catch (IOException ex)
                    {
                        log.Error(ex);
                        CustomMessageBox.Show("lost communication with the board.", "lost comms");
                        up.close();
                        return(false);
                    }
                    catch
                    {
                        up.__reboot();
                        up.close();
                        CustomMessageBox.Show("No need to upload. already on the board");
                        return(true);
                    }

                    try
                    {
                        updateProgress(0, "Upload");
                        up.upload(fw);
                        updateProgress(100, "Upload Done");
                    }
                    catch (Exception ex)
                    {
                        updateProgress(0, "ERROR: " + ex.Message);
                        log.Info(ex);
                        Console.WriteLine(ex.ToString());
                        return(false);
                    }
                    finally
                    {
                        up.close();
                    }

                    // wait for IO firmware upgrade and boot to a mavlink state
                    CustomMessageBox.Show("Please wait for the musical tones to finish before clicking OK");

                    return(true);
                }
            }

            updateProgress(0, "ERROR: No Response from board");
            return(false);
        }
Пример #6
0
        /// <summary>
        /// Do full update - get firmware from internet
        /// </summary>
        /// <param name="temp"></param>
        /// <param name="historyhash"></param>
        public bool update(string comport, software temp, string historyhash)
        {
            BoardDetect.boards board = BoardDetect.boards.none;

            try
            {
                updateProgress(-1, Strings.DetectingBoardVersion);

                board = BoardDetect.DetectBoard(comport);

                if (board == BoardDetect.boards.none)
                {
                    CustomMessageBox.Show(Strings.CantDetectBoardVersion);
                    return(false);
                }

                int apmformat_version = -1; // fail continue

                if (board != BoardDetect.boards.px4 && board != BoardDetect.boards.px4v2 && board != BoardDetect.boards.vrbrainv40 && board != BoardDetect.boards.vrbrainv45 && board != BoardDetect.boards.vrbrainv50 && board != BoardDetect.boards.vrbrainv51 && board != BoardDetect.boards.vrbrainv52 && board != BoardDetect.boards.vrherov10 && board != BoardDetect.boards.vrubrainv51 && board != BoardDetect.boards.vrubrainv52 && board != BoardDetect.boards.vrgimbalv20 && board != BoardDetect.boards.vrugimbalv11)
                {
                    try
                    {
                        apmformat_version = BoardDetect.decodeApVar(comport, board);
                    }
                    catch { }

                    if (apmformat_version != -1 && apmformat_version != temp.k_format_version)
                    {
                        if (DialogResult.No == CustomMessageBox.Show(Strings.EppromChanged, String.Format(Strings.EppromFormatChanged, apmformat_version, temp.k_format_version), MessageBoxButtons.YesNo))
                        {
                            CustomMessageBox.Show(Strings.PleaseConnectAndBackupConfig);
                            return(false);
                        }
                    }
                }


                log.Info("Detected a " + board);

                updateProgress(-1, Strings.DetectedA + board);

                string baseurl = "";
                if (board == BoardDetect.boards.b2560)
                {
                    baseurl = temp.url2560.ToString();
                }
                else if (board == BoardDetect.boards.b1280)
                {
                    baseurl = temp.url.ToString();
                }
                else if (board == BoardDetect.boards.b2560v2)
                {
                    baseurl = temp.url2560_2.ToString();
                }
                else if (board == BoardDetect.boards.px4)
                {
                    baseurl = temp.urlpx4v1.ToString();
                }
                else if (board == BoardDetect.boards.px4v2)
                {
                    baseurl = temp.urlpx4v2.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv40)
                {
                    baseurl = temp.urlvrbrainv40.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv45)
                {
                    baseurl = temp.urlvrbrainv45.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv50)
                {
                    baseurl = temp.urlvrbrainv50.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv51)
                {
                    baseurl = temp.urlvrbrainv51.ToString();
                }
                else if (board == BoardDetect.boards.vrbrainv52)
                {
                    baseurl = temp.urlvrbrainv52.ToString();
                }
                else if (board == BoardDetect.boards.vrherov10)
                {
                    baseurl = temp.urlvrherov10.ToString();
                }
                else if (board == BoardDetect.boards.vrubrainv51)
                {
                    baseurl = temp.urlvrubrainv51.ToString();
                }
                else if (board == BoardDetect.boards.vrubrainv52)
                {
                    baseurl = temp.urlvrubrainv52.ToString();
                }
                else if (board == BoardDetect.boards.vrgimbalv20)
                {
                    baseurl = temp.urlvrgimbalv20.ToString();
                }
                else if (board == BoardDetect.boards.vrugimbalv11)
                {
                    baseurl = temp.urlvrugimbalv11.ToString();
                }
                else
                {
                    CustomMessageBox.Show(Strings.InvalidBoardType);
                    return(false);
                }

                if (board < BoardDetect.boards.px4)
                {
                    if (temp.name.ToLower().Contains("arducopter"))
                    {
                        CustomMessageBox.Show(Strings.ThisBoardHasBeenRetired, Strings.Note);
                    }
                }

                if (historyhash != "")
                {
                    baseurl = getUrl(historyhash, baseurl);
                }

                // update to use mirror url
                Localizations.ReplaceMirrorUrl(ref baseurl);

                log.Info("Using " + baseurl);

                // Create a request using a URL that can receive a post.
                WebRequest request = WebRequest.Create(baseurl);
                request.Timeout = 10000;
                // Set the Method property of the request to POST.
                request.Method = "GET";
                // Get the request stream.
                Stream dataStream; //= request.GetRequestStream();
                // Get the response.
                WebResponse response = request.GetResponse();
                // Display the status.
                log.Info(((HttpWebResponse)response).StatusDescription);
                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();

                long bytes   = response.ContentLength;
                long contlen = bytes;

                byte[] buf1 = new byte[1024];

                FileStream fs = new FileStream(Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"firmware.hex", FileMode.Create);

                updateProgress(0, Strings.DownloadingFromInternet);

                dataStream.ReadTimeout = 30000;

                while (dataStream.CanRead)
                {
                    try
                    {
                        updateProgress(50, Strings.DownloadingFromInternet);
                    }
                    catch { }
                    int len = dataStream.Read(buf1, 0, 1024);
                    if (len == 0)
                    {
                        break;
                    }
                    bytes -= len;
                    fs.Write(buf1, 0, len);
                }

                fs.Close();
                dataStream.Close();
                response.Close();

                updateProgress(100, Strings.DownloadedFromInternet);
                log.Info("Downloaded");
            }
            catch (Exception ex)
            {
                updateProgress(50, Strings.FailedDownload);
                CustomMessageBox.Show("Failed to download new firmware : " + ex.ToString());
                return(false);
            }

            MissionPlanner.Utilities.Tracking.AddFW(temp.name, board.ToString());

            return(UploadFlash(comport, Path.GetDirectoryName(Application.ExecutablePath) + Path.DirectorySeparatorChar + @"firmware.hex", board));
        }
Пример #7
0
        /// <summary>
        /// Detect board version
        /// </summary>
        /// <param name="port"></param>
        /// <returns> boards enum value</returns>
        public static boards DetectBoard(string port, List <DeviceInfo> ports)
        {
            var t    = Type.GetType("Mono.Runtime");
            var MONO = (t != null);

            SerialPort serialPort = new SerialPort();

            serialPort.PortName = port;

            if (!MONO)
            {
                try
                {
                    // check the device reported productname

                    foreach (var item in ports)
                    {
                        log.InfoFormat("{0}: {1} - {2}", item.name, item.description, item.board);

                        //if (port.ToLower() == item.name.ToLower())
                        {
                            //USB\VID_0483&PID_DF11   -- stm32 bootloader
                            //USB\VID_1209&PID_5740   -- ardupilot chibios

                            // new style bootloader
                            if (item.hardwareid.StartsWith(@"USB\VID_0483&PID_5740") ||
                                Regex.IsMatch(item.hardwareid, "VID_2DAE") ||
                                Regex.IsMatch(item.hardwareid, "VID_3162") ||
                                item.hardwareid.StartsWith(@"USB\VID_1209&PID_5740"))
                            {
                                if (item.board == "fmuv2" || item.board.ToLower() == "fmuv2-bl")
                                {
                                    log.Info("is a fmuv2");
                                    return(boards.px4v2);
                                }

                                if (item.board == "fmuv3" || item.board.ToLower() == "fmuv3-bl")
                                {
                                    log.Info("is a fmuv3");
                                    return(boards.px4v3);
                                }

                                if (item.board == "fmuv4" || item.board.ToLower() == "fmuv4-bl")
                                {
                                    log.Info("is a fmuv4");
                                    return(boards.px4v4);
                                }

                                if (item.board == "fmuv5" || item.board.ToLower() == "fmuv5-bl")
                                {
                                    log.Info("is a fmuv5");
                                }

                                if (item.board == "revo-mini" || item.board.ToLower() == "revo-mini-bl")
                                {
                                    log.Info("is a revo-mini");
                                    //return boards.revomini;
                                }

                                if (item.board == "mini-pix" || item.board.ToLower() == "mini-pix-bl")
                                {
                                    log.Info("is a mini-pix");
                                    //return boards.minipix;
                                }

                                if (item.board == "mindpx-v2" || item.board.ToLower() == "mindpx-v2-bl")
                                {
                                    log.Info("is a mindpx-v2");
                                    //return boards.mindpxv2;
                                }

                                chbootloader = item.board.Replace("-bl", "").Replace("-Bl", "").Replace("-BL", "");
                                return(boards.chbootloader);
                            }

                            // old style bootloader

                            else if (item.board == "PX4 FMU v5.x")
                            {
                                //USB\VID_26AC&PID_0032\0
                                log.Info("is a PX4 FMU v5.x (fmuv5)");
                                return(boards.fmuv5);
                            }

                            else if (item.board == "PX4 FMU v4.x")
                            {
                                log.Info("is a px4v4 pixracer");
                                return(boards.px4v4);
                            }

                            else if (item.board == "PX4 FMU v2.x")
                            {
                                CustomMessageBox.Show(Strings.PleaseUnplugTheBoardAnd);

                                DateTime DEADLINE = DateTime.Now.AddSeconds(30);

                                while (DateTime.Now < DEADLINE)
                                {
                                    string[] allports = SerialPort.GetPortNames();

                                    foreach (string port1 in allports)
                                    {
                                        log.Info(DateTime.Now.Millisecond + " Trying Port " + port1);
                                        try
                                        {
                                            using (var up = new Uploader(port1, 115200))
                                            {
                                                up.identify();
                                                Console.WriteLine("Found board type {0} brdrev {1} blrev {2} fwmax {3} chip {5:X} chipdes {6} on {4}", up.board_type,
                                                                  up.board_rev, up.bl_rev, up.fw_maxsize, port1, up.chip, up.chip_desc);

                                                if (up.fw_maxsize == 2080768 && up.board_type == 9 && up.bl_rev >= 5)
                                                {
                                                    log.Info("is a px4v3");
                                                    return(boards.px4v3);
                                                }
                                                else
                                                {
                                                    log.Info("is a px4v2");
                                                    return(boards.px4v2);
                                                }
                                            }
                                        }
                                        catch (Exception ex)
                                        {
                                            log.Error(ex);
                                        }
                                    }
                                }

                                log.Info("Failed to detect px4 board type");
                                return(boards.none);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(ex);
                }

                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort"); // Win32_USBControllerDevice
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                foreach (ManagementObject obj2 in searcher.Get())
                {
                    log.InfoFormat("-----------------------------------");
                    log.InfoFormat("Win32_USBDevice instance");
                    log.InfoFormat("-----------------------------------");

                    foreach (var item in obj2.Properties)
                    {
                        log.InfoFormat("{0}: {1}", item.Name, item.Value);
                    }

                    // check vid and pid
                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
                    {
                        // check port name as well
                        if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
                        {
                            log.Info("is a 2560-2");
                            return(boards.b2560v2);
                        }
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0010"))
                    {
                        // check port name as well
                        //if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
                        {
                            log.Info("is a px4");
                            return(boards.px4);
                        }
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0032"))
                    {
                        // check port name as well
                        //if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
                        {
                            log.Info("is a CUAVv5");
                            chbootloader = "CUAVv5";
                            return(boards.chbootloader);
                        }
                    }
                    // chibios or normal px4
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_0483&PID_5740") ||
                             obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0011") ||
                             obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_1209&PID_5740"))
                    {
                        CustomMessageBox.Show(Strings.PleaseUnplugTheBoardAnd);

                        DateTime DEADLINE = DateTime.Now.AddSeconds(30);

                        while (DateTime.Now < DEADLINE)
                        {
                            string[] allports = SerialPort.GetPortNames();

                            foreach (string port1 in allports)
                            {
                                log.Info(DateTime.Now.Millisecond + " Trying Port " + port1);
                                try
                                {
                                    using (var up = new Uploader(port1, 115200))
                                    {
                                        up.identify();
                                        Console.WriteLine("Found board type {0} brdrev {1} blrev {2} fwmax {3} chip {5:X} chipdes {6} on {4}", up.board_type,
                                                          up.board_rev, up.bl_rev, up.fw_maxsize, port1, up.chip, up.chip_desc);

                                        if (up.fw_maxsize == 2080768 && up.board_type == 9 && up.bl_rev >= 5)
                                        {
                                            log.Info("is a px4v3");
                                            return(boards.px4v3);
                                        }
                                        else if (up.fw_maxsize == 2080768 && up.board_type == 50 && up.bl_rev >= 5)
                                        {
                                            log.Info("is a fmuv5");
                                            return(boards.fmuv5);
                                        }
                                        else
                                        {
                                            log.Info("is a px4v2");
                                            return(boards.px4v2);
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    log.Error(ex);
                                }
                            }
                        }

                        log.Info("Failed to detect px4 board type");
                        return(boards.none);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0021"))
                    {
                        log.Info("is a px4v3 X2.1");
                        return(boards.px4v3);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012"))
                    {
                        log.Info("is a px4v4 pixracer");
                        return(boards.px4v4);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0013"))
                    {
                        log.Info("is a px4v4pro pixhawk 3 pro");
                        return(boards.px4v4pro);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0001"))
                    {
                        log.Info("is a px4v2 bootloader");
                        return(boards.px4v2);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016"))
                    {
                        return(boards.px4rl);
                    }
                    //|| obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0013") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0014") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0015") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016")

                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1140"))
                    {
                        log.Info("is a vrbrain 4.0 bootloader");
                        return(boards.vrbrainv40);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1145"))
                    {
                        log.Info("is a vrbrain 4.5 bootloader");
                        return(boards.vrbrainv45);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1150"))
                    {
                        log.Info("is a vrbrain 5.0 bootloader");
                        return(boards.vrbrainv50);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1151"))
                    {
                        log.Info("is a vrbrain 5.1 bootloader");
                        return(boards.vrbrainv51);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1152"))
                    {
                        log.Info("is a vrbrain 5.2 bootloader");
                        return(boards.vrbrainv52);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1154"))
                    {
                        log.Info("is a vrbrain 5.4 bootloader");
                        return(boards.vrbrainv54);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1910"))
                    {
                        log.Info("is a vrbrain core 1.0 bootloader");
                        return(boards.vrcorev10);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1351"))
                    {
                        log.Info("is a vrubrain 5.1 bootloader");
                        return(boards.vrubrainv51);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1352"))
                    {
                        log.Info("is a vrubrain 5.2 bootloader");
                        return(boards.vrubrainv52);
                    }
                    else if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_1FC9&PID_001C"))
                    {
                        log.Info("is a NXP RDDRONE-FMUK66");
                        return(boards.nxpfmuk66);
                    }
                }
            }
            else
            {
                // if its mono
                if (CustomMessageBox.DialogResult.Yes ==
                    CustomMessageBox.Show("Is this a APM 2+?", "APM 2+", CustomMessageBox.MessageBoxButtons.YesNo))
                {
                    return(boards.b2560v2);
                }
                else
                {
                    if (CustomMessageBox.DialogResult.Yes ==
                        CustomMessageBox.Show("Is this a CUBE/PX4/PIXHAWK/PIXRACER?", "PX4/PIXHAWK",
                                              CustomMessageBox.MessageBoxButtons.YesNo))
                    {
                        if (CustomMessageBox.DialogResult.Yes ==
                            CustomMessageBox.Show("Is this a PIXRACER?", "PIXRACER",
                                                  CustomMessageBox.MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v4);
                        }
                        else if (CustomMessageBox.DialogResult.Yes ==
                                 CustomMessageBox.Show("Is this a CUBE?", "CUBE",
                                                       CustomMessageBox.MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v3);
                        }
                        else if (CustomMessageBox.DialogResult.Yes ==
                                 CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK",
                                                       CustomMessageBox.MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v2);
                        }

                        return(boards.px4);
                    }
                    else
                    {
                        return(boards.b2560);
                    }
                }
            }

            if (CustomMessageBox.DialogResult.Yes ==
                CustomMessageBox.Show("Is this a Linux board?", "Linux", CustomMessageBox.MessageBoxButtons.YesNo))
            {
                if (CustomMessageBox.DialogResult.Yes ==
                    CustomMessageBox.Show("Is this Bebop2?", "Bebop2", CustomMessageBox.MessageBoxButtons.YesNo))
                {
                    return(boards.bebop2);
                }

                if (CustomMessageBox.DialogResult.Yes == CustomMessageBox.Show("Is this Disco?", "Disco",
                                                                               CustomMessageBox.MessageBoxButtons.YesNo))
                {
                    return(boards.disco);
                }
            }

            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }

            serialPort.DtrEnable = true;
            serialPort.BaudRate  = 57600;
            serialPort.Open();

            Thread.Sleep(100);

            int a = 0;

            while (a < 20) // 20 * 50 = 1 sec
            {
                //Console.WriteLine("write " + DateTime.Now.Millisecond);
                serialPort.DiscardInBuffer();
                serialPort.Write(new byte[] { (byte)'0', (byte)' ' }, 0, 2);
                a++;
                Thread.Sleep(50);

                //Console.WriteLine("btr {0}", serialPort.BytesToRead);
                if (serialPort.BytesToRead >= 2)
                {
                    byte b1 = (byte)serialPort.ReadByte();
                    byte b2 = (byte)serialPort.ReadByte();
                    if (b1 == 0x14 && b2 == 0x10)
                    {
                        serialPort.Close();
                        log.Info("is a 1280");
                        return(boards.b1280);
                    }
                }
            }

            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }

            log.Warn("Not a 1280");

            Thread.Sleep(500);

            serialPort.DtrEnable = true;
            serialPort.BaudRate  = 115200;
            serialPort.Open();

            Thread.Sleep(100);

            a = 0;
            while (a < 4)
            {
                byte[] temp = new byte[] { 0x6, 0, 0, 0, 0 };
                temp = BoardDetect.genstkv2packet(serialPort, temp);
                a++;
                Thread.Sleep(50);

                try
                {
                    if (temp[0] == 6 && temp[1] == 0 && temp.Length == 2)
                    {
                        serialPort.Close();
                        //HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2341&PID_0010\640333439373519060F0\Device Parameters
                        if (!MONO)
                        {
                            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort");
                            // Win32_USBControllerDevice
                            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                            foreach (ManagementObject obj2 in searcher.Get())
                            {
                                //Console.WriteLine("Dependant : " + obj2["Dependent"]);

                                // all apm 1-1.4 use a ftdi on the imu board.

                                /*    obj2.Properties.ForEach(x =>
                                 * {
                                 *  try
                                 *  {
                                 *      log.Info(((PropertyData)x).Name.ToString() + " = " + ((PropertyData)x).Value.ToString());
                                 *  }
                                 *  catch { }
                                 * });
                                 */
                                // check vid and pid
                                if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
                                {
                                    // check port name as well
                                    if (
                                        obj2.Properties["Name"].Value.ToString()
                                        .ToUpper()
                                        .Contains(serialPort.PortName.ToUpper()))
                                    {
                                        log.Info("is a 2560-2");
                                        return(boards.b2560v2);
                                    }
                                }
                            }

                            log.Info("is a 2560");
                            return(boards.b2560);
                        }
                    }
                }
                catch
                {
                }
            }

            serialPort.Close();
            log.Warn("Not a 2560");

            if (CustomMessageBox.DialogResult.Yes == CustomMessageBox.Show("Is this a APM 2+?", "APM 2+",
                                                                           CustomMessageBox.MessageBoxButtons.YesNo))
            {
                return(boards.b2560v2);
            }
            else
            {
                if (CustomMessageBox.DialogResult.Yes ==
                    CustomMessageBox.Show("Is this a PX4/PIXHAWK?", "PX4/PIXHAWK",
                                          CustomMessageBox.MessageBoxButtons.YesNo))
                {
                    if (CustomMessageBox.DialogResult.Yes ==
                        CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK",
                                              CustomMessageBox.MessageBoxButtons.YesNo))
                    {
                        return(boards.px4v2);
                    }

                    return(boards.px4);
                }
                else
                {
                    return(boards.b2560);
                }
            }
        }
Пример #8
0
        /// <summary>
        /// upload to px4 standalone
        /// </summary>
        /// <param name="filename"></param>
        public bool UploadPX4(string filename)
        {
            Uploader up;

            updateProgress(0, "Reading Hex File");
            px4uploader.Firmware fw;
            try
            {
                fw = px4uploader.Firmware.ProcessFirmware(filename);
            }
            catch (Exception ex)
            {
                CustomMessageBox.Show(Strings.ErrorFirmwareFile + "\n\n" + ex.ToString(), Strings.ERROR);
                return(false);
            }

            try
            {
                // check if we are seeing heartbeats
                MainV2.comPort.BaseStream.Open();
                MainV2.comPort.giveComport = true;
                BoardDetect.boards board = BoardDetect.DetectBoard(MainV2.comPortName);

                if (MainV2.comPort.getHeartBeat().Length > 0)
                {
                    MainV2.comPort.doReboot(true);
                    MainV2.comPort.Close();

                    //specific action for VRBRAIN4 board that needs to be manually disconnected before uploading
                    if (board == BoardDetect.boards.vrbrainv40)
                    {
                        CustomMessageBox.Show(
                            "VRBRAIN 4 detected. Please unplug the board then press OK and plug back in.\n");
                    }
                }
                else
                {
                    MainV2.comPort.BaseStream.Close();
                    CustomMessageBox.Show(Strings.PleaseUnplugTheBoardAnd);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                CustomMessageBox.Show(Strings.PleaseUnplugTheBoardAnd);
            }

            DateTime DEADLINE = DateTime.Now.AddSeconds(30);

            updateProgress(0, "Scanning comports");

            while (DateTime.Now < DEADLINE)
            {
                string[] allports = SerialPort.GetPortNames();

                foreach (string port in allports)
                {
                    log.Info(DateTime.Now.Millisecond + " Trying Port " + port);

                    updateProgress(-1, "Connecting");

                    try
                    {
                        up = new Uploader(port, 115200);
                    }
                    catch (Exception ex)
                    {
                        //System.Threading.Thread.Sleep(50);
                        Console.WriteLine(ex.Message);
                        continue;
                    }

                    try
                    {
                        up.identify();
                        updateProgress(-1, "Identify");
                        log.InfoFormat("Found board type {0} boardrev {1} bl rev {2} fwmax {3} on {4}", up.board_type,
                                       up.board_rev, up.bl_rev, up.fw_maxsize, port);

                        up.ProgressEvent += new Uploader.ProgressEventHandler(up_ProgressEvent);
                        up.LogEvent      += new Uploader.LogEventHandler(up_LogEvent);
                    }
                    catch (Exception)
                    {
                        Console.WriteLine("Not There..");
                        //Console.WriteLine(ex.Message);
                        up.close();
                        continue;
                    }

                    // test if pausing here stops - System.TimeoutException: The write timed out.
                    System.Threading.Thread.Sleep(500);

                    try
                    {
                        up.currentChecksum(fw);
                    }
                    catch (IOException ex)
                    {
                        log.Error(ex);
                        CustomMessageBox.Show("lost communication with the board.", "lost comms");
                        up.close();
                        return(false);
                    }
                    catch
                    {
                        up.__reboot();
                        up.close();
                        CustomMessageBox.Show(Strings.NoNeedToUpload);
                        return(true);
                    }

                    try
                    {
                        updateProgress(0, "Upload");
                        up.upload(fw);
                        updateProgress(100, "Upload Done");
                    }
                    catch (Exception ex)
                    {
                        updateProgress(0, "ERROR: " + ex.Message);
                        log.Info(ex);
                        Console.WriteLine(ex.ToString());
                        return(false);
                    }
                    finally
                    {
                        up.close();
                    }

                    // wait for IO firmware upgrade and boot to a mavlink state
                    CustomMessageBox.Show(Strings.PleaseWaitForTheMusicalTones);

                    return(true);
                }
            }

            updateProgress(0, "ERROR: No Response from board");
            return(false);
        }
        /// <summary>
        /// Detect board version
        /// </summary>
        /// <param name="port"></param>
        /// <returns> boards enum value</returns>
        public static boards DetectBoard(string port)
        {
            SerialPort serialPort = new SerialPort();

            serialPort.PortName = port;

            if (!MainV2.MONO)
            {
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort"); // Win32_USBControllerDevice
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                foreach (ManagementObject obj2 in searcher.Get())
                {
                    Console.WriteLine("PNPID: " + obj2.Properties["PNPDeviceID"].Value.ToString());

                    // check vid and pid
                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
                    {
                        // check port name as well
                        if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
                        {
                            log.Info("is a 2560-2");
                            return(boards.b2560v2);
                        }
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0010"))
                    {
                        // check port name as well
                        //if (obj2.Properties["Name"].Value.ToString().ToUpper().Contains(serialPort.PortName.ToUpper()))
                        {
                            log.Info("is a px4");
                            return(boards.px4);
                        }
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0011"))
                    {
                        log.Info("is a px4v2");
                        return(boards.px4v2);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012"))
                    {
                        log.Info("is a px4v4 pixracer");
                        return(boards.px4v4);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0001"))
                    {
                        log.Info("is a px4v2 bootloader");
                        return(boards.px4v2);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016"))
                    {
                        log.Info("is a px4v2 bootloader");
                        CustomMessageBox.Show(
                            "You appear to have a bootloader with a bad PID value, please update your bootloader.");
                        return(boards.px4v2);
                    }

                    //|| obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0012") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0013") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0014") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0015") || obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_26AC&PID_0016")

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1140"))
                    {
                        log.Info("is a vrbrain 4.0 bootloader");
                        return(boards.vrbrainv40);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1145"))
                    {
                        log.Info("is a vrbrain 4.5 bootloader");
                        return(boards.vrbrainv45);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1150"))
                    {
                        log.Info("is a vrbrain 5.0 bootloader");
                        return(boards.vrbrainv50);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1151"))
                    {
                        log.Info("is a vrbrain 5.1 bootloader");
                        return(boards.vrbrainv51);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1152"))
                    {
                        log.Info("is a vrbrain 5.2 bootloader");
                        return(boards.vrbrainv52);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1910"))
                    {
                        log.Info("is a vrbrain core 1.0 bootloader");
                        return(boards.vrcorev10);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1351"))
                    {
                        log.Info("is a vrubrain 5.1 bootloader");
                        return(boards.vrubrainv51);
                    }

                    if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_27AC&PID_1352"))
                    {
                        log.Info("is a vrubrain 5.2 bootloader");
                        return(boards.vrubrainv52);
                    }
                }
            }
            else
            {
                // if its mono
                if (DialogResult.Yes == CustomMessageBox.Show("Is this a APM 2+?", "APM 2+", MessageBoxButtons.YesNo))
                {
                    return(boards.b2560v2);
                }
                else
                {
                    if (DialogResult.Yes ==
                        CustomMessageBox.Show("Is this a PX4/PIXHAWK/PIXRACER?", "PX4/PIXHAWK", MessageBoxButtons.YesNo))
                    {
                        if (DialogResult.Yes ==
                            CustomMessageBox.Show("Is this a PIXRACER?", "PIXRACER", MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v4);
                        }
                        if (DialogResult.Yes ==
                            CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK", MessageBoxButtons.YesNo))
                        {
                            return(boards.px4v2);
                        }
                        return(boards.px4);
                    }
                    else
                    {
                        return(boards.b2560);
                    }
                }
            }

            if (DialogResult.Yes == CustomMessageBox.Show("Is this a Linux board?", "Linux", MessageBoxButtons.YesNo))
            {
                if (DialogResult.Yes == CustomMessageBox.Show("Is this Bebop2?", "Bebop2", MessageBoxButtons.YesNo))
                {
                    return(boards.bebop2);
                }
            }

            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }

            serialPort.DtrEnable = true;
            serialPort.BaudRate  = 57600;
            serialPort.Open();

            Thread.Sleep(100);

            int a = 0;

            while (a < 20) // 20 * 50 = 1 sec
            {
                //Console.WriteLine("write " + DateTime.Now.Millisecond);
                serialPort.DiscardInBuffer();
                serialPort.Write(new byte[] { (byte)'0', (byte)' ' }, 0, 2);
                a++;
                Thread.Sleep(50);

                //Console.WriteLine("btr {0}", serialPort.BytesToRead);
                if (serialPort.BytesToRead >= 2)
                {
                    byte b1 = (byte)serialPort.ReadByte();
                    byte b2 = (byte)serialPort.ReadByte();
                    if (b1 == 0x14 && b2 == 0x10)
                    {
                        serialPort.Close();
                        log.Info("is a 1280");
                        return(boards.b1280);
                    }
                }
            }

            if (serialPort.IsOpen)
            {
                serialPort.Close();
            }

            log.Warn("Not a 1280");

            Thread.Sleep(500);

            serialPort.DtrEnable = true;
            serialPort.BaudRate  = 115200;
            serialPort.Open();

            Thread.Sleep(100);

            a = 0;
            while (a < 4)
            {
                byte[] temp = new byte[] { 0x6, 0, 0, 0, 0 };
                temp = BoardDetect.genstkv2packet(serialPort, temp);
                a++;
                Thread.Sleep(50);

                try
                {
                    if (temp[0] == 6 && temp[1] == 0 && temp.Length == 2)
                    {
                        serialPort.Close();
                        //HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB\VID_2341&PID_0010\640333439373519060F0\Device Parameters
                        if (!MainV2.MONO &&
                            !Thread.CurrentThread.CurrentCulture.IsChildOf(CultureInfoEx.GetCultureInfo("zh-Hans")))
                        {
                            ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_SerialPort");
                            // Win32_USBControllerDevice
                            ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
                            foreach (ManagementObject obj2 in searcher.Get())
                            {
                                //Console.WriteLine("Dependant : " + obj2["Dependent"]);

                                // all apm 1-1.4 use a ftdi on the imu board.

                                /*    obj2.Properties.ForEach(x =>
                                 * {
                                 *  try
                                 *  {
                                 *      log.Info(((PropertyData)x).Name.ToString() + " = " + ((PropertyData)x).Value.ToString());
                                 *  }
                                 *  catch { }
                                 * });
                                 */
                                // check vid and pid
                                if (obj2.Properties["PNPDeviceID"].Value.ToString().Contains(@"USB\VID_2341&PID_0010"))
                                {
                                    // check port name as well
                                    if (
                                        obj2.Properties["Name"].Value.ToString()
                                        .ToUpper()
                                        .Contains(serialPort.PortName.ToUpper()))
                                    {
                                        log.Info("is a 2560-2");
                                        return(boards.b2560v2);
                                    }
                                }
                            }

                            log.Info("is a 2560");
                            return(boards.b2560);
                        }
                    }
                }
                catch
                {
                }
            }

            serialPort.Close();
            log.Warn("Not a 2560");

            if (DialogResult.Yes == CustomMessageBox.Show("Is this a APM 2+?", "APM 2+", MessageBoxButtons.YesNo))
            {
                return(boards.b2560v2);
            }
            else
            {
                if (DialogResult.Yes ==
                    CustomMessageBox.Show("Is this a PX4/PIXHAWK?", "PX4/PIXHAWK", MessageBoxButtons.YesNo))
                {
                    if (DialogResult.Yes ==
                        CustomMessageBox.Show("Is this a PIXHAWK?", "PIXHAWK", MessageBoxButtons.YesNo))
                    {
                        return(boards.px4v2);
                    }
                    return(boards.px4);
                }
                else
                {
                    return(boards.b2560);
                }
            }
        }