private void tsConnect_Click(object sender, EventArgs e) { try { if (IsConnected()) { if (m_spPort.IsOpen) { m_spPort.Close(); } if (m_cmdClient.IsRun) { m_cmdClient.Stop(); } #if Telnet_Enable if ((m_telnet != null && m_telnet.IsOpenConnection())) { if (m_telnet != null && m_telnet.IsOpenConnection()) { m_telnet.Close(); } } #endif m_connection = Config.ConnectionType.NONE; } else { IPSettings dlg = new IPSettings(); if (m_connection == Config.ConnectionType.NETWORK) { dlg.rbNetwork.Checked = true; } else { dlg.rbSerialPort.Checked = true; } if (m_address != null) { dlg.tIPAddress.Text = m_address; } if (dlg.ShowDialog() == DialogResult.OK) { if (dlg.rbNetwork.Checked == true) { #if Telnet_Enable if (dlg.tIPAddress.Text.Length < 7) { MessageBox.Show("Unknown host"); return; } connect_state = CmdPing(dlg.tIPAddress.Text); if (connect_state != "Connect") { MessageBox.Show(connect_state); return; } else { m_address = dlg.tIPAddress.Text; //tsConnect.Enabled = true; //Startup ftp server StartUpFtpServer(m_address); //Startup telnet StartUpTelnet(m_address); //StartUpTelnetXP(m_address); } m_connection = Config.ConnectionType.NETWORK; #else try { //m_cmdClient = new TextCmdClient(); m_pre_net = true; m_cmdClient.Host = dlg.tIPAddress.Text;// "localhost"; m_cmdClient.Port = COMMANDS_PORT; m_cmdClient.OnReceivedEventHandle = m_atc.ReceivedFunc; m_atc.SendCmd = m_cmdClient.Send; m_cmdClient.Start(); Thread.Sleep(500); m_atc.ReceivedEventHandle = ReceivedConnectReponse; m_atc.SendExit(); int count = 0; while (m_connection != Config.ConnectionType.NETWORK) { if (count < 5) { count++; m_atc.SendExit(); Thread.Sleep(800); } else { MessageBox.Show("Connection timed out!"); return; } } } catch { MessageBox.Show("Failed to open network!"); return; } m_address = dlg.tIPAddress.Text; #endif } else { m_pre_net = false; m_atc.SendCmd = m_spPort.SendData; m_spPort.ReceivedDataHandle = m_atc.ReceivedFunc; if (dlg.cbPort.Text != null && dlg.cbPort.Text.Length != 0) { try { m_spPort.PortName = dlg.cbPort.Text; m_spPort.Open(); Thread.Sleep(500); m_atc.ReceivedEventHandle = ReceivedConnectReponse; m_atc.SendExit(); int count = 0; while (m_connection != Config.ConnectionType.SERIAL_PORT) { if (count < 5) { count++; m_atc.SendExit(); Thread.Sleep(800); } else { MessageBox.Show("Connection timed out!"); if (m_spPort.IsOpen) { m_spPort.Close(); } return; } } } catch { MessageBox.Show("Failed to open serial port!"); return; } } else { MessageBox.Show("Please choose a serial port!"); } m_connection = Config.ConnectionType.SERIAL_PORT; } } else { return; } } } catch (Exception eeeee) { MessageBox.Show(eeeee.Message, "Application Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop); Application.Exit(); } SetConnectStateDisplay(); }
protected void ConnectTarget( ) { try { if(IsConnected()) { if (m_spPort.IsOpen) { m_spPort.Close(); } if (m_cmdClient.IsRun) { m_cmdClient.Stop(); } m_connection = ConnectionType.NONE; } else { IPSettings dlg = new IPSettings(); if (m_connection == ConnectionType.NETWORK) { dlg.rbNetwork.Checked = true; } else { dlg.rbSerialPort.Checked = true; } if (m_address != null) dlg.tIPAddress.Text = m_address; if (dlg.ShowDialog() == DialogResult.OK) { if(dlg.rbNetwork.Checked == true) { try { //m_cmdClient = new TextCmdClient(); m_pre_net = true; m_cmdClient.Host = dlg.tIPAddress.Text;// "localhost"; m_cmdClient.Port = COMMANDS_PORT; m_cmdClient.OnReceivedEventHandle = m_atc.ReceivedFunc; m_atc.SendCmd = m_cmdClient.Send; m_cmdClient.Start(); Thread.Sleep(500); m_atc.ReceivedEventHandle = ReceivedConnectReponse; m_atc.SendExit(); int count =0; while(m_connection != ConnectionType.NETWORK ) { if(count < 5) { count ++; m_atc.SendExit(); Thread.Sleep(800); } else { MessageBox.Show("Connection timed out!"); m_cmdClient.Stop(); return; } } } catch { MessageBox.Show("Failed to open network!"); return; } m_address = dlg.tIPAddress.Text; } else { m_pre_net = false; m_atc.SendCmd = m_spPort.SendData; m_spPort.ReceivedDataHandle = m_atc.ReceivedFunc; if(dlg.cbPort.Text != null && dlg.cbPort.Text.Length!=0) { try { m_spPort.PortName = dlg.cbPort.Text; m_spPort.Open(); Thread.Sleep(500); //#if Test m_atc.ReceivedEventHandle = ReceivedConnectReponse; m_atc.SendExit(); int count =0; while(m_connection != ConnectionType.SERIAL_PORT) { if(count < 5) { count ++; m_atc.SendExit(); Thread.Sleep(800); } else { MessageBox.Show("Connection timed out!"); if (m_spPort.IsOpen) { m_spPort.Close(); } return; } } //#endif } catch { MessageBox.Show("Failed to open serial port!"); return; } } else { MessageBox.Show("Please choose a serial port!"); } m_connection = ConnectionType.SERIAL_PORT; } } else { return; } } } catch (Exception eeeee) { MessageBox.Show(eeeee.Message, "Application Error!!!", MessageBoxButtons.OK, MessageBoxIcon.Stop); Application.Exit(); } }