Show TOC Start of Content Area

This graphic is explained in the accompanying textAdd / Remove Symbologies  Locate the document in its SAP Library structure

In this example the scanner uses Scanner Aware Mode and three barcodes are scanned after Code 39, Code 128, and Codabar symbologies are added to the scanner. The program performs the following steps:

Caution

This example assumes that at least one driver has been installed. It also assumes this driver supports all the attributes that are being used.

...

       1.      Implements the constructor for the class. It opens a connection to the scanner in scan aware mode and sets an event listener.

       2.      Implements the onError method from ScannerListener.

       3.      Implements the onDataReceived method from ScannerListener.

       4.      Declares and implements a method called scanBarcode. This method starts the scanning engine, waits for the application to read a barcode, and stops the scanning engine.

       5.      Declares and implements the openScanAwareConnection method.

       6.      Declares and implements the method called activateSymbologies. This method adds Code 39, Code 128, and Codabar symbologies to the scanner.

       7.      Declares and implements the method called deactivateSymbologies. This method removes Code 39 and Code 128 symbologies from the scanner.

       8.      Declares and implements the close method. It closes the connection to the scanner.

       9.      Creates an instance of the class and calls the activateSymbologies method.

   10.      Scans three barcodes. The scanned barcodes must have their symbology activated; otherwise the scanner will not read them.

   11.      Calls the deactivateSymbologies method. Then scans one barcode and closes the connection to the scanner.

 

package scanner_api_examples;

 

import com.sap.ip.me.api.pios.PIOSException;

import com.sap.ip.me.api.pios.connection.*;

import com.sap.ip.me.api.pios.scanner.*;

import com.sap.ip.me.api.pios.symbology.*;

 

public class AddingSymbologies implements ScannerListener {

   private ScannerConnection scannerConnection = null;

   private boolean barcodeScanned = false;

   private Exception lastException = null;

 

   //-----------------------(1)-----------------------

   public AddingSymbologies() throws PIOSException {

      openScanAwareConnection();

      scannerConnection.setEventListener(this);

   }

 

   //-----------------------(2)-----------------------

   public void onError(ScannerException e) {

      lastException = e;

      barcodeScanned = true;    

   }

 

   //-----------------------(3)-----------------------

   public void onDataReceived(ScannerData scannerData) {

      try {

         System.out.println(

            "Data = " + new String(scannerData.toByteArray(), "ASCII"));     

      } catch (Exception ex) {        

         lastException = ex;

      }

      barcodeScanned = true;

   }

 

   //-----------------------(4)-----------------------

   public void scanBarcode() throws Exception {

      scannerConnection.startRead();

     

      while (!barcodeScanned) {

         Thread.sleep(500);

      }

     

      if (lastException != null) {

         throw lastException;

      }

     

      scannerConnection.endRead();

      barcodeScanned = false;

   }

 

   //-----------------------(5)-----------------------

   private void openScanAwareConnection() throws PIOSException {

      Connector connector = Connector.getInstance();

      DriverInfo[] scanners = connector.listDrivers(ConnectionType.SCANNER);

      ScannerParameters parameters = new ScannerParameters(scanners[0]);

      parameters.setMode(ScannerParameters.SCAN_AWARE);

      scannerConnection = (ScannerConnection) connector.open(parameters);

   }

 

   //-----------------------(6)-----------------------

   public void activateSymbologies() throws PIOSException {

      scannerConnection.addSymbology(new Code39(Code39.FULLASCII));

      scannerConnection.addSymbology(new Code128());

      scannerConnection.addSymbology(

         new Codabar(

            Codabar.CHECK_DIGIT_MOD16

               | Codabar.CHECK_DIGIT_TRANSMIT));

   }

 

   //-----------------------(7)-----------------------

   public void deactivateSymbologies() throws PIOSException {

      scannerConnection.removeSymbology(SymbologyType.CODE128);

      scannerConnection.removeSymbology(SymbologyType.CODE39);

   }

 

   //-----------------------(8)-----------------------

   public void close() {

      try {

         scannerConnection.close();

      } catch (Exception ex) {

         ex.printStackTrace();

      }

   }

 

   public static void main(String[] args) {

      AddingSymbologies addSymbologiesExample = null;

 

      try {

         //-----------------------(9)-----------------------

         addSymbologiesExample = new AddingSymbologies();

         addSymbologiesExample.activateSymbologies();

 

         //-----------------------(10)-----------------------

         for (int i = 0; i < 3; i++) {

            addSymbologiesExample.scanBarcode();

         }

 

         //-----------------------(11)-----------------------

        

         addSymbologiesExample.deactivateSymbologies();

         addSymbologiesExample.scanBarcode();

        

      } catch (Exception ex) {

         ex.printStackTrace();

      } finally {

         addSymbologiesExample.close();

      }

   }

}

End of Content Area