Problem with Mega2560 and receiving 16 characters via serial interface

I am currently writing an RS232 monitor. Using a Mega2560. I use basically the following code:

void setup() {
   Serial.begin(500000);
   Serial2.begin(9600,SERIAL_8N2);
}

void loop() {
 char IncomingChar; 
 while (Serial2.available() > 0) {       
      IncomingChar = Serial2.read();
      Serial.print(IncomingChar);
 }
}

This works fine except if I send a string of 16 characters. Then no characters are received or one or the whole string. 15 characters or less and 17 and more work fine.

Please edit your post and insert your code inside it, using code tags. The code tags make the code look

like this

when posting source code files. It makes it easier to read, and can be copied with a single mouse click. Also, if you don't do it, some of the character sequences in the code can be misinterpred by the forum code as italics or funny emoticons. The "Code: [Select]" feature allows someone to select the entire sketch so it can be easily copied and pasted into the IDE for testing.

Also, please post your entire sketch, so we can see what is going on.

wolvekamp:
I use basically the following code:

If what you posted is not the actual code that gives rise to the problem then please post the real program.

...R

I would like to share to the whole program but there is a 9000 character limit. So see attachment.

rs232-exp-v1.0.ino (8.91 KB)

Thank you, that is the correct way to post.

wolvekamp:
I would like to share to the whole program but there is a 9000 character limit. So see attachment.

There is a huge amount of stuff in that program that is probably irrelevant to your problem. Can you post a short complete program that illustrates the problem?

While preparing the short program you might even find the solution.

...R

I started with the short program (in the first post). When I load this program and send a string with 16 characters then the issue appears.

wolvekamp:
I started with the short program (in the first post). When I load this program and send a string with 16 characters then the issue appears.

How do you get away with a baud rate of 500,000? Am I missing something? Also what test characters are you sending? You talk about 15,16,17+ characters. Are they all the same character? Perhaps you are getting stuck on some value.

Have you tested this, by making a direct connection from the transmitter to the serial monitor (or comm program), i.e. bypass the pass through arrangement?

The 500.000 baud rate is for the USB port. That isn't the problem. The issue is what I read from serial port 2. It is a strange problem.

So if I execute on Unix the command:

echo "This is no issue" > /dev/tty.serial0 (17 characters including LF)

I get the response:
This is no issue

if I execute on Unix the command:

echo "This is a issue" > /dev/tty.serial0 (16 characters including LF)

I get the response:
T

if I execute on Unix the command:

echo "This is a issu" > /dev/tty.serial0 (15 characters including LF)

I get the response:
This is a issu

It always works fine except if I send a string of 16 characters. I use the last version of Arduino (1.8.13) on a Mac. Serial Port 2 is connected to a RS232 port on a Unix system.

wolvekamp:
I started with the short program (in the first post). When I load this program and send a string with 16 characters then the issue appears.

This is very confusing.

In Reply #2 I asked if the code in your Original Post would illustrate the problem and if not I asked you to post the code that does illustrate the problem. In response you posted the code in Reply #3 which led me to understand that the code in your Original Reply was irrelevant to the problem.

Now, to avoid any further misunderstanding, are you saying that when you run the short program in your Original Post you get the results that you describe in your Original Post?

...R

PS ... do you have a USB-TTL cable that you could try using with a terminal program such as PuTTY or Minicom. I routinely use Minicom for communicating with my Arduinos - some over the regular USB connection and some with a USB-TTL cable.

wolvekamp:
The 500.000 baud rate is for the USB port. That isn't the problem.

Have you actually tried changing it to something more typical, like 115200? Or you just believe it should work?

I regularly use 500,000 baud for communication between my Arduinos and my PC - both with the regular USB cable and with a USB-TTL cable. It is a speed at which the Atmega 328 has no timing error - unlike 115,200

...R

Sorry about the slight hijack, but I see no option for setting the baud to non-standard baud rates in Tera Term. Is that a feature in Putty, etc? Closest rate is 460,800...

Set it to 9600 for all i care, just to exclude it as an issue.

are you saying that when you run the short program in your Original Post you get the results that you describe in your Original Post?

that is the question !

are you saying that when you run the short program in your Original Post you get the results that you describe in your Original Post?

that is the question !

This would clearly be @wolvekamp's answer in Reply #6

I started with the short program (in the first post). When I load this program and send a string with 16 characters then the issue appears.

Perhaps he needs to repeat it. Just because we are all having trouble seeing the issue with the original code posted it doesn't mean he is not experiencing the issue.

His example of the serial output is very clear.

My question, what are the characters? There is a big difference between say, it only works with 16 characters when receiving "xxxxxxxxxxxUxxxxxxxxxxxUxx...." and "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...". In the former case, there is a clue to an external cause. In the latter, nothing can be found in the input that could trigger such a consistent response.

I tried to use Atom I got the same strange behavior. I will now check if the issue isn't in the Unix driver.

( By the way, I have tested with 115200 as output speed with the same strange behavior.)

:o It was a Unix driver problem. I have programmed a ESP8266 with the following code:

void setup() {
Serial.begin(9600);
}

void loop() {
Serial.println("This is no issue");
delay(2000);
Serial.println("This is a issue");
delay(2000);
}

Connected it to the serial2 port of the 2560 and I get both lines :-). Thanks for the input of every one!