//Konfigurationsdialog zur Auswahl einer TCP-Verbindung oder eines COM-Ports bool ConfigCommunication() { if (_communicationType == _communicationNone) { SelectSrc selectSrcDialog = new SelectSrc(); // Open Source Selection Dialog DialogResult selectSrcResult = selectSrcDialog.ShowDialog(this); if (selectSrcDialog.useSocket) { // Select IP address and port number and set BinaryReader and BinaryWriter // TODO: set Protocol handler SelectTCPIP selectIpDialog = new SelectTCPIP(); DialogResult selectIpResult = selectIpDialog.ShowDialog(this); if (selectIpDialog.error) { // if an error occured or the input form was cancelled, return false return(false); } try { // Initialise an TCP Socket m_socket = new TcpClient(); m_socket.Connect(selectIpDialog.ipAddress, selectIpDialog.port); // try to connect to given IP + Port } catch (Exception ex) { if (ex is System.Net.Sockets.SocketException) { // Connect failed to connect MessageBox.Show(Resources.ConnectError + selectIpDialog.ipAddress + "\n" + ex.Message); return(false); } } m_protocolHandler = new ProtocolHandler(m_socket, this); _communicationType = _communicationSocket; return(true); } else if (selectSrcDialog.useCOM) { // Select COM Port, set Binary Reader and Writer var comport = ""; var baudrate = ""; var databits = ""; Parity parity = Parity.None; StopBits stopbits = StopBits.One; var comDialog = new ChooseCom(); //Open window if (comDialog.ShowDialog(this) == DialogResult.OK) { comport = comDialog.Port; //Save selected COM-Port baudrate = comDialog.Baud; //Save selected BAUD-Rate databits = comDialog.DataBits; //Save selected Data Bits switch (comDialog.Parity) //Save selected Parity { case "None": parity = Parity.None; break; case "Odd": parity = Parity.Odd; break; case "Even": parity = Parity.Even; break; case "Mark": parity = Parity.Mark; break; case "Space": parity = Parity.Space; break; } switch (comDialog.StopBits) //Save selected Stop Bits { case "One": stopbits = StopBits.One; break; case "OnePointFive": stopbits = StopBits.OnePointFive; break; case "Two": stopbits = StopBits.Two; break; } comDialog.Dispose(); } if (comport == "") { return(false); } m_serialPort.PortName = comport; m_serialPort.BaudRate = Convert.ToInt32(baudrate); m_serialPort.DataBits = Convert.ToInt32(databits); m_serialPort.Parity = parity; m_serialPort.StopBits = stopbits; try { m_serialPort.Open(); //Serielle verbindung öffnen m_protocolHandler = new ProtocolHandler(m_serialPort, this); //Start ProtocolHandler _communicationType = _communicationCom; //Declare Communicaton as open return(true); } catch (IOException) { MessageBox.Show(@"IO Exception: " + comport + Resources.PortNotFound); return(false); } catch (Exception) { MessageBox.Show(@"Exception: " + comport + Resources.PortNotFound); return(false); } } else { // no selection made return(false); } } else { // already connected return(false); } }