private void fillSerialPortsList(KerbalSimpitConfig config) { SerialPorts = new List <KSPSerialPort>(); int count = config.SerialPorts.Count; for (byte i = 0; i < count; i++) { KSPSerialPort newPort = new KSPSerialPort(this, config.SerialPorts[i].PortName, config.SerialPorts[i].BaudRate, i); SerialPorts.Add(newPort); } }
public void OpenPort(int portID) { if (portID >= SerialPorts.Count) { Debug.Log("Simpit OpenPort called for port " + portID + " but I only have " + SerialPorts.Count + " ports"); return; } KSPSerialPort port = SerialPorts[portID]; if (port.portStatus != KSPSerialPort.ConnectionStatus.CLOSED && port.portStatus != KSPSerialPort.ConnectionStatus.ERROR) { //Port already opened. Nothing to do. return; } String portName = port.PortName; if (portName.StartsWith("COM") || portName.StartsWith("/")) { // TODO do more validation ? At least it is not undefined } else { Debug.LogWarning("Simpit : no port name is defined for port " + port.ID + ". Please check the SimPit config file."); // Display a message for 20s that persist on different scene ScreenMessages.PostScreenMessage("Simpit : no port name is defined for port " + port.ID + ". Please check the SimPit config file.", 20, true); return; } if (port.open()) { if (Config.Verbose) { Debug.Log(String.Format("KerbalSimpit: Opened {0}", portName)); } } else { if (Config.Verbose) { Debug.Log(String.Format("KerbalSimpit: Unable to open {0}", portName)); } } if (!DoEventDispatching) { StartEventDispatch(); } }
private KSPSerialPort[] createPortList(KerbalSimpitConfig config) { List <KSPSerialPort> PortList = new List <KSPSerialPort>(); int count = config.SerialPorts.Count; for (byte i = 0; i < count; i++) { KSPSerialPort newPort = new KSPSerialPort(config.SerialPorts[i].PortName, config.SerialPorts[i].BaudRate, i); PortList.Add(newPort); } return(PortList.ToArray()); }
public void ClosePort(int portID) { if (portID >= SerialPorts.Count) { Debug.Log("Simpit OpenPort called for port " + portID + " but I only have " + SerialPorts.Count + " ports"); return; } KSPSerialPort port = SerialPorts[portID]; if (port.portStatus == KSPSerialPort.ConnectionStatus.CLOSED) { // Port is already closed. Nothing to do. return; } //If all port are closed except this one, we can stop the event dispatching bool canStopEventDispatch = true; foreach (KSPSerialPort otherPort in SerialPorts) { if (otherPort.ID != portID && (otherPort.portStatus != KSPSerialPort.ConnectionStatus.CLOSED && port.portStatus != KSPSerialPort.ConnectionStatus.ERROR)) { canStopEventDispatch = false; } } if (canStopEventDispatch) { // Sets this to false, to signal to the workers to stop running. // Without this, they will cause many problems, and prevent the Arduino from being reconnected // Also, without this if the Arduino is disconnected the workers will throw so many errors, that // they seem to be the cause of KSP crashing not long after DoEventDispatching = false; } port.close(); }