Send a configuration command
What this example shows
sendCommand() changes any scanner setting, even one the module has no
dedicated function for. This example switches the Code ID on and off. With
Code ID on, the scanner puts a symbology identifier in front of every code (for
example A for Code 128 and Q for QR Code), so your program can tell barcode
types apart.
Type y or n in the REPL to switch it. The example keeps printing scanned
codes while it waits for your key.
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!")
# Set up non-blocking stdin so we can poll for barcodes while waiting for input
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)
while True:
flush_stdin()
print()
print("Transmit Code ID with barcode? (y/n)")
print("-------------------------------------")
print("Type 'y' or 'n', or scan a barcode:")
while not stdin_available():
barcode = scanner.readBarcode()
if barcode:
print("...")
print("Code found:", barcode)
time.sleep_ms(200)
cmd = sys.stdin.read(1)
if cmd == "y":
print("Code ID will be displayed on scan")
scanner.sendCommand("CIDENA", "1")
elif cmd == "n":
print("Code ID will NOT be displayed on scan")
scanner.sendCommand("CIDENA", "0")
else:
print("Command not recognized")
🔗Open the complete example
Examples/de2120-sendCommand.py at commit 848e03e
Expected result:
Scanner online!
Transmit Code ID with barcode? (y/n)
-------------------------------------
Type 'y' or 'n', or scan a barcode:
Type y and the example prints Code ID will be displayed on scan; type n
and it prints Code ID will NOT be displayed on scan. Any other key prints
Command not recognized. The prompt then appears again. A code scanned while
the prompt waits prints as ... followed by Code found: and the code's
contents, with the identifier in front of the code once Code ID is on.
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.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. A single code keeps at most 256 bytes.
ReturnsThe scanned code, or None when no complete scan has arrived yet.
scanner.sendCommand(cmd, arg, max_wait_ms)returns boolSends one configuration command to the scanner, framed the way it expects: ^_^, the command, the argument and a closing period (^_^CIDENA1. here). It then waits for the scanner's answer: an ACK byte (0x06) or a NACK byte (0x15).
ReturnsTrue on ACK; False on NACK or when max_wait_ms passes with no answer.
Parameters
| Type | Name | Description |
|---|---|---|
str | cmd | The command or property name, for example CIDENA (transfer Code ID). |
str | arg | The value, for example "1" to switch on or "0" to switch off. Empty by default. |
int | max_wait_ms | How long to wait for an answer, in milliseconds. 3000 by default. |
Putting it together
Each pass of the loop first empties any keys still waiting, then prints the
prompt. While no key arrives it calls readBarcode() every 200 ms, so scans
still show up. A y or n becomes sendCommand("CIDENA", "1") or
sendCommand("CIDENA", "0").
The same pattern sends any other setting. The module names the ones it knows
at the top of de2120.py, for example BEPSUC (beep on a successful decode,
1 by default) or IMGREG (reading area).