Skip to main content

Read barcodes over a hardware UART

What this example shows​

An ESP32 talks to the scanner through its hardware UART2 and prints every code the scanner reads to the Serial Monitor. A hardware UART is a dedicated serial peripheral, which the example's own comment calls more reliable than SoftwareSerial.

Wire it as the example's comment says: GPIO4 is the ESP32's RX (to the scanner's TX) and GPIO5 its TX (to the scanner's RX), plus 3V3 and GND. Switch the scanner to UART first by scanning the POR232 configuration barcode.

This example also uses HardwareSerial, which comes with the ESP32 board package.

The example code​

Select Board
HardwareScan.ino
/**
**************************************************
*
* @file HardwareScan.ino
* @brief Scan and output barcodes using the DE2120 1D/2D barcode
* scanner module over HardwareSerial (UART2) on ESP32.
*
* Unlike SoftwareSerial, HardwareSerial uses a dedicated
* UART peripheral for reliable communication at 115200 bps.
* The library will automatically negotiate the baud rate
* down to 9600 bps on first connect.
*
* Default wiring: GPIO4 = RX (scanner TX),
* GPIO5 = TX (scanner RX). Change the pin arguments in
* scannerSerial.begin() to match your wiring.
*
* The scanner must first be put into TTL/RS232 mode by
* scanning the POR232 barcode from the DYScan settings
* manual before uploading this sketch.
*
* Make sure you have SoftwareSerial library installed. It
* can be found in Arduino library manager.
*
* @authors Fran Fodor @ soldered.com
***************************************************/

#include "HardwareSerial.h"
HardwareSerial scannerSerial(2); // Use UART2

#include "DE2120-SOLDERED.h"
DE2120_Soldered scanner;

#define BUFFER_LEN 40
char scanBuffer[BUFFER_LEN];

void setup()
{
Serial.begin(115200);
Serial.println("DE2120 Scanner Example");

scannerSerial.begin(115200, SERIAL_8N1, 4, 5); // RX=GPIO4, TX=GPIO5
delay(500);

if (scanner.begin(scannerSerial) == false)
{
Serial.println("Scanner did not respond. Please check wiring. Did you scan the POR232 barcode? Freezing...");
while (1);
}
Serial.println("Scanner online!");
}

void loop()
{
if (scanner.readBarcode(scanBuffer, BUFFER_LEN))
{
Serial.print("Code found: ");
for (int i = 0; i < strlen(scanBuffer); i++)
Serial.print(scanBuffer[i]);
Serial.println();
}
delay(200);
}

Expected result:

Serial MonitorAutoscrollShow timestampNewline115200 baud
Message (Enter to send message to the board)
DE2120 Scanner Example
Scanner online!

Then press the trigger button on the board and point it at a code. Each code the scanner reads prints on its own line as Code found: followed by the code's contents. When the scanner does not answer, the sketch prints Scanner did not respond. Please check wiring. Did you scan the POR232 barcode? Freezing... and stops.

Functions used​

HardwareSerial(uint8_t uart_nr)returns None

Constructor from the ESP32 board package. Creates a serial port on one of the ESP32's hardware UARTs. The example uses UART2.

ReturnsConstructor; no return value.

Parameters

TypeNameDescription
uint8_tuart_nrThe hardware UART number. The example passes 2.
scannerSerial.begin(unsigned long baud, uint32_t config = SERIAL_8N1, int8_t rxPin = -1, int8_t txPin = -1, ...)returns void

From the ESP32 board package. Starts the hardware UART at a baud rate and frame format on the RX and TX pins you name. The scanner library changes the baud rate itself afterwards, so the 115200 here is only a starting value.

ReturnsNothing.

Parameters

TypeNameDescription
unsigned longbaudBaud rate. The example passes 115200.
uint32_tconfigData bits, parity and stop bits. SERIAL_8N1 is the default.
int8_trxPinThe ESP32 pin that receives, wired to the scanner's TX. The example uses 4.
int8_ttxPinThe ESP32 pin that transmits, wired to the scanner's RX. The example uses 5.
DE2120_Soldered()returns None

Creates the scanner object. It holds no pins or port yet: begin() connects it to a serial port.

ReturnsConstructor; no return value.

scanner.begin(HardwareSerial &serialPort)returns bool

Connects the scanner object to a hardware serial port and checks that the scanner answers. It sets the port 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 so the first scan is read cleanly.

Returnstrue when the scanner answered, false when it did not.

Parameters

TypeNameDescription
HardwareSerial &serialPortThe port the scanner is wired to, here scannerSerial.
scanner.readBarcode(char *resultBuffer, uint8_t size)returns bool

Reads the bytes waiting from the scanner into resultBuffer. A scan ends with a carriage return; once one arrives, the function returns true and the buffer holds the code, the carriage return and a terminating zero. A buffer that already holds a finished scan is cleared before the next one. Call it repeatedly from loop().

Returnstrue when a complete scan is in the buffer, false when no complete scan has arrived yet.

Parameters

TypeNameDescription
char *resultBufferBuffer that collects the code. The example uses 40 bytes.
uint8_tsizeSize of the buffer. At most this many bytes are read, so a longer code is cut short.

Putting it together​

scannerSerial.begin() sets up UART2 on the pins you wired, and scanner.begin() uses that port to find the scanner and bring it to 9600 baud. From then on loop() polls readBarcode() every 200 ms. When it returns true, the sketch prints the buffer one character at a time after Code found: .

The scanner starts in manual mode, so it reads while the trigger button is pressed or TRIG is held low. To read without the button, see Change scanner settings from the Serial Monitor.