public static bool Connect(string comport, string name, string password, out string error) { error = "Succes"; if (!ComPort.IsOpen()) { if (ComPort.Connect(comport)) { ComPort.Write("RS"); string temp = ComPort.Read(); if (temp == "err") { ComPort.Disconnect(); error = "De Ergometer is niet verbonden"; return(false); } Thread.Sleep(200); ComPort.Write("CM"); ComPort.Read(); Thread.Sleep(200); ComPort.Write("ST"); string response = ComPort.Read(); SaveMeting(response); } else { error = "De ergometer is niet verbonden"; return(false); } } if (Doctor == null || !Doctor.Connected) { if (Doctor == null) { Doctor = new TcpClient(); } try { Doctor.Connect(HOST, PORT); } catch (Exception e) { error = "Server is niet online."; return(false); } Name = name; NetCommand net = NetHelper.ReadNetCommand(Doctor); if (net.Type == NetCommand.CommandType.SESSION) { Session = net.Session; } else { throw new Exception("Session not assigned"); } running = true; t = new Thread(run); t.IsBackground = true; t.Start(); } if (!Loggedin) { NetCommand command = new NetCommand(name, false, password, Session); NetHelper.SendNetCommand(Doctor, command); NetCommand response = NetHelper.ReadNetCommand(Doctor); if (response.Type == NetCommand.CommandType.RESPONSE && response.Response == NetCommand.ResponseType.LOGINWRONG) { Loggedin = false; error = "De inloggegevens zijn onjuist."; return(false); } else { Loggedin = true; } } return(true); }