Barcode reader -Arduinoo Mega2560

Hello everyone!!
I'm looking for a few days how to make my arduino mega card 2560 read data from a barcode reader module (Lecteur de code-barres DFR0314 DFRobot - Articles retires | GO TRONIC) connected to the card using RS232-TTL converter (Convertisseur RS232 - TTL DFR0077 DFRobot - Convertisseurs | GO TRONIC) .

so I wrote the following arduino code using a code in the technical file of the module :
#define ARDUINO_RX_PIN 0
#define ARDUINO_TX_PIN 1

String code = ""; //initialize the output string
boolean endbit = 0; //a flag to mark 0D received
char temp;

void setup() {
Serial.begin(9600); //initialize the Serial port

pinMode(ARDUINO_RX_PIN, INPUT);
pinMode(ARDUINO_TX_PIN, OUTPUT);

if (Serial.available() > 0) {
temp = char( Serial.read()); //read the input data
code += temp;
}
if (temp == 0x0D){ // Or temp == '\r'
Serial.println(code);
code = "";
endbit = 0;
temp = 0;
}

}

but i get no result :confused:
any help please ....

assuming your wiring is all good, since you are using a Mega, it would make more sense to connect your barcode to one if its other serial ports and something like this in code (untested):

String code = "";           //initialize the output string
boolean endbit = 0;            //a flag to mark 0D received
char temp;

void setup() {
  Serial.begin(9600);       //initialize the Serial port
  Serial1.begin(9600); //for your barcode reader
}

void loop() {
  if (Serial1.available() > 0)     {
    temp = char( Serial1.read());    //read the input data
    code += temp;
  }
  if (temp == 0x0D) {          // Or temp == '\r'
    Serial.println(code); //output to serial monitor
    code = "";
    endbit = 0;
    temp = 0;
  }
}

Hope that helps.

Yes - agree - don't mess with Serial both for the console and your module... have a dedicated Serial port for the barcode scanner

also avoid the String class - read this tutorial Serial Input Basics to understand how to handle Serial communications (or that one since you might be french)

thank you both for responding me :slight_smile:
@ sherzaad i tried the code you gave stil the same problem
@J-M-L i 'll try with that and i will tell you the result

make sure your barcode is configured at 9600 bauds