Wierd problems with serial communication

first of all: hello everyone. i am new here. :slight_smile:

i’m from germany, so i hope everyone can understand what i’m typing and help me. :smiley:

okay, here is my problem: i’m using the arduino mega 2560 and over the serial ports i want to communicate with a rfid-reader.
communication works fine with the old reader i have. it sends 12 bytes with 9600 baud and 8 data bits, no parity and one stop bit.
i can read those bytes and i can send them to my pc. it shows me all the all bytes send and they are the same ones i can see on my oscilloscope.
additionally i count the number of bytes that is send and send that number to a lcd-display.
and like it should it tells me 12.

so, now i got a new rfid reader.
this one communicates with 9600 baud and 8 data bits, even parity and one stop bit.
so i configured my serial port new with: Serial3.begin(9600, SERIAL_8E1);
this reader does send me 11 bytes. i see those bytes on my oscilloscope.
but i dont see them on my PC. sometimes i get the right bytes, but most of the time i only get 0B.
and when i check the number of bytes send like befor it tells me 256, or even higher numbers.
sometimes i see the 11, but most of the time it tells me a higher number.

what could be the problem?
the configuration of the serial port is done right.
i can send commandes to the reader and it understands me.

the bytes are being send all 50ms. is that to fast?
what could i do about that?
btw.: how does the serial buffer work in the first place?
how does it know that i am waiting for 11, or 12 bytes?

i use the same code for both readers. for the second one i just change it to even parity.

#include <LiquidCrystal.h>

LiquidCrystal lcd(9, 8, 5, 4, 3, 2);

int come_in [12];
char test_speicher [12];
int x = 0;
int z=0;

const int reset   = 40;           // Pin-Nummer Reset-Pin des RFID-Moduls

void setup()
{  
  lcd.begin(20, 4);
  lcd.clear(); 
  lcd.setCursor(0, 0);
  lcd.print ("Hallo");
  
  Serial.begin(9600);
  Serial3.begin(9600, SERIAL_8E1);    //9600 baud, 8 data bits, even parity, 1 stop bit 
   
  digitalWrite(reset,HIGH);
  
  lcd.clear();
 }

void loop()
{
if (Serial3.available()) {
    // wait a bit for the entire message to arrive
    delay(100);
    
    // read all the available characters
    while (Serial3.available() > 0) {
      come_in[ z ] = Serial3.read();
      z++;
    }
  
    for( int i=0; i<z; i++ ){
      test_speicher[ i ] = come_in[ i ];
      }
      
    Serial.print(test_speicher); 
 
  lcd.setCursor(0, 1);
  lcd.print( z );
}

oh and an other quick question: I am using the the first serial port “Serial” too and everytime i want to program the board over usb, i have to disconnect the pins 0 and 1.
is that the only way to program the board?

hope u understood my problem and can maybe help me.

btw.: feel free to correct my english. ^^

You have to disconnect the serial port used by usb-to-serial for the Arduino IDE.
But you can make more serial ports with SoftwareSerial.
Perhaps you can keep the first serial port for the Arduino IDE.

If you use a oscilloscope, can you measure and calculate the baudrate and the voltage level ? If the voltage level is only 3V and the baudrate is 10% off, that could be a problem.

You don’t check for the maximum buffer size while characters are being received. So it will be very easy to get ram overflow.
while (Serial3.available() > 0 && z < sizeof( come_in))

You have to disconnect the serial port used by usb-to-serial for the Arduino IDE. But you can make more serial ports with SoftwareSerial. Perhaps you can keep the first serial port for the Arduino IDE.

In order: No. usb-to-serial is on Serial, not Serial3. Not necessary. There are 4 hardware serial ports on the Mega. The OP already is using Serial (separate port) for the IDE.

i am also using Serial for sending the data read from Serial3.

and isnt the maximum buffer size 64?

okay, i made some progress with the help of a colleague.

so you dont have to read the first post:

i’m using the arduino mega 2560 and over the serial ports i want to communicate with a rfid-reader.

i am using this now:

void loop(){

  lcd.setCursor(0, 0);
  lcd.print ("Loop");

  if (Serial3.available()) {

    Serial3.setTimeout(50);
    Serial3.readBytes(come_in,11);

    for( int i=5; i<10; i++ ){                       // I get 11 bytes, but only need the information
      test_speicher[ i-5 ] = come_in[ i ];      // in the bytes 6 to 10
    }

    test_speicher[5] = 0;   

    Serial2.print((char*)test_speicher);

  } 

  delay(1000);

}

test_speicher is a char array with the size of 6.

so this works quite good.
but i have a few things that bother me.

1st: even if i hold the transponder into the antenna for only a short moment i already get 5 serial prints to my PC.

2nd: if i let the transponder stay in the antenna field, after a short time, i get wrong numbers.