Read barcodes continuously
What this example shows
The example switches the scanner to continuous mode, where it reads on its own
without the trigger button or a startScan() call. Point it at a code and the
code prints in the shell. The example sets a 0.5 s repeat interval for the same
code.
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 code
from machine import UART
from de2120 import DE2120
import time
# Adjust tx and rx to match the pins you have wired on your board.
scanner = DE2120(tx=5, rx=4)
if not scanner.begin():
print("DE2120 not detected. Check wiring.")
raise SystemExit
# Enable continuous read with a 0.5 s repeat interval for the same barcode
scanner.enableContinuousRead(repeat_interval=2)
print("DE2120 in continuous read mode. Point at a barcode.")
while True:
barcode = scanner.readBarcode()
if barcode:
print("Scanned:", barcode)
time.sleep_ms(50)
🔗Open the complete example
Examples/de2120-continuousRead.py at commit 848e03e
Expected result:
DE2120 in continuous read mode. Point at a barcode.
Each code the scanner reads then prints as Scanned: followed by the code's
contents. When the scanner does not answer, the example prints
DE2120 not detected. Check wiring. and stops.
Functions used
DE2120(uart=None, tx=None, rx=None, uart_id=1)returns DE2120Creates 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
| Type | Name | Description |
|---|---|---|
UART | uart | A ready machine.UART object. Optional. |
int | tx | The controller pin that transmits, wired to the scanner's RX. The example uses 5. |
int | rx | The controller pin that receives, wired to the scanner's TX. The example uses 4. |
int | uart_id | Which UART to create when you pass tx and rx. 1 by default. |
scanner.begin()returns boolChecks 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.enableContinuousRead(repeat_interval=2)returns boolPuts 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
| Type | Name | Description |
|---|---|---|
int | repeat_interval | For the same code: 0 output once, 1 no delay, 2 every 0.5 s, 3 every 1 s. 2 by default; the example passes 2. |
scanner.readBarcode()returns str or NoneCollects the bytes waiting from the scanner. A scan ends with a carriage return; when one arrives, the function returns the code without it. Call it repeatedly in a loop. A single code keeps at most 256 bytes.
ReturnsThe scanned code, or None when no complete scan has arrived yet.
Putting it together
After begin() finds the scanner, one call to enableContinuousRead() changes
its reading mode. From then on the scanner sends codes by itself, and the loop
only collects them with readBarcode() every 50 ms.
To go back to the trigger button, call disableContinuousRead(), which puts
the scanner in manual mode again.