I am wanting to add a barcode scanner to my Arduino project.
Looking on the internet I see lots of barcode scanners with "UART" compatibility. I looked up the UART protocol and how to use it with the Serial object.
But none of the scanners say what UART values to send via the Serial object in order to start scanning, stop scanning, or know when the scanner got a value. They all seem to assume that I will just know this.
I have tried searching for common UART code for barcode scanners, but I can't seem to find anything.
Are there common commands/values to send via UART for start and stop the scanning of a barcode scanner? (If so, what are they?)
UART is an RX/TX protocol, unlike with e.g. I2C or SPI there is no master who always has to initiate/start the communication. So with UART between an Arduino and a barcode scanner, both can send each other data when it's needed (the data will be in a buffer for the receiver to read).
If you look closely at examples like this one: https://www.rtscan.net/arduino-qr-code-scanner/
You see two things:
if (mySerial.available() > 0 )Here the Arduino checks if some data was received from the scanner (if the function returns an integer value >0 this means that there are that amount of bytes available to be read from the scanner.
send_cmd()function is used to send data/commands to the scanner, in order to change the baud rate (the UART communication speed).PS: in this kind of application it's important to use the SoftwareSerial library, because if you try to use the standard Serial interface (pins 0 and 1 and the default Serial connection between the pc and the Arduino) then the pc-Arduino link would interfere with the Arduino-scanner link.