Is there anyway to send manually typing messages to arduino through serial port in platformio?

For the illustration, the test code is:
3

In arduino-ide, I typing “arduino” in the text box and click “send” button, then the result is:
1

But in platformio-ide, I typing “arduino” in Serial Monitor, the result is:
5

Is there anyway to solve this problem in platformio-ide?
Best regards.

The Arduino serial monitor works differently to any of the utilities you can use with PlatfomIO. With it you can type a line of text, none of which is sent to the Arduino board until you click the send button.

With screen, miniterm or VSCode’s terminal, you are typing directly at the Arduino’s USART so your while loop is finding one character each time around.

Fixes?

  • Type faster! :grinning:
  • Type into an editor, copy the required text, paste into the terminal window - that sometimes works for shorter texts.
  • Use a terminal that can do line mode. But I’ve not found one yet!
  • Use the Arduino IDE and Serial Monitor.

Open the Arduino IDE and configure the port to suit your board. Then, once you have uploaded with PlatformIO you can open the Arduino serial monitor, set the baud rate and start typing.

You may need to add some of these parameters to platforio.ini to make it work, definitely monitor_port and maybe monitor_speed:
https://docs.platformio.org/en/latest/projectconf/section_env_monitor.html

You must close the serial monitor when you need to recompile and upload your code.

I have searched for ways to put the various terminal utilities I have used into line mode, but failed. :frowning_face: so far. Hence, when I need to talk to my boards, I use the Arduino IDE’s serial monitor.

HTH

Cheers,
Norm.

3 Likes

Depending on your OS of choice, you might want to look at Termite (Windows) or CoolTerm (cross-platform). I’ve used Termite before when I’ve wanted a simple send-buffer type terminal, but I just realised that CoolTerm does has a Line Mode option (under Terminal options), so is probably the better option due it’s cross-platform nature.

3 Likes

Nice on @pfeerick, I’ll check that out.

Cheers,
Norm.

1 Like

Many, many, many thanks indeed @pfeerick for the hint about CoolTerm. What an absolutely brilliant utility that is for talking to an AVR running PlatformIO code. I’ve just downloaded it and tested it with my own USART interupt driven replacement for the Arduino “Serial” class and it works perfectly.

Thanks!

Cheers,
Norm.

1 Like

Great to hear you like it. It is certainly a really handy little tool :slight_smile:

Thank you for giving me the detailled reason, @normandunbar

Thank you, @pfeerick

1 Like

Hi, @pfeerick
Is there any detailed method to use CoolTerm for talking to arduino when running platformio code?
I searched these days, but failed.
Best regards.

Not that I know of. There isn’t anything special about it anyway. Load up CoolTerm, hit the Options toolbar button, pick your serial port, baud rate. If you need it to act like the Arduion IDE, and send after pressing enter rather than as you type, goto the ‘Terminal’ page of the options, and set it to ‘Line mode’, and change the Enter key emulation if needed (CR+LF, LF, etc). Then connect or disconnect as needed (i.e. connect after uploading, disconnect before doing a new upload).

It is probably possible to integrate it into PlatformIO somewhat via extra-scripting, but that would probably be a bit clunky. Assuming it is possible.

Thank you for giving me the detailed illustration, @pfeerick
By the way, I find that there is “send_on_enter” in Serial Monitor of platformio ide:


Does it is what we want?

2 Likes

Nice spot.

Documentation says

Send a text to device on ENTER

So it’s worth a try. See what happens when you add monitor_filters = send_on_enter to your platformio.ini, and restart the serial monitor. :wink:

1 Like

Hi, @pfeerick
It works very well.
By the way, I find that if the arduino code contains ‘\n’ as the terminate character, for example:


then the “stringComplete = true;” in “else if” can’t be reached.
For finding the reason, I set “monitor_filters = debug, send_on_enter”, then I find:
4
It seem that the platformio receives ‘\r\n’ (not ‘\n’) when I hit the “Enter” key in my keyboard.
But in my textbook, it says that ‘\n’ is Enter key, I’m very puzzled.
Best regards.

Yes, \n is the traditional newline/line feed character. However, Windows considers it to be a carriage return (\r) + line feed (\n) for the end of line. Hence CR+LF, instead of just LF. on Windows, and just LF on Linux/unix/MacOS. Also, C/C++/Perl expects LF, but Python/Java expects CR+LF. Not confusing at all. :face_vomiting:

Since it’s the default for python (which PlatformIO is written in), I expect it will be the same everywhere, but can be changed in the source code. But this is the reason you have to option to change it in the Arduino IDE serial monitor.

Hi, @pfeerick
I find that there is an option in platformio device monitor:
1
It maybe the method I searched, but how to set “–eol = LF” in platformio.ini or somewhere else?
Best regards.

I used this to get it to work in a terminal session outside of VSCodium:

pio device monitor --eol=CRLF --echo --filter send_on_enter

I needed the --echo as I suspect that with the filter to send_on_enter, it no longer echoes. Anyway, I now know that that works on the command line, adding the folloiwng to my platformio.ini file also now works:

monitor_flags = 
    --echo 
    --eol 
    CRLF

As per the docs at Redirecting..., the flags and their options must each be supplied on a new line. What it doesn’t say, and it will not work, unless you start each line with a tab. (Or spaces?) I get the impression that this needs to be in Python format - where indents are required.

The above works in VSCodium (1.34.0) on Linux Mint 19.3 64 bit.

HTH

Cheers,
Norm.

1 Like

Yup, python format. And it can be spaces or tab - just needs to be indented.

1 Like

Hi, @normandunbar , thanks a lot.
The “monitor_flags” format also works in vscode+platformio (on win10 64 bit), according to my arduino code above, I add the following to my platformio.ini file:

monitor_flags =
    --filter
    debug
    --filter
    send_on_enter
    --echo
    --eol
    LF

By the way, I set --eol to be LF, so the “Enter” key equals ‘\n’:
6
but in my arduino code above, the

else if ( inChar == ‘\n’ )

in “void serialEvent()” can not be reached. I don’t know why. Can you help me?
Best regards.

I would add a Serial.println(inChar, HEX); debug line just after you read a character into inChar to see if the \n is actually being passed over to your code. I have a feeling, but don’t quote me, that the send-on-enter filter might be filtering off the \n and it’s not reaching your code.

You would see a or 0a if there is a \n coming through.

HTH

Cheers,
Norm.

1 Like

Thank you very much, @normandunbar
You are right, the ‘\n’ was filtered off, so ‘\n’ can not be received by arduino.
7
Best regards.

1 Like