// ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ // Utilities - TODO wip // ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ //Method to check for valid connection, Invoked from start void CheckConnection() { if (netStat != Application.internetReachability) { netStat = Application.internetReachability; //Get network state switch (netStat) { case NetworkReachability.NotReachable: Console.Instance.Write(DebugLevel.Key, MessageLevel.LogError, "Internet Connection Lost"); liveConnection = connectionStatus.None; // connection is not available break; case NetworkReachability.ReachableViaCarrierDataNetwork: Console.Instance.Write(DebugLevel.Key, MessageLevel.LogError, "Internet Connection Not Reliable, Please connect to Wi-fi"); liveConnection = connectionStatus.Mobile; // connection is not really available break; case NetworkReachability.ReachableViaLocalAreaNetwork: Console.Instance.Write(DebugLevel.Key, MessageLevel.Log, "Internet Connection Live"); liveConnection = connectionStatus.Internet; // connection is available break; } } }
private Boolean Disconnect() { if (comPortClose()) { connection = connectionStatus.disconnected ; return true; } else return false; }
private void ConnectDisconnectButton_Click(object sender, RoutedEventArgs e) { if (connection == connectionStatus.disconnected) { if (Connect()){ ConnectDisconnectButton.Content = "DISCONNECT"; HRHelper.ResetHeartRate(); } else MessageBox.Show("Connection failed. Could not open COM port"); } else if (connection == connectionStatus.connected) { if (Disconnect()) ConnectDisconnectButton.Content = "CONNECT"; else MessageBox.Show("Disconnect failed. Could not close COM port"); } else { //undefined state connection = connectionStatus.disconnected; //reset } }
private Boolean Connect() { if (ComPortOpen()) { if (serialPort.IsOpen) { connection = connectionStatus.connected; return true; } } return false; }
private void button_connect_Click(object sender, EventArgs e) { MTCom.DeviceInfo devInfo = combobox_usbdevices.SelectedItem as MTCom.DeviceInfo; if (textbox_frequency.Text == "" || textbox_filename.Text == "") { MessageBox.Show("Frequency or Filename cannot be empty."); return; } //If the range is in 1 to 32767 else { int value_frequency = -1; try { value_frequency = int.Parse(textbox_frequency.Text); } catch (Exception string_message) { MessageBox.Show("Exception :{0}", string_message.Message); return; } if (value_frequency > 0 && value_frequency <= 32767) { if (devInfo != null && !mtConn.IsOpen) { try { mtConn.Open(devInfo); groupbox_devices.Enabled = false; groupbox_save.Enabled = false; textbox_status.Text = "Connected via USB"; connection = connectionStatus.CON_USB; //If the connection is successful button_connect.Enabled = false; button_disconnect.Enabled = true; timer1.Interval = int.Parse(textbox_frequency.Text); timer1.Start(); } catch (MTException ex) { MessageBox.Show(ex.Message); //Any exception occurs, close the connection textbox_status.Text = ex.Message; groupbox_save.Enabled = true; groupbox_devices.Enabled = true; button_disconnect.Enabled = false; button_connect.Enabled = true; connection = connectionStatus.CON_DISCONNECTED; timer1.Stop(); } } } else { MessageBox.Show("Please enter a valid value of Frequency (1-32767)."); return; } } }
private void timer1_Tick(object sender, EventArgs e) { if (mtConn.IsOpen) { Int32 cycleCounterTmp = 0; try { string response = mtConn.SendWaitReply("IC", 2); string getCycleCounterString = response.Substring(4, 8); cycleCounterTmp = Convert.ToInt32(getCycleCounterString, 16); //If it is screwing if (cycleCounter < cycleCounterTmp) { cycleCounter = cycleCounterTmp; textbox_status.Text = String.Format("Cycle counter is currently {0}", cycleCounter); saveTraceToFile(); } //If it is unscrewing else { //Save it to the file textbox_status.Text = String.Format("Unscrew Operation"); saveTraceToFile(); } } catch (MTException ex) { if (mtConn.IsOpen) mtConn.Close(); MessageBox.Show("Exception occured while Connection"); textbox_status.Text = ex.Message; groupbox_save.Enabled = true; groupbox_devices.Enabled = true; button_disconnect.Enabled = false; button_connect.Enabled = true; connection = connectionStatus.CON_DISCONNECTED; timer1.Stop(); } catch (FormatException) { MessageBox.Show("Format Exception occured"); } catch (OverflowException) { MessageBox.Show("OverFlow Exception occured"); } } }
private void MainForm_Load(object sender, EventArgs e) { //Enable - disable buttons in forms while loading button_connect.Enabled = true; button_disconnect.Enabled = false; textbox_status.Enabled = false; button_disconnect.Enabled = false; textbox_status.Enabled = false; textbox_filename.Enabled = false; MTCom.DeviceInfo[] devices = MTCom.GetDeviceList(); connection = connectionStatus.CON_DISCONNECTED; foreach (MTCom.DeviceInfo devInfo in devices) { if (devInfo.IfType == MTCom.ComInterface.MT_IF_USB && devInfo.DevStatus == MTCom.DeviceStatus.MT_DEVICE_READY) { switch (devInfo.DevType) { case MTCom.DeviceType.MT_DEVICE_MTF400_A: case MTCom.DeviceType.MT_DEVICE_MTF400_B: case MTCom.DeviceType.MT_DEVICE_MTF400_D: case MTCom.DeviceType.MT_DEVICE_MT_G4: combobox_usbdevices.Items.Add(devInfo); break; default: break; } } } if (combobox_usbdevices.Items.Count > 0) combobox_usbdevices.SelectedIndex = 0; }
private void button_disconnect_Click(object sender, EventArgs e) { if (mtConn.IsOpen) mtConn.Close(); //On disconnect - make the filename textbox_filename.Text = ""; textbox_status.Text = ""; textbox_status.Text = "Disconnected"; groupbox_save.Enabled = true; groupbox_devices.Enabled = true; button_connect.Enabled = true; button_disconnect.Enabled = false; connection = connectionStatus.CON_DISCONNECTED; timer1.Stop(); }
//public connectionStatus HTTP_Connection() //{ // connectionStatus connectStat = new connectionStatus(); // connectStat.error = String.Empty; // System.Net.WebRequest req = System.Net.WebRequest.Create(TestDOJUrl); // try // { // connectStat.connected = false; // var response = (HttpWebResponse)req.GetResponse(); // if (response.StatusCode == HttpStatusCode.OK) // { // // it is responsive // connectStat.error = string.Format("{0} Available", TestDOJUrl); // connectStat.connected = true; // } // else // { // connectStat.error = string.Format("{0} Returned, but with status: {1}", // TestDOJUrl, response.StatusDescription); // } // return connectStat; // } // catch (Exception ex) // { // // not available at all, for some reason // connectStat.error = string.Format("{0} unavailable: {1}", TestDOJUrl, ex.Message); // connectStat.connected = false; // return connectStat; // } //} public connectionStatus HTTP_Connection() { try { string Out = String.Empty; string Error = String.Empty; string Log = String.Empty; ExtractJNode eJson; connectionStatus connectStat = new connectionStatus(); connectStat.error = String.Empty; connectStat.connected = true; DOJWebApiUrl = ConfigurationManager.AppSettings["DOJWebApiUrl"]; System.Net.WebRequest req = System.Net.WebRequest.Create(DOJWebApiUrl); req.Method = "PUT"; req.Timeout = 100000; req.ContentType = "application/json"; byte[] sentData = Encoding.UTF8.GetBytes(connectionTestData); req.ContentLength = sentData.Length; using (Stream sendStream = req.GetRequestStream()) { sendStream.Write(sentData, 0, sentData.Length); sendStream.Close(); } System.Net.WebResponse res = req.GetResponse(); Stream ReceiveStream = res.GetResponseStream(); using (StreamReader sr = new StreamReader(ReceiveStream, Encoding.UTF8)) { Char[] read = new Char[256]; int count = sr.Read(read, 0, 256); while (count > 0) { String str = new String(read, 0, count); Out += str; count = sr.Read(read, 0, 256); } } JObject o = JObject.Parse(Out); eJson = new ExtractJNode("Messages.Code", o); string code = eJson.traverseNode(); if (code == "SYSERR") { eJson = new ExtractJNode("Messages.Message", o); string message = eJson.traverseNode(); connectStat.error = code + " : " + message; connectStat.connected = false; } return(connectStat); } catch (Exception error) { string err = error.Message; throw error; } }