コード例 #1
0
        public override async Task OnExecuteAsync(ProgramOptions pOptions, CommandOptions cOptions, CancellationToken cToken)
        {
            Console.WriteLine("Scanning for serial ports...");

            if (!Enum.TryParse(pOptions.Status, out PortStatus portStatus))
            {
                Console.WriteLine("Invalid port status.");
                return;
            }

            try
            {
                var portScanner = new SerialPortScanner();
                var settings    = new ScanProperties(pOptions.MinPort, pOptions.MaxPort, portStatus);

                var scanResult = await portScanner.ScanAsync(settings, cToken);

                new PortStatusPrinter().PrintTable(scanResult);

                Console.WriteLine("\nDone.");
            }
            catch (Exception e)
            {
                Console.WriteLine($"Command 'serialPort', ran into exception: {e.Message}");
            }
        }
        public void Test_InvalidPortScanRange(int minPort, int maxPort)
        {
            var spScanner = new SerialPortScanner();

            Assert.ThrowsAsync <ArgumentException>(async() =>
            {
                var scanProperties = new ScanProperties
                {
                    MinPort = minPort,
                    MaxPort = maxPort
                };

                await spScanner.ScanAsync(scanProperties, CancellationToken.None);
            });
        }
        public void Test_MaxPortLimit()
        {
            var spScanner = new SerialPortScanner();

            Assert.ThrowsAsync <ArgumentOutOfRangeException>(async() =>
            {
                var scanProperties = new ScanProperties
                {
                    MinPort = 1,
                    MaxPort = 65536
                };

                await spScanner.ScanAsync(scanProperties, CancellationToken.None);
            });
        }
        public void Test_ValidPortScanRange(int minPort, int maxPort)
        {
            var spScanner  = new SerialPortScanner();
            var totalPorts = (maxPort - minPort) + 1;

            var scanProperties = new ScanProperties
            {
                MinPort = minPort,
                MaxPort = maxPort
            };

            var actual = spScanner.ScanAsync(scanProperties, CancellationToken.None)
                         .GetAwaiter()
                         .GetResult()
                         .Count();

            Assert.Equal(totalPorts, actual);
        }
コード例 #5
0
        public void Test_PortScanRangeEquals(int minPort, int maxPort)
        {
            var spScanner = new SerialPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            IList <IPrintablePortStatus> sResult = default;

            try
            {
                var task = spScanner.ScanAsync(new ScanProperties(minPort, maxPort, PortStatus.Any), cToken);
                sResult = task.Result.ToList();
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }

            Assert.Equal((maxPort - minPort) + 1, sResult.Count);
        }
コード例 #6
0
        public void Test_ValidPortScanRange()
        {
            var spScanner = new SerialPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            try
            {
                var task   = spScanner.ScanAsync(new ScanProperties(1, 3, PortStatus.Any), cToken);
                var result = task.Result;
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }

            Assert.True(true);
        }
コード例 #7
0
        public void Test_ValidPortStatus(PortStatus status)
        {
            const int minPort = 1;
            const int maxPort = 2;

            var spScanner = new SerialPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            IList <IPrintablePortStatus> sResult = default;

            try
            {
                var task = spScanner.ScanAsync(new ScanProperties(minPort, maxPort, status), cToken);
                sResult = task.Result.ToList();
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }

            bool statusOk;

            if (status == PortStatus.Any)
            {
                statusOk = ((maxPort - minPort) + 1) == sResult.Count;
            }
            else
            {
                var portStatusString = status.ToString();
                statusOk = sResult.All(x => x.GetStatusString() == portStatusString);
            }

            Assert.True(statusOk);
        }
コード例 #8
0
        public void Test_InvalidPortScanRange()
        {
            var spScanner = new SerialPortScanner();
            var cancellationTokenSource = new CancellationTokenSource();
            var cToken = cancellationTokenSource.Token;

            try
            {
                Assert.ThrowsAsync <ArgumentException>(async() =>
                {
                    var scanProperties = new ScanProperties(3, 1, PortStatus.Any);
                    await spScanner.ScanAsync(scanProperties, cToken);
                });
            }
            catch (Exception e)
            {
                Assert.True(false, e.Message);
            }
            finally
            {
                cancellationTokenSource.Dispose();
            }
        }