Serial communication not working unless Serial monitor is opened/closed

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

Re: Serial communication not working unless Serial monitor is opened/closed

Post by TurboTimmy »

ag123 wrote: Tue Dec 28, 2021 12:28 pm Arduino IDE and various serial terminal configures usb-serial with with DTR flag set. This is the normal way in 'old' rs232 serial hardware.
This probably includes all those usb-uart dongles.
These days not every serial terminal app turns on DTR
Is there a way of getting this to work when using the HID option? I have the code working and displaying the results right away. Thats great. I now want to add the Keyboard.h library so that I can send keystrokes. I have tried using this while in "CDC(generic 'Serial' supersede U(S)ART) mode, but the resulting error says that I need to use the HID option. The other error I get is:

Code: Select all

STM32_HDSDR_Read_FrequencyMode_TFT_2020_12_21:92:8: error: 'class HardwareSerial' has no member named 'dtr'
   92 | Serial.dtr(false);


So when I download the sketch with HID chosen and Serial.dtr(false) commented out, the Keyboard side of it works just fine, but I am unable to connect the software that I am using because I am unable to see the com port (it doesn't show up anywhere, not even in Windows device manager). Any thoughts?
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Serial communication not working unless Serial monitor is opened/closed

Post by GonzoG »

No. For serial communication you need CDC mode set and for keyboard you need HID. You cannot have both.
So you'll need to use UART and TTL/USB converter if you want keyboard functionality.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Serial communication not working unless Serial monitor is opened/closed

Post by TurboTimmy »

Ok, So I have been playing around with things, and am fairly happy with the results, but it's not quite there yet.

The Serial.dtr(false); absolutely nailed it and really opened things up. From this, I was able to work out what I needed to do to get my Arduino Pro Micro working as it should (I commented out 2 lines from CDC.cpp file). On the Pro Micro, I now am able to use it as an HID device as well as sending/receiving serial data all via the built in USB port. This is exactly what I need to do with the STM32 board that I am using (Pro Micro is too slow).

So, is it at all possible to edit some of the core files to make this happen? It would appear that I need to set the board to HID so that I can make use of the Keyboard.h library, so what core file to I need to edit? What do I need to look for within that file?

When the board is in HID mode, I am unable to see a com port number for it anywhere. This is also causing an issue.

This is so close to being an absolute perfect fit for my project, I really don't want to give up now
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Serial communication not working unless Serial monitor is opened/closed

Post by ag123 »

i'd not suggest messing further with keyboard i.e. HID. the way to do it is to write a composite usb device driver that does both HID and CDC.
you would need to do that yourself.
For now, the delivered drivers is either CDC or HID, but not both.
So you would either have to make do with HID or CDC (i.e. usb serial) choose one.
If you need both, the easier way for now is to use your usb-uart dongle for serial.
note also that HID is (much more) complicated than just a keyboard. Even for that matter if you just take a keyboard, there are things that you probably didn't think of implementing like the leds, numlock, caps lock, multi-key alt, ctrl, shift etc. Those things are necessary for a real keyboard implementation.

in practical terms, HID can 'send data' to the host just like serial, and your are not using any of the 'standard' keyboard function, and you need a completely custom HID driver. That would be more complicated on both the host app as well as device than say a simple usb-serial (CDC) app.
Last edited by ag123 on Wed Dec 29, 2021 5:26 pm, edited 1 time in total.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Serial communication not working unless Serial monitor is opened/closed

Post by TurboTimmy »

ag123 wrote: Wed Dec 29, 2021 5:17 pm i'd not suggest messing further with keyboard i.e. HID. the way to do it is to write a composite usb device driver that does both HID and CDC.
you would need to do that yourself.
For now, the delivered drivers is either CDC or HID, but not both.
So you would either have to make do with HID or CDC (i.e. usb serial) choose one.
If you need both, the easier way for now is to use your usb-uart dongle for serial.
I do need both, and agree that using the dongle would be the way forward. However, when I try and use the Keyboard.h library in CDC(generic 'Serial' supersede U(S)ART) mode, I get an error telling me that I need to use HID mode.

So how would I get the Keyboard library to work in another mode?

I can can use Serial1 to send/receive data via dongle, but to use Keyboard, it would involve sending the commands via Serial1 as well which aparently I am unable to do
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Serial communication not working unless Serial monitor is opened/closed

Post by ag123 »

don't select CDC, just select Serial UART.
GonzoG
Posts: 403
Joined: Wed Jan 15, 2020 11:30 am
Answers: 27
Location: Prudnik, Poland

Re: Serial communication not working unless Serial monitor is opened/closed

Post by GonzoG »

TurboTimmy wrote: Wed Dec 29, 2021 4:41 pm So, is it at all possible to edit some of the core files to make this happen? It would appear that I need to set the board to HID so that I can make use of the Keyboard.h library, so what core file to I need to edit? What do I need to look for within that file?
It is possible, but it's much more complicated then editing few files.
If it was simple it would have been done.
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Serial communication not working unless Serial monitor is opened/closed

Post by TurboTimmy »

ag123 wrote: Wed Dec 29, 2021 5:28 pm don't select CDC, just select Serial UART.
Image

These are the options I have available. Am I looking in the right place?
ag123
Posts: 1656
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

Re: Serial communication not working unless Serial monitor is opened/closed

Post by ag123 »

select HID for your keyboard
your UART is already there, you would need your usb-uart dongle for that
TurboTimmy
Posts: 22
Joined: Wed Dec 22, 2021 9:43 pm

Re: Serial communication not working unless Serial monitor is opened/closed

Post by TurboTimmy »

ag123 wrote: Wed Dec 29, 2021 5:43 pm select HID for your keyboard
Ok, so that would mean using 2 USB cables wouldn't it? Or could I use dongle to do both the HID part, and the serial? I'll give that a go and see what happens :D
Post Reply

Return to “General discussion”