Example #1
0
        private void txtCommand_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                if (this.optSerial.Checked)
                {
                    //MessageBox.Show(this.txtCommand.Text);
                    if (this.CommPort != null && this.CommPort.IsOpen)
                    {
                        this.CommPort.Write(this.txtCommand.Text + (char)13);
                        //txtLog.AppendText(this.txtCommand.Text + Environment.NewLine);

                        if (this.txtCommand.Text.Trim().ToLower() == "exit")
                        {
                            Thread.Sleep(3000);
                            this.CommPort.Close();
                            this.CommPort = null;
                            bIsConnected  = false;

                            MessageBox.Show("COMM 연결이 해제되었습니다.");
                        }
                    }
                    else
                    {
                        MessageBox.Show("COMM 연결이 되어있지 않습니다. 연결 후 사용하세요.");
                    }
                }
                else
                {
                    if (tc == null || !tc.IsConnected)
                    {
                        MessageBox.Show("텔렛 연결이 되어있지 않습니다. 연결 후 사용하세요.");
                    }
                    else
                    {
                        tc.WriteLine(this.txtCommand.Text.Trim() + (char)13);
                        //MessageDisp(tc.Read());

                        if (this.txtCommand.Text.Trim().ToLower() == "exit")
                        {
                            bIsStop = true;
                            tc.Close();
                            tc = null;
                            MessageBox.Show("텔렛 연결이 해제되었습니다.");
                        }
                    }
                }

                this.txtCommand.Text = "";
            }
        }
Example #2
0
        public void DisConnect(bool bIsMsg)
        {
            try
            {
                try
                {
                    if (CommPort != null && CommPort.IsOpen)
                    {
                        CommPort.Write("exit" + (char)13);
                        if (bIsMsg)
                        {
                            Thread.Sleep(3000);
                        }
                        CommPort.Close();
                    }
                }
                catch { }

                CommPort = null;

                try
                {
                    if (tc != null && tc.IsConnected)
                    {
                        tc.Close();
                    }
                }
                catch { }

                tc = null;

                if (bIsMsg)
                {
                    MessageBox.Show("연결이 해제되었습니다.");
                }
            }
            catch {  }

            bIsConnect    = false;
            bIsDisConnect = true;
            bIsLogon      = false;
            bIsStarted    = false;
            bIsStoped     = true;

            returnStatus("DisConnected");
        }
