Beispiel #1
0
        //------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Functions
        //------------------------------------------------------------------------------------------------------------------------
        public override bool Initialize(mNodeConfig mNodeConfig, string PluginConfig, string UserConfig)
        {
            //init base
            if (base.Initialize(mNodeConfig, PluginConfig, UserConfig) == false)
            {
                return(false);
            }

            //set queue handler
            SendQueue.SetHandler(s =>
            {
                if (SendToCloudPort != null)
                {
                    SetPortState(SendToCloudPort.PortKey, s);
                }
            });

            //init
            try
            {
                var ports = SerialPort.GetPortNames();
                DebugEx.TraceLog("Serial Ports found: " + string.Join(",", ports));

                var port = ports?.Where(p => p.Contains("USB")).FirstOrDefault();
                if (port != null && !port.IsNullOrEmpty())
                {
                    DebugEx.TraceLog("Selected Port: " + port);

                    //replace this with entry from conf
                    mySerial = new SerialPort(port, 9600);
                    if (mySerial.IsOpen)
                    {
                        try
                        {
                            mySerial.Close();
                            mySerial.Dispose();
                        }
                        catch { }
                    }
                    mySerial.DataReceived  += MySerial_DataReceived;
                    mySerial.ErrorReceived += MySerial_ErrorReceived;
                    mySerial.ReadTimeout    = 2000;
                    mySerial.NewLine        = "\r\n";
                    mySerial.Open();

                    _IsRunning = true;
                    StartSerialPortReadThread();

                    SendQueue.Start();
                    SendQueue.Pause();
                }
                else
                {
                    DebugEx.Assert("The USB port does not exist");
                }
            }
            catch (Exception ex) { DebugEx.Assert(ex, "Could not init"); }

            //setup
            SetupSerialPortThings();

            //done
            return(true);
        }