Browse
(440) 299-7778
Menu
(440) 299-7778
One-year parts and labor warranty30-day money-back guaranteeEnglishEspañolContact

The text of a manual · part 10 of 11

PSM20i Bar Code Scanner Attachment Programmer's Guide: Appendix B Sample Program

Pages 65 to 76 of 80, from PSM20i Bar Code Scanner Attachment Programmer's Guide

The original document is the PDF (773 KB). This is its text as extracted, unedited; tables and drawings read best in the PDF, and each page heading opens it at that page.

For the Symbol PSM20i

Page 65

Appendix B Sample Program The sample program in this Appendix allows you to enable and disable to barcode scanner and program the scanner to decode Code 39 and Code 39 Trioptic barcodes. /** This software is provided to you by Symbol Technologies, Inc. under a * license from Motorola, Inc. You may copy, modify and use this software * solely for purposes of designing, manufacturing, and selling the barcode * reader accessory or other devices that interact with or control this * accessory. No other license, express or implied, relating to the software * is granted to you. */ /* * * © Copyright 2000 Motorola, Inc. All Rights Reserved. * This notice does not imply publication. */ import javax.microedition.lcdui.*; import javax.microedition.midlet.*; import java.io.*; import javax.microedition.io.*; import com.symbol.j2me.midlets.ascanner.BarCodeReader;

public class c39Test extends MIDlet implements CommandListener { public static final boolean COLOR = false; // color support public static final boolean DEBUG = false; // debug statements // basic colors public static final int WHITE = 0xFFFFFF; public static final int BLACK = 0x000000;

B-1

Page 66

PSM20i Bar Code Scanner Attachment Programmer’s Guide public static final int BLUE = 0x0000FF; public static final int LIGHT_GRAY = 0xAAAAAA; public static final int DARK_GRAY = 0x555555; // reference to current display private Display myDisplay = null; private List mainScreen = null; private List c39Screen = null; private List c39TriopticScreen = null; private Form resultScreen = null; private Command okCommand = new Command("OK", Command.OK, 1); private Command backCommand = new Command("Back", Command.BACK, 2); private Command exitCommand = new Command("Exit", Command.EXIT, 2); private BarCodeReader barCodeReader = null; private Alert alert = null; private DecodeCanvas decodeCanvas = null; private String sdkver = ""; private boolean response = true; public c39Test() { myDisplay = Display.getDisplay(this); barCodeReader = new BarCodeReader(); // Get version of SDK sdkver = barCodeReader.getSDKVersion(); decodeCanvas = new DecodeCanvas(this, barCodeReader); decodeCanvas.addCommand(backCommand); decodeCanvas.setCommandListener(this); // create main screen mainScreen = new List("CODE 39 Test", List.IMPLICIT); mainScreen.append("Enable Scanner", null); mainScreen.append("Scan Barcodes", null); mainScreen.append("Disable Scanner", null); mainScreen.append("Code 39", null); mainScreen.append("Code 30 Trioptic", null); mainScreen.addCommand(okCommand); mainScreen.addCommand(exitCommand); mainScreen.setCommandListener(this); // create result screen resultScreen = new Form("Result");

B-2

Page 67

Sample Program resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); // create Code 39 screen c39Screen = new List("Code 39", List.IMPLICIT); c39Screen.append("Enable Code 39",null); c39Screen.append("Disable Code 39",null); c39Screen.addCommand(okCommand); c39Screen.addCommand(backCommand); // create Code 39 Trioptic screen c39TriopticScreen = new List("Code 39 Trioptic", List.IMPLICIT); c39TriopticScreen.append("Enable C39 Trioptic",null); c39TriopticScreen.append("Disable C39 Trioptic",null); c39TriopticScreen.addCommand(okCommand); c39TriopticScreen.addCommand(backCommand); } /** * Start the MIDlet */ public void startApp() throws MIDletStateChangeException { myDisplay.setCurrent(mainScreen); } /** * Pause the MIDlet */ public void pauseApp() { } /** * Called by the framework before the application is unloaded */ public void destroyApp(boolean unconditional) throws MIDletStateChangeException { } /** * Indicates that a command event has occurred on Screen d * @param c a Command object identifying the command * @param d the Screen on which this event has occurred */ public void commandAction(Command c, Displayable d) {