Example #3
0
        public void Connect(PORTData portData)
        {
            try
            {
                this.m_PortData = portData;

                if (this.m_PortData.UserID == "")
                {
                    MessageBox.Show("시리얼연결 아이디가 정확하지 않습니다");
                    return;
                }
                if (this.m_PortData.Password == "")
                {
                    MessageBox.Show("시리얼연결 패스워드가 정확하지 않습니다");
                    return;
                }

                if (this.m_PortData.bUseComm)
                {
                    if (CommPort != null)
                    {
                        CommPort.Close();
                        CommPort = null;
                    }

                    CommPort          = new SerialPort(this.m_PortData.PortName);
                    CommPort.BaudRate = this.m_PortData.nCommSpeed;

                    if (this.m_PortData.CommData == "7 Bit")
                    {
                        CommPort.DataBits = 7;
                    }
                    else if (this.m_PortData.CommData == "8 Bit")
                    {
                        CommPort.DataBits = 8;
                    }

                    if (this.m_PortData.CommParity == "none")
                    {
                        CommPort.Parity = Parity.None;
                    }
                    else if (portData.CommParity == "odd")
                    {
                        CommPort.Parity = Parity.Odd;
                    }
                    else if (portData.CommParity == "even")
                    {
                        CommPort.Parity = Parity.Even;
                    }
                    else if (portData.CommParity == "mark")
                    {
                        CommPort.Parity = Parity.Mark;
                    }
                    else if (portData.CommParity == "space")
                    {
                        CommPort.Parity = Parity.Space;
                    }

                    if (this.m_PortData.CommStop == "1 bit")
                    {
                        CommPort.StopBits = StopBits.One;
                    }
                    else if (this.m_PortData.CommStop == "1.5 bit")
                    {
                        CommPort.StopBits = StopBits.OnePointFive;
                    }
                    else if (this.m_PortData.CommStop == "2 bit")
                    {
                        CommPort.StopBits = StopBits.Two;
                    }

                    if (this.m_PortData.CommFlow == "Xon/Xoff")
                    {
                        CommPort.Handshake = Handshake.XOnXOff;
                    }
                    else if (this.m_PortData.CommFlow == "hardware")
                    {
                        CommPort.Handshake = Handshake.None;
                    }
                    else if (this.m_PortData.CommFlow == "none")
                    {
                        CommPort.Handshake = Handshake.None;
                    }

                    CommPort.RtsEnable = true;

                    CommPort.DataReceived += new SerialDataReceivedEventHandler(CommPort_DataReceived);

                    CommPort.Open();
                    Thread.Sleep(2000);

                    if (CommPort == null || !CommPort.IsOpen)
                    {
                        DisConnect(false);
                        MessageBox.Show("시리얼포트 연결에 실패하였습니다.");
                        return;
                    }

                    MessageBox.Show("시리얼포트 연결에 성공하였습니다.");
                } //UseComm

                if (this.m_PortData.bUseTelnet)
                {
                    if (this.m_PortData.TelnetServer == "")
                    {
                        MessageBox.Show("텔렛주소가 정확하지 않습니다");
                        return;
                    }
                    if (this.m_PortData.UserID == "")
                    {
                        MessageBox.Show("텔렛 아이디가 정확하지 않습니다");
                        return;
                    }
                    if (this.m_PortData.Password == "")
                    {
                        MessageBox.Show("텔렛 패스워드가 정확하지 않습니다");
                        return;
                    }

                    if (tc != null && tc.IsConnected)
                    {
                        tc.Close();
                    }
                    tc = null;

                    tc           = new TelnetConnection(this.m_PortData.TelnetServer, 23);
                    tc.strEncode = this.strEncode;
                    Thread.Sleep(2000);

                    if (tc == null || !tc.IsConnected)
                    {
                        DisConnect(false);
                        MessageBox.Show("텔렛 연결에 실패하였습니다.");
                        return;
                    }

                    MessageBox.Show("텔렛 연결에 성공하였습니다.");
                } //Use Telnet

                bIsConnect    = true;
                bIsDisConnect = false;
                bIsLogon      = false;
                bIsStarted    = false;
                bIsStoped     = false;

                returnStatus("Connected");
            }
            catch (Exception ex)
            {
                DisConnect(false);
                MessageBox.Show("연결이 실패하였습니다:" + ex.ToString());
            }
        }
