Send a configuration command
What this example shows
Any scanner setting can be changed from your sketch with sendCommand(), even
one the library 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 it sends (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 Serial Monitor to switch it. The sketch keeps printing
scanned codes while it waits for your answer.
The example uses pin 4 as the controller's RX (wire it to the scanner's TX)
and pin 5 as its TX (wire it to the scanner's RX); change the two numbers to
match your wiring. Use a 3.3 V controller, and switch the scanner to UART first
by scanning the POR232 configuration barcode.
This example also uses SoftwareSerial, which is not part of the Soldered DE2120 library. The example's comment says you can find it in the Arduino Library Manager.
The example code
/**
**************************************************
*
* @file SendCommand.ino
* @brief Toggle Code ID prefix transmission on the DE2120
* 1D/2D barcode scanner module over SoftwareSerial.
*
* When Code ID is enabled, the scanner prepends a
* symbology identifier character to every scanned barcode
* (e.g. 'A' for Code 128, 'Q' for QR Code). This can help
* the host application distinguish barcode types at runtime.
*
* The sketch presents a Serial Monitor prompt and also
* continuously polls for barcodes while waiting for input.
* Sending 'y' enables Code ID via the CIDENA command;
* sending 'n' disables it.
*
* The scanner must first be put into TTL/RS232 mode by
* scanning the POR232 barcode from the DYScan settings
* manual. Change the SoftwareSerial pin arguments to match
* your wiring before uploading.
*
* Make sure you have SoftwareSerial library installed. It
* can be found in Arduino library manager.
*
* @authors Fran Fodor @ soldered.com
***************************************************/
#include "SoftwareSerial.h"
SoftwareSerial softSerial(4, 5); // change according to your wiring
#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");
if (scanner.begin(softSerial) == 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()
{
flushRx(); // Clear the serial rx buffer to avoid line endings
Serial.println();
Serial.println("Transmit Code ID with Barcode? (y/n)");
Serial.println("-------------------------------------");
Serial.println("Type 'y' or 'n' or scan a barcode: ");
// Wait for the user to reply but look for barcodes in the meantime
while (Serial.available() == false)
{
if (scanner.readBarcode(scanBuffer, BUFFER_LEN))
{
Serial.println("...");
Serial.print("Code found: ");
for (int i = 0; i < strlen(scanBuffer); i++)
Serial.print(scanBuffer[i]);
Serial.println();
}
delay(200);
}
switch (Serial.read())
{
case 'y':
Serial.println("Code ID will be displayed on scan");
scanner.sendCommand("CIDENA", "1");
break;
case 'n':
Serial.println("Code ID will NOT be displayed on scan");
scanner.sendCommand("CIDENA", "0");
break;
default:
Serial.println("Command not recognized");
break;
}
}
void flushRx(){
while(Serial.available()){
Serial.read();
delay(1);
}
}
Expected result:
DE2120 Scanner Example
Scanner online!
Transmit Code ID with Barcode? (y/n)
-------------------------------------
Type 'y' or 'n' or scan a barcode:
Send y and the sketch prints Code ID will be displayed on scan; send n
and it prints Code ID will NOT be displayed on scan. Any other character
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
SoftwareSerial(rxPin, txPin, inverse_logic)returns NoneConstructor from the SoftwareSerial library. Creates a serial port on two digital pins, run in software. Only one SoftwareSerial port can receive at a time.
ReturnsConstructor; no return value.
Parameters
| Type | Name | Description |
|---|---|---|
| pin number | rxPin | The pin that receives, wired to the scanner's TX. The example uses 4. |
| pin number | txPin | The pin that transmits, wired to the scanner's RX. The example uses 5. |
bool | inverse_logic | Inverts the signal sense. Optional, false by default; the example leaves it out. |
DE2120_Soldered()returns NoneCreates the scanner object. It holds no pins or port yet: begin() connects it to a serial port.
ReturnsConstructor; no return value.
scanner.begin(SoftwareSerial &serialPort)returns boolConnects the scanner object to a SoftwareSerial port and checks that the scanner answers. It starts the port at 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
| Type | Name | Description |
|---|---|---|
SoftwareSerial & | serialPort | The port the scanner is wired to, here softSerial. |
scanner.readBarcode(char *resultBuffer, uint8_t size)returns boolReads 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.
Returnstrue when a complete scan is in the buffer, false when no complete scan has arrived yet.
Parameters
| Type | Name | Description |
|---|---|---|
char * | resultBuffer | Buffer that collects the code. The example uses 40 bytes. |
uint8_t | size | Size of the buffer. At most this many bytes are read, so a longer code is cut short. |
scanner.sendCommand(const char *cmd, const char *arg, uint32_t maxWaitInms)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). The command and the argument together fit 9 characters.
Returnstrue on ACK; false on NACK or when maxWaitInms passes with no answer.
Parameters
| Type | Name | Description |
|---|---|---|
const char * | cmd | The command or property name, for example CIDENA (transfer Code ID). |
const char * | arg | The value, for example 1 to switch on or 0 to switch off. Empty by default. |
uint32_t | maxWaitInms | How long to wait for an answer, in milliseconds. 3000 by default. |
Putting it together
loop() first clears anything left in the Serial Monitor's input, such as the
line ending after your last answer, then prints the prompt. While no key
arrives it polls 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 library names the ones it knows
in SparkFun_DE2120_Arduino_Library.h, for example BEPSUC (beep on a
successful decode, 1 by default) or IMGREG (reading area).