B-3

Page 68

PSM20i Bar Code Scanner Attachment Programmer’s Guide

// make a selection from the main screen if ((c == okCommand) && (d == mainScreen)) { int selected = mainScreen.getSelectedIndex(); try { switch (selected) { case 0: // Enable Scanner if (barCodeReader.isEnabled()) { alert = new Alert("Error", "Reader is already enabled", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { resultScreen = new Form("Result"); resultScreen.append(sdkver); // 07/29/2002 TM resultScreen.append("Enabling barcode reader. Please wait.."); myDisplay.setCurrent(resultScreen); EnableThread t = new EnableThread(this); t.start(); } break; case 1: // Scan Barcodes if (!barCodeReader.isEnabled()) { alert = new Alert("Error", "Reader is not enabled!", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { decodeCanvas.setDecodeText(""); myDisplay.setCurrent(decodeCanvas); } break; case 2: // Disable Scanner if (!barCodeReader.isEnabled()) { alert = new Alert("Error", "Reader is not enabled!", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { resultScreen = new Form("Result"); resultScreen.append("Disable - ");

B-4

Page 69

Sample Program barCodeReader.disable(); resultScreen.append("OK"); resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } break; case 3: // Code 39 if (!barCodeReader.isEnabled()) { alert = new Alert("Error", "Reader is not enabled", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { c39Screen.setCommandListener(this); myDisplay.setCurrent(c39Screen); } break; case 4: // Code 39 Trioptic if (!barCodeReader.isEnabled()) { alert = new Alert("Error", "Reader is not enabled", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { c39TriopticScreen.setCommandListener(this); myDisplay.setCurrent(c39TriopticScreen); } break; } } catch (Exception e) { System.err.println(e); resultScreen = new Form("Error!"); resultScreen.append("\n" + e.toString()); resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } }

B-5

Page 70

PSM20i Bar Code Scanner Attachment Programmer’s Guide

// make a selection from the Code 39 screen if ((c == okCommand) && (d == c39Screen)) { int selected = c39Screen.getSelectedIndex(); try { switch (selected) { case 0: // Enable CODE 39 response = barCodeReader.code39(true, false,(byte)0x00,(byte)0x0A,(byte)0x06); if(!response){ alert = new Alert("Error", "C39 Enable FAILED", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { resultScreen = new Form("Result"); resultScreen.append("Code 39 Enabled!"); resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } break; case 1: // Disable CODE 39 response = barCodeReader.code39(false, false,(byte)0x00,(byte)0x00,(byte)0x00); if(!response){ alert = new Alert("Error", "C39 Disable FAILED", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { resultScreen = new Form("Result"); resultScreen.append("Code 39 Disabled!"); resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } break; } } catch (Exception e) { System.err.println(e); resultScreen = new Form("Error!"); resultScreen.append("\n" + e.toString());

B-6

Page 71

Sample Program resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } } // make a selection from the Code 39 Trioptic screen if ((c == okCommand) && (d == c39TriopticScreen)) { int selected = c39TriopticScreen.getSelectedIndex(); try { switch (selected) { case 0: // Enable CODE 39 Trioptic response = barCodeReader.code39Trioptic(true); if(!response){ alert = new Alert("Error", "C39 Trioptic Enable FAILED", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { resultScreen = new Form("Result"); resultScreen.append("C39 Trioptic Enabled!"); resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } break; case 1: // Disable CODE 39 Trioptic response = barCodeReader.code39Trioptic(false); if(!response){ alert = new Alert("Error", "C39 Trioptic Disable FAILED", null, AlertType.ERROR); alert.setTimeout(Alert.FOREVER); myDisplay.setCurrent(alert); } else { resultScreen = new Form("Result"); resultScreen.append("C39 Trioptic Disabled!"); resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } break; } } catch (Exception e) {

B-7

Page 72

PSM20i Bar Code Scanner Attachment Programmer’s Guide

