Keyboard.h goes crazy

Hi Guys!

Please give me a wise advice.

Here are two Arduinos. A Mega (provides data) and a Leonardo (transmits them to the PC)

I try to use an Arduino Leonardo as an "auto USB keyboard" connected to a PC.

It gets data from another Arduino ( Mega) via serial port. The data are read from a file on SD card located on the Mega. They are containing plain text. The transfer of bytes is OK to the Leonardo because I see them in the Serial monitor.

BUT....

Using the Keyboard.h and writing them out on Leonardo (to act as USB keyboard,) it goes crazy if the transmitted bytes more than 120 characters. In this case I see only some "letter salad"

So this is my issue....

Thank you for your help!

Have a nice day!

Gábor

Welcome to the forum

Post the sketches running on the Mega and Leo, using code tags when you do

Hi Bob!

Thank you-replied me!

In the Mega the code is simple:

  • I open a textfile.
    -Byte by byte send its content to the Serial1 of Mega.
    -When I reach the end of file I close it.

The content of file appears truly and correctly on Serial1 of Mega, and ALSO in Serial1 of Leonardo. So it works properly.

The Leonardo code is simple(based on an internal example)

#include "Keyboard.h"

void setup()
{
Serial.begin(9600); // for testing purpose
Serial1.begin(9600); //the incoming port. Data from the Mega
Keyboard.begin(); // HUH! THE PROBLEM MAKER!

}

void loop()
{
char incoming; // the character

                   if(Serial.available()>0) //more than 0 bytes in the serial buffer
                   {
                        incoming=Serial1.read(); // reading in
                       
                       Serial.write(incoming); // the testing port to be monitored.
                       Keyboard.write(incoming); //writing out as USB keyboard
                   }

}

This is so simple. If the number of incoming characters is less than 120 then all works fine If more than 120 everything goes mad! In the Leonardo's own serial port ( Serial) everything appears well. Yet in case of more than 120 incoming bytes! This makes me think that maybe the issue is in the Keyboard library....

How could I solve or circumwent the problem?

:slight_smile:

Gábor

Not a solution, but as an experiment slow down the transmission of data from the Mega to the Leo by adding a delay() between each byte

#include "Keyboard.h"

void setup() {
  Serial.begin(9600); // for testing purpose
  Serial1.begin(9600); //the incoming port. Data from the Mega
  Keyboard.begin(); // HUH! THE PROBLEM MAKER!
}

void loop() {
  static char incoming; // the character
  static byte counter = 0;
  if (Serial1.available() > 0) { //more than 0 bytes in the serial buffer
    incoming = Serial1.read(); // reading in
    counter++;
    Serial.write(incoming); // the testing port to be monitored.
    Keyboard.write(incoming); //writing out as USB keyboard
  }
  if (counter > 119) {
    delay(10);
    counter = 0;
  }
}

Check if data is available on Serial but read Serial1.

Please use code tags when posting code.

1 Like

Hi Kolaha!

Thank you for your idea! I will try it.
I would like to note that if I am monitoring the incoming bytes thru Serial they are arriving correctly.

So you think that the Keyboard.h library is needs some time to perform its action?
Lets try!

I will tell my results with this programming trick....

Bye! Gábor

You are right! I did a mistake ! In the real code the availability test is about Serial1

Thank you for remark!

Hi Bob!

I tried to slow down the Mega's transmission exactly as you recommended. In my experiments I added extreme delays- ONE SECOND between bytes. But it caused only slower crazyness- in other words, the letters appearing in the PC formed the SAME letter salad as before- but arrived by 1 second each.

Gábor

The idea of adding a delay() was to see if it helped, which it didn't, so the speed of transmission is not the problem

Post your real code :wink:

Dear Bob!

I did also tried the Keyboard.print( ) with a VERY LARGE constant string.
It was more than 120 characters between double quotes.It worked well!

Based on this result I did also tried the following algorhytm:
(Code example)

char incoming;
 String text;

 void loop()
{
incoming=Serial1.read;
text=text+incoming;
Keyboard.print(text);
}

My result? The same madness...... I cannot to understand

Huh!

I am now not at home but remember what I wrote:

.....


if (Serial1.available()>0) // are characters in the serial buffer?
{
......statement....
....statement.....
}

Guys!

Some thoughts:

Is any alternative of keyboard.h?
How could I access the developer of the original library?
Or to obtain its source code?
Maybe my problem is a bug of this library?

The source code is on your PC

Hi Bob!

Excellent advice! I will see the source code of the library as you recommended!

UKHeliBob via Arduino Forum <notifications@arduino.discoursemail.com> ezt írta (időpont: 2023. júl. 3., Hét 18:25):

What kind of text is in the file? The Keyboard library can only handle ASCII characters (0-127), anything beyond that would be translated to raw key codes.

Hi Guys! We found and solved the issue!

The cause is buffer overrun. The transmitter is quicker as receiver could process bytes. When I inserted 3 ms delay in transmitter side everything became correct!

Gábor

oqibidipo via Arduino Forum <notifications@arduino.discoursemail.com> ezt írta (időpont: 2023. júl. 4., Ke 10:56):

As suggested in reply #5

THANK YOU FOR EVERYBODY FOR THE HELP AND IDEAS!

Gábor