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

Serial communication not working unless Serial monitor is opened/closed

Post by TurboTimmy »

I thought i would create a new thread for this issue to help keep things seperate, and maybe it will come in useful for anyone else having the same issue.

I am having a huge problem getting my STM32F401CCU6 to work properly with Serial Read/write. I am also having the same problem with a Pro Micro.

I am trying to send a serial command to software on my PC, listen for the answer and then display the results on a tft display.

The problem is that when I connect my board to my PC via the on board USB port, I cannot get the code to run (display results on display) unless I open and immidiately close the Serial monitor. If using Serial1 via a UART adaptor, I dont have any problems. Interestingly I also dont have any problems if I use an Arduino Uno, or a Arduino Nano.
I have spent a number of days now trying to find a solution. The closest thing I could find was a suggestion of putting a 10uf capacitor between RST and GND. I tried this, but it did not work. There is also some suggestion that it has something to do with a reset when Serial monitor is opened. Has anyone else experienced this? Is there a fix?

I would like to use my STM32 board if possible. I have found that things run alot faster on these boards compared to Arduino. The final project will have alot more added to it, so the extra speed/power will really come in handy.

Thanks in advance

Tim
Last edited by TurboTimmy on Sun Jan 02, 2022 7:43 pm, edited 1 time in total.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

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

Post by ag123 »

are you using usb serial?
and are you using linux?
if you are you may need to update your udev rules with the flag

Code: Select all

ENV{ID_MM_DEVICE_IGNORE}="1"
https://github.com/stm32duino/wiki/wiki ... itor-fails
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: Mon Dec 27, 2021 8:29 am are you using usb serial?
and are you using linux?
if you are you may need to update your udev rules with the flag

Code: Select all

ENV{ID_MM_DEVICE_IGNORE}="1"
https://github.com/stm32duino/wiki/wiki ... itor-fails
Thanks for the reply. I am using USB serial (on board USB port), and I'm using Windows.

It is really strange that when I use Serial1 and a UART adaptor there is no problem at all. This is not ideal though as I eventually want to add Keyboard.press later in the sketch, so it needs to all be done via the boards USB port (unless Keyboard.h can be made to send to Serial1).
ag123
Posts: 1655
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'm not sure if windows is similarly affected, I'd let others respond on that.
It seemed, however, that USB (CDC) serial normally works quite well, on both Windows and Linux after things like that udev problem is fixed.
In Linux, i've done tests and measured throughput closer to 1 Mbps (exceeding it) USB-CDC Serial with all that multiplexing with a mouse and keyboard, internal hubs connected.
There are some known issues with usb serial for bulk data
https://github.com/stm32duino/Arduino_C ... ssues/1399
but i'd doubt you are affected in the current case.

Among the things, check if other intervening codes may be a cause, e.g. comment them and test again. Some codes may 'hang' by doing a spinlock waiting for IO to be done. e.g. codes like

Code: Select all

while(digitalRead(pin) != HIGH);
would just 'hang' until you bring that pin high, they tend to matter for some cases where the app waits for an 'interrupt' (i.e. pin to change state)

I'm not too sure if there could be 'faulty' stm32f401, the solder connections and rather frequently faulty usb cables.
try changing usb cables, some have resolve it simply doing that.

try terminal apps like putty
https://www.putty.org/
I'm using putty rather than that bundled in the Arduino IDE.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

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

Post by ag123 »

there is another tactic which i often use

Code: Select all

void setup() {
	Serial.begin();
	while(!Serial);
	//or
	while(!Serial.available());
}
i.e. spinlock usb serial till something connects or that a key is pressed.
this may help avoid some problems related to buffers and bulk transfers.

for keyboard, usb-hid i'd suggest not to use it. Serial is normally simpler and you can wrap your own protocol around it.
with usb-hid, it is normally necessary to be rather well versed with usb and its protocols, they won't be easy to troubleshoot if things are coded incorrectly.

usb serial is '8 bit clean', i.e. whatever raw data you pass say using Serial.write() or Serial.print() is passed as-is to the host.
I've the adc send data back to the host that way as a stream of raw binary 16 bits words.
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: Mon Dec 27, 2021 12:29 pm there is another tactic which i often use

Code: Select all

void setup() {
	Serial.begin();
	while(!Serial);
	//or
	while(!Serial.available());
}
i.e. spinlock usb serial till something connects or that a key is pressed.
this may help avoid some problems related to buffers and bulk transfers.

for keyboard, usb-hid i'd suggest not to use it. Serial is normally simpler and you can wrap your own protocol around it.
with usb-hid, it is normally necessary to be rather well versed with usb and its protocols, they won't be easy to troubleshoot if things are coded incorrectly.

usb serial is '8 bit clean', i.e. whatever raw data you pass say using Serial.write() or Serial.print() is passed as-is to the host.
I've the adc send data back to the host that way as a stream of raw binary 16 bits words.
Thank you for the pointers. I will go through it and see what I can find.

I have already tried:

Serial.begin();
while(!Serial);
//or
while(!Serial.available());

These did nothing to help the problem. I have a feeling that it might be something to do with one of the config files that determines how the USP port opperates. Messing around with those kind of files is massively above my ability, unless someone is able post instructions like "change line XXX to YYY".

I find it strange that if I use Serial1 (and connect to PC via UART adaptor), I dont have any problems at all. The code runs perfectly and displays the results on the display right away as soon as I start the software on my PC. When using "Serial" I have to open Serial monitor and then close it right away fro it to then work.

You have given me lots to look into. Although this issue is causing problems, I am very much enjoying learning new things from it.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

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

Post by ag123 »

you can try calling

Code: Select all

Serial.dtr(false);
in setup();
may be it helps
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 »

I have no problems with USB Serial but you need to initiate it properly (open COM port) in your program on PC, otherwise it won't work.
And in your case it looks like you haven't done it properly, otherwise you wouldn't been able to open serial monitor.
COM ports cannot be shared, so only one process/program can access it.
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: Mon Dec 27, 2021 6:06 pm you can try calling

Code: Select all

Serial.dtr(false);
in setup();
may be it helps

This absolutely did the trick. Works right away. Thank you.

Could I be really cheeky and ask if you could explain how/why that worked?
GonzoG wrote: Mon Dec 27, 2021 7:59 pm I have no problems with USB Serial but you need to initiate it properly (open COM port) in your program on PC, otherwise it won't work.
And in your case it looks like you haven't done it properly, otherwise you wouldn't been able to open serial monitor.
COM ports cannot be shared, so only one process/program can access it.
Thank you so much for your help with this. Just to answer your last post, the "open COM port" was not the problem. If I had the software connected then I couldn't open Serial monitor. Or If I had Serial monitor open, I couldn't connect the software. Perhaps I didn't explain very well.
ag123
Posts: 1655
Joined: Thu Dec 19, 2019 5:30 am
Answers: 24

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

Post by ag123 »

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
Post Reply

Return to “General discussion”