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()); } }
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()); } }