Sending Keyboard.h to Serial1

Post here first, or if you can't find a relevant section!
Post Reply
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Sending Keyboard.h to Serial1

Post by TurboTimmy »

Hello All, yes it's me again.

I am working on a small project that requires some Keyboard.press functions. The only thing is, I need them to be sent via Serial1 instead of the default. Strange, but this is the only way I have found that my over all project will work (issue with serial read/write not working until serial monitor has been opened and instantly closed).

Anyway, I have successfully managed to get Keyboard.press to work with the on board USB port, but I need to be able to send it to Serial1 instead. Here is a the basic code that I am working with (extract from the overall sketch);

Code: Select all


#include <Keyboard.h>

#define Button PB2

void setup() { 

pinMode(Button, INPUT_PULLUP);

Serial1.begin(115200);
Keyboard.begin();

}

void loop() { 

if (digitalRead(Button) == HIGH)
 { Keyboard.press('a'); Keyboard.releaseAll(); delay(150); }
 
 }

Yes I know that the button press could be coded alot better, but I wanted to keep things really simple for now.

The above works fine when using the default USB port on the board (STM32F401CCU6)

So, is it possible to get this code working with Serial1? If so, would someone be kind enough to talk me through it?

Thanks in advance
Tim
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Sending Keyboard.h to Serial1

Post by GonzoG »

Keyboard works only on USB as it's an USB device.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Sending Keyboard.h to Serial1

Post by TurboTimmy »

GonzoG wrote: Sun Dec 26, 2021 11:57 pm Keyboard works only on USB as it's an USB device.
Ah. I was kind of hoping that it could have been sent to Serial1 as thats the only way I can get things working. I have to connect via a UART adapter and send via Serial 1. So it looks like I may have to get the bigger issue sorted then.

The bigger issue:

I am running alot of Serial Read/write and displaying the results on a tft display. The problem I am facing is that it will only work once I have opened serial monitor and then immediately close it. Things then kick into life. This is not ideal though, I need it to work properly right away. I have been looking online for a couple of days now for a fix. The closest thing I could find was putting a 10uf capacitor between RST and GND (something to do with reset when serial monitor is opened). This didnt solve the problem though. I am also having the same problem when I use a Pro Micro.

If I use UART adaptor and Serial1, there is no problem at all and things work straight away.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Sending Keyboard.h to Serial1

Post by GonzoG »

If you need open Serial monitor at start, then program is written like this.
With USB you can check if connection is active (e.g. serial monitor opened and connected). It's often done in setup() and ppl often put there also a while loop to wait for serial monitor to connect:

Code: Select all

while( !Serial);
With UART/USART this isn't possible as those are much simpler interfaces and if you're connected to a PC with TTL<->USB converter, then UART is connected to a converter so you have no way to check if converter is connected to USB.

Also with stm32 core you cannot have keyboard and Serial on USB. You have to choose between CDC (Serial) or HID (keyboard).
That's not a problem with Arduino boards with native USB (Micro, Leonardo, Pro Micro) as those have both modes enabled.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Sending Keyboard.h to Serial1

Post by ag123 »

you can try calling

Code: Select all

Serial.dtr(false);
in setup();
maybe it helps

keyboard or rather HID is a usb protocol. since you have a hardware usb-uart dongle after all you may like to use usb for the keyboard and your serial dongle for Serial().

the more appropriate way is to make an app on the desktop that communicate over usb-serial to the board.
in that way you can use Serial() for usb serial.

the desktop app can be quite easily done say with c#
https://www.c-sharpcorner.com/UploadFil ... n-C-Sharp/
java, e.g. using processing
https://processing.org/
https://processing.org/reference/librar ... index.html
python
https://pyserial.readthedocs.io/en/late ... intro.html
etc

i made oscilloscopes and logic analyzers working just over usb-serial
an example is here
viewtopic.php?f=10&t=116
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Sending Keyboard.h to Serial1

Post by TurboTimmy »

ag123 wrote: Mon Dec 27, 2021 6:06 pm you can try calling

Code: Select all

Serial.dtr(false);
in setup();
maybe it helps

keyboard or rather HID is a usb protocol. since you have a hardware usb-uart dongle after all you may like to use usb for the keyboard and your serial dongle for Serial().

the more appropriate way is to make an app on the desktop that communicate over usb-serial to the board.
in that way you can use Serial() for usb serial.

the desktop app can be quite easily done say with c#
https://www.c-sharpcorner.com/UploadFil ... n-C-Sharp/
java, e.g. using processing
https://processing.org/
https://processing.org/reference/librar ... index.html
python
https://pyserial.readthedocs.io/en/late ... intro.html
etc

i made oscilloscopes and logic analyzers working just over usb-serial
an example is here
viewtopic.php?f=10&t=116
I have never really had the confidence to create any kind of Desktop app/software. Just the thought of it is very daunting seeing as I am still very much a novice to all of this. The link that you have provided are very useful indeed. Once this STM32 project is finished, I will most certanly be making a desktop app the next project :D
Post Reply

Return to “General discussion”