Read barcodes over SoftwareSerial
What this example shows
The controller reads every code the scanner sends and prints it to the Serial Monitor. Here the scanner sits on two ordinary digital pins driven by SoftwareSerial.
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. The board works at 3.3 V logic, so use a 3.3 V controller.
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 SoftwareScan.ino
* @brief Scan and output barcodes using the DE2120 1D/2D barcode
* scanner module over SoftwareSerial.
*
* The scanner must first be put into TTL/RS232 mode by
* scanning the POR232 barcode from the DYScan settings
* manual. The library will automatically negotiate the
* baud rate down from 115200 to 9600 bps on first connect.
* 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()
{
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:
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
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. You do not need to call softSerial.begin() yourself.
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. Call it repeatedly from loop().
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. |
Putting it together
scanner.begin() starts the SoftwareSerial port itself, finds the scanner and
brings 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. If your board has a free hardware UART, the
hardware UART example
does the same job on it.