System.err.println(e); resultScreen = new Form("Error!"); resultScreen.append("\n" + e.toString()); resultScreen.addCommand(okCommand); resultScreen.setCommandListener(this); myDisplay.setCurrent(resultScreen); } } // go back to main screen from c39 screen if ((c == backCommand) && ( (d == c39Screen) || (d == c39TriopticScreen))){ myDisplay.setCurrent(mainScreen); } // go back to main screen from scan screen if ((c == backCommand) && (d == decodeCanvas)) { myDisplay.setCurrent(mainScreen); } // go back to main screen from the result screen if ((c == okCommand) && (d == resultScreen)) { myDisplay.setCurrent(mainScreen); } // exit the MIDlet if ((c == exitCommand) && (d == mainScreen)) { // if (barCodeReader.isEnabled()) // barCodeReader.disable(); notifyDestroyed(); } } class EnableThread extends Thread { c39Test parent = null; public EnableThread(c39Test parent) { this.parent = parent; } public void run() { boolean result = false; try { System.out.println("Enabling.."); result = barCodeReader.enable(); System.out.println("Configuring..");

B-8

Page 73

Sample Program resultScreen.delete(0); System.out.println("Done.."); } catch (Exception e) { System.err.println(e); } if (result) { resultScreen.append("Ready to Scan"); } else { resultScreen.append("Error"); } resultScreen.addCommand(okCommand); resultScreen.setCommandListener(parent); myDisplay.setCurrent(resultScreen); } } class DecodeCanvas extends Canvas { private c39Test parent = null; private BarCodeReader barCodeReader = null; private String decodeString = ""; private int width = getWidth(); private int height = getHeight(); public DecodeCanvas(c39Test parent, BarCodeReader barCodeReader) { this.parent = parent; this.barCodeReader = barCodeReader; } public void paint(Graphics g) { // clear screen g.setColor(WHITE); // white g.fillRect(0, 0, width, height); // draw the title Font f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM); int yPos = 0; if (COLOR) g.setColor(BLUE); // blue else g.setColor(LIGHT_GRAY); g.fillRect(0, yPos, width, f.getHeight());

B-9

Page 74

PSM20i Bar Code Scanner Attachment Programmer’s Guide if (COLOR) g.setColor(WHITE); // white else g.setColor(BLACK); g.setFont(f); g.drawString("Barcode Scanner", 0, yPos, Graphics.LEFT | Graphics.TOP); g.setColor(BLACK); // black g.drawLine(0, f.getHeight() + yPos - 1, width, f.getHeight() + yPos - 1); // draw instructions f = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL); g.setFont(f); g.drawString("Press PTT to’", 0, 16, Graphics.TOP | Graphics.LEFT); g.drawString("begin scanning", 0, 16 + f.getHeight() + 1, Graphics.TOP | Graphics.LEFT); // draw the decoded text if (decodeString == null) decodeString = "Unable to read"; System.out.println("String width: " + f.stringWidth(decodeString)); g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_SMALL)); if (f.stringWidth(decodeString) < width) { g.drawString(decodeString, 0, height/2, Graphics.LEFT | Graphics.TOP); } else { int y = height/2; int start = 0; int increment = (width/f.charWidth(’W’)); int end = increment; while (end < decodeString.length()) { g.drawString(decodeString.substring(start, end), 0, y, Graphics.TOP | Graphics.LEFT); y = y + f.getHeight() + 1; start = end; end = end + increment; } // draw the last portion of the string g.drawString(decodeString.substring(start), 0, y, Graphics.TOP | Graphics.LEFT);

B-10

Page 75

Sample Program

} } public void keyPressed(int keyCode) { switch (keyCode) { case -50: // PTT button decodeString = barCodeReader.startDecode(); if (decodeString == null) { AlertType.CONFIRMATION.playSound(myDisplay); // error } else { if (decodeString.equals("NR")) AlertType.ERROR.playSound(myDisplay); // no decode else AlertType.INFO.playSound(myDisplay); // correct decode } repaint(); break; default: break; } } public void setDecodeText(String decodeString) { this.decodeString = decodeString; repaint(); } } }

// end of class c39Test

B-11

Page 76

PSM20i Bar Code Scanner Attachment Programmer’s Guide

B-12