/// <summary> /// Turns port 1 on or off. /// </summary> /// <param name="status">True for on, False for off.</param> /// <exception cref="FailureDetectorException"></exception> public void SetRelay1Status(bool status) { sem.WaitOne(); if (status) { try { sp.Write("relay on 1" + CARRIAGE_RETURN); } catch (Exception inner) { string errMsg = "USBRelayController.SetRelay1Status : Unable to write command to relay board."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } } else { try { sp.Write("relay off 1" + CARRIAGE_RETURN); } catch (Exception inner) { string errMsg = "USBRelayController.SetRelay1Status : Unable to write command to relay board."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } } sem.Release(); }
public void Close() { sem.WaitOne(); try { sp.Close(); } catch (Exception inner) { string errMsg = "USBRelayController.Close : Unable to close serial port."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } sem.Release(); }
/// <summary> /// Attempts to open up serial port connection to com port specified by portNum. /// </summary> /// <param name="portNum">Com port to connect to.</param> /// <exception cref="RelayControllerException"></exception> public void Open(int portNum) { sem.WaitOne(); sp.PortName = "COM" + portNum.ToString(); sp.BaudRate = 9600; sp.DataBits = 8; try { sp.Open(); } catch (Exception inner) { string errMsg = "USBRelayController.USBRelayController : Unable to open serial port."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } isOpen = true; sem.Release(); }
/// <summary> /// Reads relay status of port 1 /// </summary> /// <exception cref="RelayControllerException"></exception> /// <returns>True if on, False if off.</returns> public bool ReadRelay1Status() { sem.WaitOne(); try { sp.DiscardInBuffer(); } catch (Exception inner) { string errMsg = "USBRelayController.ReadRelay1Status : Unable to discard input buffer."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } try { sp.Write("relay read 1" + CARRIAGE_RETURN); } catch (Exception inner) { string errMsg = "USBRelayController.ReadRelay1Status : Unable to write command."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } // sleep a bit to wait for response Thread.Sleep(15); String message; try { message = sp.ReadExisting(); } catch (Exception inner) { string errMsg = "USBRelayController.ReadRelay1Status : Unable to read response from controller."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } bool status; try { status = message.Contains("on"); } catch (Exception inner) { string errMsg = "USBRelayController.ReadRelay1Status : Received null response from controller."; RelayControllerException ex = new RelayControllerException(errMsg, inner); log.Error(errMsg, ex); sem.Release(); throw ex; } sem.Release(); return(status); }