Rock.Apps.CheckScannerUtility.BatchPage.ConnectToScanner C# (CSharp) Méthode

ConnectToScanner() public méthode

Connects to scanner.
public ConnectToScanner ( ) : bool
Résultat bool
        public bool ConnectToScanner()
        {
            var rockConfig = RockConfig.Load();

            if ( rockConfig.ScannerInterfaceType == RockConfig.InterfaceType.MICRImageRS232 )
            {
                if ( micrImage == null )
                {
                    // no MagTek driver
                    return false;
                }

                micrImage.CommPort = rockConfig.MICRImageComPort;
                micrImage.PortOpen = false;

                UpdateScannerStatusForMagtek( false );

                object dummy = null;

                // converted from VB6 from MagTek's sample app
                if ( !micrImage.PortOpen )
                {
                    micrImage.PortOpen = true;
                    if ( micrImage.DSRHolding )
                    {
                        // Sets Switch Settings
                        // If you use the MicrImage1.Save command then these do not need to be sent
                        // every time you open the device
                        micrImage.MicrTimeOut = 1;
                        micrImage.MicrCommand( "SWA 00100010", ref dummy );
                        micrImage.MicrCommand( "SWB 00100010", ref dummy );
                        micrImage.MicrCommand( "SWC 00100000", ref dummy );
                        micrImage.MicrCommand( "HW 00111100", ref dummy );
                        micrImage.MicrCommand( "SWE 00000010", ref dummy );
                        micrImage.MicrCommand( "SWI 00000000", ref dummy );

                        // The OCX will work with any Micr Format.  You just need to know which
                        // format is being used to parse it using the FindElement Method
                        micrImage.FormatChange( "6200" );
                        micrImage.MicrTimeOut = 5;

                        // get Version to test if we have a good connection to the device
                        string version = "-1";
                        try
                        {
                            this.Cursor = Cursors.Wait;
                            version = micrImage.Version();
                        }
                        finally
                        {
                            this.Cursor = null;
                        }

                        if ( !version.Equals( "-1" ) )
                        {
                            UpdateScannerStatusForMagtek( true );
                        }
                        else
                        {
                            MessageBox.Show( string.Format( "MagTek Device is not responding on COM{0}.", micrImage.CommPort ), "Scanner Error" );
                            return false;
                        }
                    }
                    else
                    {
                        MessageBox.Show( string.Format( "MagTek Device is not attached to COM{0}.", micrImage.CommPort ), "Missing Scanner" );
                        return false;
                    }
                }

                ScannerFeederType = FeederType.SingleItem;
            }
            else
            {
                try
                {
                    if ( this.rangerScanner == null )
                    {
                        // no ranger driver
                        return false;
                    }
                }
                catch
                {
                    return false;
                }

                try
                {
                    this.Cursor = Cursors.Wait;
                    rangerScanner.StartUp();
                }
                finally
                {
                    this.Cursor = null;
                }

                string feederTypeName = rangerScanner.GetTransportInfo( "MainHopper", "FeederType" );
                if ( feederTypeName.Equals( "MultipleItems" ) )
                {
                    ScannerFeederType = FeederType.MultipleItems;
                }
                else
                {
                    ScannerFeederType = FeederType.SingleItem;
                }
            }

            return true;
        }

Usage Example

Exemple #1
0
        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="RoutedEventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            _rockConfig.CaptureAmountOnScan = chkCaptureAmountOnScan.IsChecked == true;

            _rockConfig.RequireControlAmount    = chkRequireControlAmount.IsChecked == true;
            _rockConfig.RequireControlItemCount = chkRequireControlItemCount.IsChecked == true;
            AddAccountsForAmountsToSave();
            if (_rockConfig.CaptureAmountOnScan && _rockConfig.SelectedAccountForAmountsIds.Count() == 0)
            {
                MessageBox.Show("Please select at least one account", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            try
            {
                txtRockUrl.Text = txtRockUrl.Text.Trim();
                Uri rockUrl      = new Uri(txtRockUrl.Text);
                var validSchemes = new string[] { Uri.UriSchemeHttp, Uri.UriSchemeHttps };
                if (!validSchemes.Contains(rockUrl.Scheme))
                {
                    txtRockUrl.Text = "http://" + rockUrl.AbsoluteUri;
                }

                RockRestClient client = new RockRestClient(txtRockUrl.Text);
                client.Login(_rockConfig.Username, _rockConfig.Password);
                BatchPage.LoggedInPerson = client.GetData <Person>(string.Format("api/People/GetByUserName/{0}", _rockConfig.Username));
            }
            catch (WebException wex)
            {
                HttpWebResponse response = wex.Response as HttpWebResponse;
                if (response != null)
                {
                    if (response.StatusCode == HttpStatusCode.Unauthorized)
                    {
                        // valid URL but invalid login, so navigate back to the LoginPage
                        _rockConfig.RockBaseUrl = txtRockUrl.Text;
                        _rockConfig.Save();
                        LoginPage loginPage = new LoginPage(true);
                        this.NavigationService.Navigate(loginPage);
                        return;
                    }
                }

                lblAlert.Content    = wex.Message;
                lblAlert.Visibility = Visibility.Visible;
                return;
            }
            catch (Exception ex)
            {
                App.LogException(ex);
                lblAlert.Content    = ex.Message;
                lblAlert.Visibility = Visibility.Visible;
                return;
            }

            _rockConfig.RockBaseUrl = txtRockUrl.Text;

            switch (cboScannerInterfaceType.SelectedItem as string)
            {
            case "MagTek COM":
                _rockConfig.ScannerInterfaceType = RockConfig.InterfaceType.MICRImageRS232;
                break;

            case "MagTek Image Safe":
                _rockConfig.ScannerInterfaceType = RockConfig.InterfaceType.MagTekImageSafe;
                break;

            default:
                _rockConfig.ScannerInterfaceType = RockConfig.InterfaceType.RangerApi;
                break;
            }

            string imageOption = cboImageOption.SelectedValue as string;

            _rockConfig.Sensitivity = txtSensitivity.Text.Trim().AsInteger().ToString();
            _rockConfig.Plurality   = txtPlurality.Text.Trim().AsInteger().ToString();

            switch (imageOption)
            {
            case "Grayscale":
                _rockConfig.ImageColorType = RangerImageColorTypes.ImageColorTypeGrayscale;
                break;

            case "Color":
                _rockConfig.ImageColorType = RangerImageColorTypes.ImageColorTypeColor;
                break;

            default:
                _rockConfig.ImageColorType = RangerImageColorTypes.ImageColorTypeBitonal;
                break;
            }

            string comPortName = cboMagTekCommPort.SelectedItem as string;

            if (!string.IsNullOrWhiteSpace(comPortName))
            {
                _rockConfig.MICRImageComPort = short.Parse(comPortName.Replace("COM", string.Empty));
            }

            var campusFilter = cboCampusFilter.SelectedItem as Campus;

            _rockConfig.CampusIdFilter = campusFilter?.Id;

            _rockConfig.Save();
            BatchPage.LoadLookups();

            // shutdown the scanner so that options will be reloaded when the batch page loads
            if (BatchPage.rangerScanner != null)
            {
                BatchPage.rangerScanner.ShutDown();
            }

            BatchPage.UnbindAllEvents();
            BatchPage.BindDeviceToPage();
            BatchPage.ConnectToScanner();
            BatchPage.LoadFinancialBatchesGrid();
            BatchPage.UpdateBatchUI(BatchPage.SelectedFinancialBatch);

            this.NavigationService.Navigate(BatchPage);
        }