I have a GM65 barcode module (link here: https://www.dfrobot.com/product-1996.html) which I am trying to connect to my Arduino UNO R4 WiFi and print out the barcode to the Serial monitor. The main issue is I am unable to get Serial.available() to work. I have tried two different approaches:
Using hardware UART pins 0, 1 for serial communication.
Using SoftwareSerial for digital serial pins, 10, 11.
I also tried using the GM65 library found here -> GitHub - xuegangxiao0117/GM65_scanner_for_Arduino: Arduino library for GM60 scanner
Which i was unable to get to work either. I have seen videos online of it working but the problem boils down to Serial.available() is never triggered. There is not an issue regarding baud rate either.
Does anyone have any experience using a barcode scanner to connect to Arduino over UART RX/TX? If so, any help or tips would be appreciated. Thanks
#include <SoftwareSerial.h>
#include "GM65_scanner.h"
//SoftwareSerial mySerial(10, 11); // not needed, should not be used with UnoR4
GM65_scanner scanner(&Serial1);
Here is the setup. Black wire is TX on barcode, Yellow is RX.
This is what I tried for hardware UART, when trying SoftwareSerial i used pins 10, 11 and called SoftwareSerial mySerial(10, 11);
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
void setup()
{
Serial.begin(9600);
while (!Serial) {};
mySerial.begin(9600);
}
void loop()
{
if (mySerial.available()) {
i = mySerial.read();
Serial.println(i);
}
}
and trying with gm65_scanner.h:
#include "GM65_scanner.h"
GM65_scanner scanner(&mySerial);
int buttonPin = 4;
void setup() {
pinMode(buttonPin,INPUT);
Serial.begin(115200);
mySerial.begin(9600);
Serial.println("initialize the scanner...");
scanner.init();
Serial.println("enable the setting QR code");
scanner.enable_setting_code();
//scan once function, only works in command mode
Serial.println("change to command trigger mode");
scanner.set_working_mode(1);
}
void loop() {
int button_state = digitalRead(buttonPin);
if(button_state == HIGH){
scanner.scan_once();
}
}
I was unable to read any barcodes from the serial monitor in any of these cases. Also as a side note I'm having trouble using the GM65_scanner library, this code is from an example given in the lib. Regardless, I still have trouble getting it to output.
Also, the barcode reader scans and beeps when scanning, and it works with USB. Just not UART.
This sets up communications between the Arduino and the PC/laptop.
This sets up communications between the Arduino and the scanner.
Yes, you need both.
Unless ...
If all you ever want to do is have the Arduino take the output from the scanner and pass it through to the PC without alteration, then the Arduino would be wasted.
The scanner also has a USB interface, and with a suitable cable can be connected directly to the PC.
The connections look correct to me. You have the barcode scanner TX going to RX on the Arduino.
I don't have a barcode scanner, but I tested your code using a gps receiver.
The first code, using Serial1 worked straight away.
Once I had it working, I removed the line that prints "Received: ", and on the next line
changed Serial.println() to Serial.print() .
The second code that you posted using SoftwareSerial would not work for me - Until I changed the pins used from 10 & 11 to 2 & 3.
I tried using some other pins (but not all) . It seemed that low numbered pins worked but high numbers didn't.
Here's proof of both versions of the code working:
As mentioned already by @JohnLincoln, only certain pins on the UNO R4 WiFi board can be used as the "SoftwareSerial" library's RX pin. These are the pins you can use:
When investigating the subject of this forum topic, I also looked to that "SerialEcho" example sketch you mention for information about supported pins after not finding it in the SoftwareSerial library documentation. I found that the information in the comment was incorrect, incomplete, and confusing. I submitted a report about this to the developers:
Since experience tells me that it is not realistic to hope that we can maintain redundant copies of any given documentation, I have proposed to replace the information in the example sketch comment with a link to the SoftwareSerial library documentation. However, before that could be done the information must be added to the documentation. So I am waiting for my documentation pull request to be merged, after which I will submit a pull request to replace the erroneous comment in the SerialEcho example.