Darien Figueroa / Mbed 2 deprecated repo3

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers FormConnection.cs Source File

FormConnection.cs

00001 /*******************************************************************************
00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All rights Reserved.
00003 * 
00004 * This software is protected by copyright laws of the United States and
00005 * of foreign countries. This material may also be protected by patent laws
00006 * and technology transfer regulations of the United States and of foreign
00007 * countries. This software is furnished under a license agreement and/or a
00008 * nondisclosure agreement and may only be used or reproduced in accordance
00009 * with the terms of those agreements. Dissemination of this information to
00010 * any party or parties not specified in the license agreement and/or
00011 * nondisclosure agreement is expressly prohibited.
00012 *
00013 * The above copyright notice and this permission notice shall be included
00014 * in all copies or substantial portions of the Software.
00015 *
00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00017 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00018 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00019 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00020 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00021 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00022 * OTHER DEALINGS IN THE SOFTWARE.
00023 *
00024 * Except as contained in this notice, the name of Maxim Integrated
00025 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00026 * Products, Inc. Branding Policy.
00027 *
00028 * The mere transfer of this software does not imply any licenses
00029 * of trade secrets, proprietary technology, copyrights, patents,
00030 * trademarks, maskwork rights, or any other form of intellectual
00031 * property whatsoever. Maxim Integrated Products, Inc. retains all
00032 * ownership rights.
00033 *******************************************************************************
00034 */
00035 
00036 using System;
00037 using System.Collections.Generic;
00038 using System.ComponentModel;
00039 using System.Data;
00040 using System.Drawing;
00041 using System.Linq;
00042 using System.Text;
00043 using System.Threading.Tasks;
00044 using System.Windows.Forms;
00045 
00046 using System.IO.Ports;
00047 
00048 namespace HealthSensorPlatform
00049 {
00050     public partial class FormConnection : Form
00051     {
00052         string port;
00053         bool done = false;
00054         //bool canceled = false;
00055         //bool connect = false;
00056 
00057         public bool Canceled { get; set; }
00058        // public bool Connect { get; set; }
00059 
00060         internal const Int32 WM_DEVICECHANGE = 0x219;
00061         internal const Int32 DBT_DEVICEARRIVAL = 0x8000;
00062         internal const Int32 DBT_DEVICEREMOVECOMPLETE = 0x8004;
00063 
00064         public FormConnection()
00065         {
00066             InitializeComponent();
00067             ScanPorts();
00068 
00069             port = Properties.Settings.Default.ComPort;
00070         }
00071 
00072         private void btnConnect_Click(object sender, EventArgs e)
00073         {
00074             if (cboPort.SelectedItem != null)
00075             {
00076                 port = cboPort.SelectedItem.ToString();
00077 
00078                 // Save default COM port for next application use
00079                 Properties.Settings.Default["ComPort"] = Port;
00080                 Properties.Settings.Default.Save();
00081 
00082                 done = true;
00083                 Close();
00084             }
00085             else
00086             {
00087                 MessageBox.Show("Selected port is invalid.", "Connection");
00088             }
00089         }
00090 
00091         /// <summary>
00092         /// Reset the state of the connection Form.
00093         /// </summary>
00094         public void Reset()
00095         {
00096             Canceled = false;
00097         }
00098 
00099 
00100         /// <summary>
00101         /// Returns default port saved in Properties.Settings.Default.ComPort
00102         /// if port is not selected or user selected port from drop down menu
00103         /// </summary>
00104         /// <returns></returns>
00105         public string Port
00106         {
00107             get { return port; }
00108         }
00109        
00110 
00111         private void btnScan_Click(object sender, EventArgs e)
00112         {
00113             ScanPorts();
00114 
00115         }
00116 
00117         /// <summary>
00118         /// Scan the port names
00119         /// </summary>
00120         public void ScanPorts()
00121         {
00122             string[] ports = SerialPort.GetPortNames();
00123 
00124             cboPort.Items.Clear();
00125             //cboPort.SelectedItem = null;
00126             cboPort.SelectedIndex = -1;
00127 
00128             foreach (string port in ports)
00129             {
00130                 cboPort.Items.Add(port);
00131 
00132                 if(String.Compare(port,Properties.Settings.Default.ComPort) == 0)
00133                 {
00134                     // Select the last used COM Port
00135                     cboPort.SelectedItem = Properties.Settings.Default.ComPort;
00136                 }
00137             }
00138             
00139         }
00140 
00141         private void btnCancel_Click(object sender, EventArgs e)
00142         {
00143             Canceled = true;
00144             done = true;
00145             Close();
00146         }
00147 
00148         public bool HandleWMDeviceChangeMessage(Message m)
00149         {
00150             string[] ports;
00151             bool removal = false;
00152 
00153             if (m.Msg == WM_DEVICECHANGE)
00154             {
00155                 if (m.WParam.ToInt32() == DBT_DEVICEREMOVECOMPLETE)
00156                 {
00157                     ports = SerialPort.GetPortNames();
00158 
00159                     foreach(string p in ports)
00160                     {
00161                         if (String.Compare(p, Port) == 0)
00162                         {
00163                             removal = true;
00164                             break;
00165                         }
00166                     }
00167                 }
00168 
00169             }
00170 
00171             return removal;
00172         }
00173 
00174         private void FormConnection_FormClosing(object sender, FormClosingEventArgs e)
00175         {
00176             // User clicked "X" button to close form,
00177             // action should be the same as if user clicked on "Canceled" button
00178             if (done == false)
00179                 Canceled = true;
00180         }
00181     }
00182 }