Ejemplo n.º 1
0
        public void Logon()
        {
            try
            {
                string s = "";

                if (!bIsConnect)
                {
                    MessageBox.Show("장치가 연결이 되어있지 않습니다");
                    return;
                }

                if (bUseComm)
                {
                    try
                    {
                        s = CommLogin(this.m_PortData.UserID, this.m_PortData.Password);
                    }
                    catch (Exception ex)
                    {
                        DisConnect(false);
                        MessageBox.Show("시리얼포트 로그인 오류:" + ex.ToString());
                        return;
                    }
                    MessageDispComm(s);

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

                if (this.m_PortData.bUseTelnet)
                {
                    try
                    {
                        s = tc.Login(this.m_PortData.UserID, this.m_PortData.Password, 200);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("텔렛연결오류:" + ex.ToString());
                        DisConnect(false);
                        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");
                        DisConnect(false);
                        MessageBox.Show("텔렛이 연결되지 않았습니다.");
                        return;
                    }

                    t_handler = new Thread(Telnet_Receive);
                    t_handler.IsBackground = true;
                    t_handler.Start();
                }

                bIsLogon = true;
                returnStatus("Logon");

                MessageBox.Show("로그인 되었습니다");
            }
            catch (Exception ex)
            {
                MessageBox.Show("로그인오류:" + ex.ToString());
                DisConnect(false);
            }
        }
Ejemplo n.º 2
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());
            }
        }