Skip to main content

Change scanner settings from the REPL

What this example shows​

A menu in the REPL drives the scanner while it runs: start and stop a scan, switch the white illumination and the red aiming light on and off, pick how much of the camera frame is searched for a code, choose manual, continuous or motion-sense reading, and switch the 1D and 2D symbology groups on and off. Codes you scan while any menu waits still print.

Wire the controller's pin 5 (TX) to the scanner's RX and its pin 4 (RX) to the scanner's TX, plus 3V3 and GND, or change tx and rx to match your wiring. Switch the scanner to UART first by scanning the POR232 configuration barcode.

The example reads the REPL's input without blocking through uselect and sys, which are not part of the de2120 module; see MicroPython's select documentation.

The example code​

import sys
import uselect
from de2120 import DE2120
import time

scanner = DE2120(tx=5, rx=4)

if not scanner.begin():
print("Scanner did not respond. Check wiring. Did you scan the POR232 barcode?")
raise SystemExit

print("Scanner online!")

poll = uselect.poll()
poll.register(sys.stdin, uselect.POLLIN)


def stdin_available():
return bool(poll.poll(0))


def flush_stdin():
while stdin_available():
sys.stdin.read(1)


def wait_for_key():
"""Block until a key is typed, printing any scanned barcodes in the meantime."""
while not stdin_available():
barcode = scanner.readBarcode()
if barcode:
print("...")
print("Code found:", barcode)
time.sleep_ms(200)
return sys.stdin.read(1)


def menu_flashlight():
flush_stdin()
print()
print("-------------------------------------")
print("1) Enable Flashlight")
print("2) Disable Flashlight")
print("-------------------------------------")
print("Select an option number:")

cmd = wait_for_key()
if cmd == "1":
print("White scan light on")
scanner.lightOn()
elif cmd == "2":
print("White scan light off")
scanner.lightOff()
else:
print("Command not recognized")


def menu_reticle():
flush_stdin()
print()
print("-------------------------------------")
print("1) Enable Reticle")
print("2) Disable Reticle")
print("-------------------------------------")
print("Select an option number:")

cmd = wait_for_key()
if cmd == "1":
print("Red scan reticle on")
scanner.reticleOn()
elif cmd == "2":
print("Red scan reticle off")
scanner.reticleOff()
else:
print("Command not recognized")


def menu_reading_area():
flush_stdin()
print()
print("-------------------------------------")
print("1) Full Width (Default)")
print("2) Center 80%")
print("3) Center 60%")
print("4) Center 40%")
print("5) Center 20%")
print("-------------------------------------")
print("Select an option number:")

cmd = wait_for_key()
areas = {"1": 100, "2": 80, "3": 60, "4": 40, "5": 20}
if cmd in areas:
pct = areas[cmd]
print("Scanning {}% of frame".format(pct))
scanner.changeReadingArea(pct)
else:
print("Command not recognized")


def menu_reading_mode():
flush_stdin()
print()
print("-------------------------------------")
print("1) Manual Read Mode (Default)")
print("2) Continuous Read Mode")
print("3) Motion Sensor Mode")
print("-------------------------------------")
print("Select an option number:")

cmd = wait_for_key()
if cmd == "1":
print("Manual Trigger Mode enabled")
scanner.disableMotionSense()
elif cmd == "2":
print("Continuous Read Mode enabled")
scanner.enableContinuousRead()
elif cmd == "3":
print("Motion Trigger Mode enabled")
scanner.enableMotionSense()
else:
print("Command not recognized")


def menu_symbologies():
flush_stdin()
print()
print("-------------------------------------")
print("1) Enable All 1D Symbologies")
print("2) Disable All 1D Symbologies")
print("3) Enable All 2D Symbologies")
print("4) Disable All 2D Symbologies")
print("-------------------------------------")
print("Select an option number:")

cmd = wait_for_key()
if cmd == "1":
print("1D Symbologies enabled")
scanner.enableAll1D()
elif cmd == "2":
print("1D Symbologies disabled")
scanner.disableAll1D()
elif cmd == "3":
print("2D Symbologies enabled")
scanner.enableAll2D()
elif cmd == "4":
print("2D Symbologies disabled")
scanner.disableAll2D()
else:
print("Command not recognized")


while True:
flush_stdin()

print()
print("DE2120 Barcode Scanner")
print("-------------------------------------")
print("1) Start Scan")
print("2) Stop Scan")
print("3) Enable/Disable Flashlight")
print("4) Enable/Disable Aiming Reticle")
print("5) Set Reading Area")
print("6) Set Reading Mode")
print("7) Enable/Disable Symbologies")
print("-------------------------------------")
print("Select an option number:")

cmd = wait_for_key()

if cmd == "1":
scanner.startScan()
elif cmd == "2":
scanner.stopScan()
elif cmd == "3":
menu_flashlight()
elif cmd == "4":
menu_reticle()
elif cmd == "5":
menu_reading_area()
elif cmd == "6":
menu_reading_mode()
elif cmd == "7":
menu_symbologies()
else:
print("Command not recognized")

🔗Open the complete example

Examples/de2120-serialSettings.py at commit 848e03e

→

Expected result:

ShellMicroPython
Scanner online!

