예제 #1
0
        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public SlaveExplorerViewModel(IMessageBoxService messageBoxService, IPreferences preferences)
        {
            _messageBoxService = messageBoxService;
            _preferences       = preferences;

            _registerViewerPreferences = new RegisterViewerPreferences(preferences);

            _coils            = new CoilsViewModel(this);
            _inputs           = new InputsViewModel(this);
            _holdingRegisters = new HoldingRegistersViewModel(this);
            _inputRegisters   = new InputRegistersViewModel(this);

            SlaveAddress = _registerViewerPreferences.SlaveAddress;

            ModbusAdapters.ApplyPreferences(_preferences, RegisterViewerPreferences.Keys.ModbusAdapter);

            _pollingTimer = new Timer()
            {
                AutoReset = false
            };

            _pollingTimer.Elapsed += PollingTimerEllapsed;

            StopPollingCommand = new RelayCommand(StopPolling, CanStopPolling);
        }
예제 #2
0
        private void Start()
        {
            if (!ModbusAdapters.IsItemSelected)
            {
                return;
            }

            var dialog = new SaveFileDialog()
            {
                Filter = CaptureConstants.CaptureFilter
            };

            if (dialog.ShowDialog() != true)
            {
                return;
            }

            CaptureViewer = null;

            BytesReceived = 0;

            //Save the filename
            _capturePath = dialog.FileName;

            _captureHost = new CaptureHost(dialog.FileName, ModbusAdapters.GetFactory().CreateStreamResource());

            _captureHost.SampleReceived += OnSampleReceived;

            Status = "Capturing...";
        }
예제 #3
0
        private async void ScanAsync(CancellationToken token)
        {
            try
            {
                IsRunning = true;

                _results.Clear();

                using (var master = ModbusAdapters.GetFactory().Create())
                {
                    try
                    {
                        //TODO: Migrate

                        //var stream = master.Master.Transport.GetStreamResource();

                        //stream.DiscardInBuffer();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex);
                    }

                    ProgressValue = 0;
                    ProgressMax   = (byte)(EndAddress - StartAddress);

                    for (byte slaveAddress = StartAddress; slaveAddress < EndAddress; slaveAddress++)
                    {
                        CurrentSlaveAddress = slaveAddress;

                        if (token.IsCancellationRequested)
                        {
                            break;
                        }

                        bool   wasFound = false;
                        string reason;

                        try
                        {
                            await master.Master.ReadHoldingRegistersAsync(slaveAddress, 1000, 1);

                            reason   = "Reading Holding Register succeeded.";
                            wasFound = true;
                        }
                        catch (TimeoutException ex)
                        {
                            reason = ex.Message;
                            //Not found
                        }
                        catch (SlaveException ex)
                        {
                            //We got sopme sort of slave exception.
                            reason   = ex.Message;
                            wasFound = true;
                        }
                        catch (Exception ex)
                        {
                            reason = ex.Message;
                        }

                        //Create the result.
                        var result = new ResultViewModel()
                        {
                            SlaveAddress = slaveAddress,
                            WasFound     = wasFound,
                            Reason       = reason,
                        };

                        _results.Add(result);

                        ProgressValue++;
                    }
                }

                if (_results.Count != 0)
                {
                    string message;

                    var successfulCount = _results.Count(r => r.WasFound);

                    if (successfulCount == 0)
                    {
                        message = "No Modbus slaves were found.";
                    }
                    else if (successfulCount == 1)
                    {
                        message = "1 Modbus slave was found.";
                    }
                    else
                    {
                        message = $"{successfulCount} Modbus slaves were found.";
                    }

                    MessageBox.Show(message, "Scan Result");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            IsRunning = false;

            _cts = null;

            CommandManager.InvalidateRequerySuggested();
        }