//open a connection to the specified comport public bool connect(Control cont, rxDelegate callback, string portName, int baudRate) { //if it is not initialized, init it if (serialPort == null) { initialize(cont, callback, portName, baudRate); } else if (serialPort.IsOpen) { ConsoleOutput("Already connected."); } else { serialPort.PortName = portName; } try { serialPort.Open(); ConsoleOutput(String.Format("Connected to port {0}", portName)); return(true); } catch { ConsoleOutput(String.Format("Unable to connect to port {0}", portName)); return(false); } }
//initialize a serial connection private void initialize(Control cont, rxDelegate callback, string portName, int baudRate) { serialPort = new SerialPort() { PortName = portName, BaudRate = baudRate, DtrEnable = true, }; control = cont; rxCallback = callback; serialPort.DataReceived += SerialPort_DataReceived; ConsoleOutput("Initialized."); }