Example #4
0
        private void cmdConnect_Click(object sender, EventArgs e)
        {
            try
            {
                bIsStop = false;

                if (optSerial.Checked)
                {
                    if (this.txtID.Text.Trim() == "")
                    {
                        MessageBox.Show("아이디를 입력하세요");
                        return;
                    }
                    if (this.txtPassword.Text.Trim() == "")
                    {
                        MessageBox.Show("패스워드를 입력하세요");
                        return;
                    }

                    if (bIsConnected)
                    {
                        if (CommPort != null)
                        {
                            CommPort.Close();
                            CommPort = null;
                        }
                        bIsConnected = false;
                    }

                    CommPort          = new SerialPort(this.cmbPort.Text.Trim());
                    CommPort.BaudRate = Convert.ToInt32(this.cmbSpeed.Text.Trim());

                    if (this.cmbData.Text.Trim() == "7 Bit")
                    {
                        CommPort.DataBits = 7;
                    }
                    else if (this.cmbData.Text.Trim() == "8 Bit")
                    {
                        CommPort.DataBits = 8;
                    }

                    if (this.cmbParity.Text.Trim() == "none")
                    {
                        CommPort.Parity = Parity.None;
                    }
                    else if (this.cmbParity.Text.Trim() == "odd")
                    {
                        CommPort.Parity = Parity.Odd;
                    }
                    else if (this.cmbParity.Text.Trim() == "even")
                    {
                        CommPort.Parity = Parity.Even;
                    }
                    else if (this.cmbParity.Text.Trim() == "mark")
                    {
                        CommPort.Parity = Parity.Mark;
                    }
                    else if (this.cmbParity.Text.Trim() == "space")
                    {
                        CommPort.Parity = Parity.Space;
                    }

                    if (this.cmbStop.Text.Trim() == "1 bit")
                    {
                        CommPort.StopBits = StopBits.One;
                    }
                    else if (this.cmbStop.Text.Trim() == "1.5 bit")
                    {
                        CommPort.StopBits = StopBits.OnePointFive;
                    }
                    else if (this.cmbStop.Text.Trim() == "2 bit")
                    {
                        CommPort.StopBits = StopBits.Two;
                    }

                    if (this.cmbFlow.Text.Trim() == "Xon/Xoff")
                    {
                        CommPort.Handshake = Handshake.XOnXOff;
                    }
                    else if (this.cmbFlow.Text.Trim() == "hardware")
                    {
                        CommPort.Handshake = Handshake.None;
                    }
                    else if (this.cmbFlow.Text.Trim() == "none")
                    {
                        CommPort.Handshake = Handshake.None;
                    }

                    CommPort.RtsEnable = true;

                    CommPort.DataReceived += new SerialDataReceivedEventHandler(CommPort_DataReceived);

                    CommPort.Open();
                    bIsConnected = true;

                    string s = "";

                    try
                    {
                        s = CommLogin(this.txtID.Text.Trim(), this.txtPassword.Text.Trim());
                    }
                    catch //(Exception ex)
                    {
                        //MessageBox.Show(ex.ToString());
                        return;
                    }
                    MessageDisp(s);

                    string prompt = s.TrimEnd();
                    prompt = s.Substring(prompt.Length - 1, 1);
                    if (prompt != "$" && prompt != ">" && prompt != "#")
                    {
                        //throw new Exception("Connection failed");
                        MessageBox.Show("로그인 되지 않았습니다.");
                        return;
                    }

                    MessageBox.Show("로그인 되었습니다");
                }
                else //Telnet
                {
                    if (tc != null && tc.IsConnected)
                    {
                        tc.Close();
                    }
                    tc = null;

                    if (this.txtTelnetAddr.Text.Trim() == "")
                    {
                        MessageBox.Show("텔렛주소를 입력하세요");
                        return;
                    }
                    if (this.txtID.Text.Trim() == "")
                    {
                        MessageBox.Show("아이디를 입력하세요");
                        return;
                    }
                    if (this.txtPassword.Text.Trim() == "")
                    {
                        MessageBox.Show("패스워드를 입력하세요");
                        return;
                    }

                    string s = "";
                    tc           = new TelnetConnection(this.txtTelnetAddr.Text.Trim(), 23);
                    tc.strEncode = this.strEncode;

                    try
                    {
                        s = tc.Login(this.txtID.Text.Trim(), this.txtPassword.Text.Trim(), 200);
                    } catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString());
                        if (tc != null && tc.IsConnected)
                        {
                            tc.Close();
                        }
                        tc = null;
                        return;
                    }
                    MessageDispTelnet(s);

                    string prompt = s.TrimEnd();
                    prompt = s.Substring(prompt.Length - 1, 1);
                    //if (prompt != "$" && prompt != ">")
                    if (prompt != "$" && prompt != ">" && prompt != "#")
                    {
                        //throw new Exception("Connection failed");
                        MessageBox.Show("연결이 되지 않았습니다.");
                        return;
                    }

                    Thread t_handler = new Thread(Telnet_Receive);
                    t_handler.IsBackground = true;
                    t_handler.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }