public Status Disconnect()
 {
     try
     {
         _xIMUSerial.Close();
         return(Status.Good);
     }
     catch
     {
         return(Status.Bad);
     }
 }
        /// <summary>
        /// Main method.
        /// </summary>
        /// <param name="args">
        /// Unused.
        /// </param>
        static void Main(string[] args)
        {
            Console.WriteLine(Assembly.GetExecutingAssembly().GetName().Name + " " + Assembly.GetExecutingAssembly().GetName().Version.Major.ToString() + "." + Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString());
            try
            {
                while (true)
                {
                    // Connect to x-IMU
                    Console.WriteLine("Searching for x-IMU...");
                    x_IMU_API.PortAssignment[] portAssignment = (new x_IMU_API.PortScanner(true, true)).Scan();
                    x_IMU_API.xIMUserial xIMUserial = new x_IMU_API.xIMUserial(portAssignment[0].PortName);
                    xIMUserial.QuaternionDataReceived += new x_IMU_API.xIMUserial.onQuaternionDataReceived(xIMUserial_QuaternionDataReceived);
                    xIMUserial.DigitalIODataReceived += new x_IMU_API.xIMUserial.onDigitalIODataReceived(xIMUserial_DigitalIODataReceived);
                    xIMUserial.Open();
                    Console.WriteLine("Connected to x-IMU " + portAssignment[0].DeviceID + " on " + portAssignment[0].PortName + ".");

                    // Poll for key press and detect if connection lost
                    Console.WriteLine("Press Esc to exit or any other key to set cursor centre orientation (i.e. tare).");
                    int prevCount;
                    do
                    {
                        prevCount = xIMUserial.PacketsReadCounter.TotalPackets;
                        Thread.Sleep(1000);
                        if (Console.KeyAvailable == true)
                        {
                            if (Console.ReadKey(true).Key == ConsoleKey.Escape)
                            {
                                return;
                            }
                            xIMUserial.SendCommandPacket(x_IMU_API.CommandCodes.AlgorithmTare);
                        }
                    } while (prevCount != xIMUserial.PacketsReadCounter.TotalPackets);
                    Console.WriteLine("No data received from x-IMU.  Closing port.");
                    xIMUserial.Close();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: " + ex.Message);
            }
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
 /// <summary>
 /// Form close event to close serial ports.
 /// </summary>
 private void Form_main_FormClosed(object sender, FormClosedEventArgs e)
 {
     xIMUserial.Close();
     gimbalSerial.Close();
 }
示例#4
0
        /// <summary>
        /// Button Click event to upload firmware.
        /// </summary>
        private void button_bootloaderUpload_Click(object sender, EventArgs e)
        {
            // Error if file not exist
            if (!File.Exists(textBox_bootloaderFilePath.Text))
            {
                MessageBox.Show("File does not exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // Error if selected port name = "Auto"
            if (string.Equals(comboBox_portName.Text, "Auto", StringComparison.CurrentCultureIgnoreCase))
            {
                MessageBox.Show("Port name \"Auto\" cannot be used for bootloading.  Please select the port name assigned to the USB connection.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            // User confirmation
            if (MessageBox.Show("Please ensure that the selected port name is the port assigned to the x-IMU USB connection else the upload will fail." + Environment.NewLine + Environment.NewLine +
                                "The new firmware will be uploaded to the x-IMU and the current firmware will be lost." + Environment.NewLine + Environment.NewLine +
                                "Do not switch off or disconnect the x-IMU while firmware is being uploaded as this may permanently damage the x-IMU.", "Warning", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Cancel)
            {
                return;
            }

            // Disable bootloader form controls
            textBox_bootloaderFilePath.Enabled = false;
            button_bootloaderBrowse.Enabled = false;
            button_bootloaderUpload.Enabled = false;
            button_bootloaderUpload.Text = "Uploading...";
            this.Update();

            // Close port is currently open
            bool reopenPort = false;
            if (xIMUserial.IsOpen)
            {
                xIMUserial.Close();
                reopenPort = true;
            }

            // Perform firmware upload procedure
            try
            {
                // Reset device
                x_IMU_API.xIMUserial tempxIMUserial = new x_IMU_API.xIMUserial(comboBox_portName.Text);
                tempxIMUserial.Open();
                tempxIMUserial.SendCommandPacket(x_IMU_API.CommandCodes.Reset);
                tempxIMUserial.Close();

                // Run external bootloader process
                ProcessStartInfo processInfo = new ProcessStartInfo("Bootloader/16-Bit Flash Programmer.exe");
                processInfo.Arguments = "-i " + "\\\\.\\" + comboBox_portName.Text + " -b 115200 \"" + textBox_bootloaderFilePath.Text + "\"";
                processInfo.UseShellExecute = false;
                Process process = Process.Start(processInfo);
                process.WaitForExit();
                process.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            // Re-enable bootloader form controls
            textBox_bootloaderFilePath.Enabled = true;
            button_bootloaderBrowse.Enabled = true;
            button_bootloaderUpload.Enabled = true;
            button_bootloaderUpload.Text = "Upload";

            // Re-open port if was closed prior to bootload
            if (reopenPort)
            {
                xIMUserial.Open();
            }
        }