Esempio n. 1
0
        private void InitializeCommunicationPort()
        {
            var port        = string.Empty;
            var useFakePort = false;

            if (environment.IsDevelopment())
            {
                logger.LogDebug("Is Debug Environment, will check for FakePort setting");

                useFakePort = configuration.GetValue <bool>("FakePort:IsActive");

                if (useFakePort)
                {
                    logger.LogDebug("Using Fake Communication Port");
                    port = "FakePort";
                }
                else
                {
                    logger.LogDebug("Will not use fake port");
                }
            }

            if (!useFakePort)
            {
                var fixedPort = configuration.GetValue <string>("SerialPort");
                if (!string.IsNullOrEmpty(fixedPort))
                {
                    logger.LogDebug($"Fixed Serial Port set in configuration: {fixedPort}");
                    port = fixedPort;
                }
                else
                {
                    logger.LogDebug("No fixed port configuration found - scanning for ports...");

                    var availableDevices = serialPortService.GetAvailableSerialPorts().ToList();

                    if (availableDevices.Count == 1)
                    {
                        port = availableDevices[0];
                        logger.LogDebug($"Found exactly one available port - will use this one");
                    }
                    else if (availableDevices.Count > 1)
                    {
                        port = availableDevices[0];
                        logger.LogDebug($"Found multiple ports - will use first one. If this is not the right device, consider specifying it using the SerialPort configuration");
                    }
                    else
                    {
                        logger.LogDebug("No Ports found. Make sure device is connected properly and if run in a docker container --device flag is used. Otherwise see docs.");
                        throw new InvalidOperationException("No Ports found. Make sure device is connected properly and if run in a docker container --device flag is used. Otherwise see docs.");
                    }
                }
            }

            ConnectSerialPort(port, useFakePort);
        }
Esempio n. 2
0
 public ActionResult <IEnumerable <string> > GetSerialPorts()
 {
     return(new ActionResult <IEnumerable <string> >(serialPortService.GetAvailableSerialPorts()));
 }