public bool Initialize(string port_progbox, string port_charger) { bool result = true; if (!m_initialized) { ProgboxIf = new SerialInterface(port_progbox, globals.baud, Parity.None, 8); ChargerIf = new SerialInterface(port_charger, globals.baud, Parity.None, 8); Progbox = new Target(); Charger = new Target(); ProgboxIf.RegisterListener(this); ChargerIf.RegisterListener(this); if (!ProgboxIf.Open() || !ChargerIf.Open()) { result = false; ProgboxIf.Close(); ChargerIf.Close(); ProgboxIf.Dispose(); ChargerIf.Dispose(); } else { m_initialized = true; } } return(result); }
public override void CloseConnection() { try { SerialInterface.Close(); PortClosed(Portname, SerialInterface.BaudRate); } catch (Exception) { ClosePortFailed(this, new EventArgs()); } }
//functions //-------------------------------- public bool Connect(SerialDevice device, int BaudRate) { try { if (IsApiBusy) { throw new InvalidOperationException("Failed to connect : API not ready."); } serialInterface.OpenDevice(device, BaudRate); switch (API_AttemptAPIHandshake()) { case -1: default: serialInterface.Close(); return(false); case 0: _throwWarning(this, $"{Environment.NewLine}Controller did not respond correctly to the handshake. Keeping connection open...{Environment.NewLine}"); goto case 1; case 1: //handshake was successful serialInterface.OnDataToRead += Serial_DataToRead; serialInterface.OnErrorRaised += Serial_ErrorRaised; break; } } catch (Exception e) { _throwException(e); return(false); } return(true); }