// Method that inits the ports. Will only be called once to initialize them when starting the mod. It will also open them.
        private void initPorts()
        {
            for (int i = 254; i >= 0; i--)
            {
                this.onSerialReceivedArray[i]          = new EventData <byte, object>(String.Format("onSerialReceived{0}", i));
                this.toSerialArray[i]                  = new EventData <byte, object>(String.Format("toSerial{0}", i));
                this.onSerialChannelSubscribedArray[i] = new EventData <byte, object>(String.Format("onSerialChannelSubscribed{0}", i));
            }

            this.onSerialReceivedArray[CommonPackets.Synchronisation].Add(this.handshakeCallback);
            this.onSerialReceivedArray[InboundPackets.CloseSerialPort].Add(this.serialCalledClose);
            this.onSerialReceivedArray[InboundPackets.RegisterHandler].Add(this.registerCallback);
            this.onSerialReceivedArray[InboundPackets.DeregisterHandler].Add(this.deregisterCallback);

            Config = new KerbalSimpitConfig();
            if (Config.Verbose)
            {
                Debug.Log("KerbalSimpit is in verbose mode");
            }
            else
            {
                Debug.Log("KerbalSimpit is not in verbose mode");
            }

            fillSerialPortsList(Config);
            if (Config.Verbose)
            {
                Debug.Log(String.Format("KerbalSimpit: Found {0} serial ports", SerialPorts.Count));
            }

            // Open the ports when initialieing the mod at start up.
            this.OpenPorts();

            Debug.Log("KerbalSimpit: Started");
        }
示例#2
0
        public void Start()
        {
            DontDestroyOnLoad(this);

            for (int i = 254; i >= 0; i--)
            {
                onSerialReceivedArray[i] = new EventData <byte, object>(String.Format("onSerialReceived{0}", i));
                toSerialArray[i]         = new EventData <byte, object>(String.Format("toSerial{0}", i));
            }

            Config = new KerbalSimpitConfig();

            SerialPorts = createPortList(Config);
            if (Config.Verbose)
            {
                Debug.Log(String.Format("KerbalSimpit: Found {0} serial ports", SerialPorts.Length));
            }
            OpenPorts();

            onSerialReceivedArray[CommonPackets.Synchronisation].Add(handshakeCallback);
            onSerialReceivedArray[InboundPackets.RegisterHandler].Add(registerCallback);
            onSerialReceivedArray[InboundPackets.DeregisterHandler].Add(deregisterCallback);

            EventDispatchThread = new Thread(EventWorker);
            EventDispatchThread.Start();
            while (!EventDispatchThread.IsAlive)
            {
                ;
            }

            Debug.Log("KerbalSimpit: Started.");
        }
        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);
            }
        }
示例#4
0
        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());
        }