/// <summary> /// Open the connection agaist initialized hostname and port number /// </summary> /// <returns>Returns true if connection is established</returns> public bool Open() { try { // Create a TcpClient. // Note, for this client to work you need to have a TcpServer // connected to the same address as specified by the server, port // combination. client = new TcpClient(_hostname, _port); //TEMP, should these be deconstructed for each time Cloes() is called? //client.Connect(_hostname, _port); this does not seem to work (cant access removed object) // Get a client stream for reading and writing. stream = client.GetStream(); connectionEstablished = true; connectionStatus = CommHandler.ConnectionStatus.Connected; } catch (Exception CANConnectException) { Console.WriteLine("Could not open CAN bus connection\n\n Exception: " + CANConnectException.ToString()); } //return true if the port managed to open return(IsOpen()); }
/// <summary> /// Close the serial connection /// </summary> /// <returns>Returns true if the port was closed</returns> public bool Close() { port.Close(); connectionEstablished = false; connectionStatus = CommHandler.ConnectionStatus.NotConnected; //return true if the port has closed return(!IsOpen()); }
/// <summary> /// Get informetion on the status of the connection /// </summary> /// <returns></returns> public CommHandler.ConnectionStatus GetConnectionStatus() { if (connectionEstablished && IsOpen()) { connectionStatus = CommHandler.ConnectionStatus.Connected; } else if (connectionEstablished && !IsOpen()) { connectionStatus = CommHandler.ConnectionStatus.Disconnected; } else if (!connectionEstablished && !IsOpen()) { connectionStatus = CommHandler.ConnectionStatus.NotConnected; } return(connectionStatus); }
/// <summary> /// Open the serial connection /// </summary> public bool Open() { try { port.Open(); connectionEstablished = true; connectionStatus = CommHandler.ConnectionStatus.Connected; } catch (UnauthorizedAccessException e) { Program.errors.Add("Cannot access com port. Comport might already be in use by another application"); } catch (IOException) { Program.errors.Add("Cannot open com port. Spesified port might not exist"); } //return true if the port managed to open return(IsOpen()); }