DE2120 Barcode Scanner
-------------------------------------
1) Start Scan
2) Stop Scan
3) Enable/Disable Flashlight
4) Enable/Disable Aiming Reticle
5) Set Reading Area
6) Set Reading Mode
7) Enable/Disable Symbologies
-------------------------------------
Select an option number:

Type a number to pick an option. Options 3 to 7 open a second menu and print a confirmation such as White scan light on, Scanning 60% of frame or Continuous Read Mode enabled once you choose; options 1 and 2 act at once without a message. The main menu then appears again. A code scanned while any menu waits prints as ... followed by Code found: and the code's contents.

Functions used​

DE2120(uart=None, tx=None, rx=None, uart_id=1)returns DE2120

Creates the scanner object. Pass a UART you made yourself as uart, or pass tx and rx pin numbers and the module creates UART(uart_id) at 9600 baud on them. With neither, it raises ValueError.

ReturnsThe scanner object.

Parameters

TypeNameDescription
UARTuartA ready machine.UART object. Optional.
inttxThe controller pin that transmits, wired to the scanner's RX. The example uses 5.
intrxThe controller pin that receives, wired to the scanner's TX. The example uses 4.
intuart_idWhich UART to create when you pass tx and rx. 1 by default.
scanner.begin()returns bool

Checks that the scanner answers. It sets the UART to 9600 baud and asks for the firmware version. Without an answer it retries at 115200 baud, tells the scanner to switch to 9600 baud, and asks again. On success it empties the receive buffer.

ReturnsTrue when the scanner answered, False when it did not.

scanner.readBarcode()returns str or None

Collects the bytes waiting from the scanner. A scan ends with a carriage return; when one arrives, the function returns the code without it. A single code keeps at most 256 bytes.

ReturnsThe scanned code, or None when no complete scan has arrived yet.

scanner.startScan()returns bool

Sends the SCAN command, which starts a single scan in manual (trigger) mode.

ReturnsTrue when the scanner acknowledged the command.

scanner.stopScan()returns bool

Sends the SLEEP command, which stops a scan in progress.

ReturnsTrue when the scanner acknowledged the command.

scanner.lightOn()returns bool

Switches the white illumination LEDs on (LAMENA1). They are on by default.

ReturnsTrue when the scanner acknowledged the command.

scanner.lightOff()returns bool

Switches the white illumination LEDs off (LAMENA0).

ReturnsTrue when the scanner acknowledged the command.

scanner.reticleOn()returns bool

Switches the red aiming light on (AIMENA1). It is on by default.

ReturnsTrue when the scanner acknowledged the command.

scanner.reticleOff()returns bool

Switches the red aiming light off (AIMENA0).

ReturnsTrue when the scanner acknowledged the command.

scanner.changeReadingArea(percent)returns bool

Sets how much of the camera frame, measured from its centre, the scanner searches for a code (IMGREG0 to IMGREG4). The full frame is the default.

ReturnsTrue when the scanner acknowledged the command; False for a value the function does not accept.

Parameters

TypeNameDescription
intpercent100 (full width), 80, 60, 40 or 20.
scanner.disableMotionSense()returns bool

Puts the scanner in manual (trigger) mode, the default (SCMMAN). The example uses it for its Manual Read Mode option.

ReturnsTrue when the scanner acknowledged the command.

scanner.enableContinuousRead(repeat_interval=2)returns bool

Puts the scanner in continuous mode (SCMCNT), where it reads without a trigger, and sets how often the same code is sent again (CNTALW).

ReturnsTrue when the scanner acknowledged the interval setting; False for a value outside 0 to 3.

Parameters

TypeNameDescription
intrepeat_intervalFor the same code: 0 output once, 1 no delay, 2 every 0.5 s, 3 every 1 s. 2 by default, which the example keeps.
scanner.enableMotionSense(sensitivity=50)returns bool

Puts the scanner in motion-sense mode (SCMMDH) and sets its motion sensitivity (MDTTHR).

ReturnsTrue when the scanner acknowledged the sensitivity setting; False for a value the function does not accept.

Parameters

TypeNameDescription
intsensitivity15 (highest), 20, 30, 50 or 100 (lowest). 50 by default, which the example keeps.
scanner.enableAll1D()returns bool

Switches on decoding of all 1D symbologies (ODCENA).

ReturnsTrue when the scanner acknowledged the command.

scanner.disableAll1D()returns bool

Switches off decoding of all 1D symbologies (ODCDIS).

ReturnsTrue when the scanner acknowledged the command.

scanner.enableAll2D()returns bool

Switches on decoding of all 2D symbologies (AQRENA).

ReturnsTrue when the scanner acknowledged the command.

scanner.disableAll2D()returns bool

Switches off decoding of all 2D symbologies (AQRDIS).

ReturnsTrue when the scanner acknowledged the command.

Putting it together​

wait_for_key() is the heart of the example: it keeps calling readBarcode() until a key arrives, so scanning never stops while a menu is open. Each menu entry then maps to one function, and each function sends one or two of the scanner's configuration commands and waits for its acknowledgement. The example ignores the return values; in your own code, check them to see whether the scanner accepted a setting.

In continuous mode the scanner reads without the trigger. To read on demand instead, stay in manual mode and call startScan() from your code, or pull TRIG low.