Message clearing while using the IDE serial monitor

Hi there,
I'm working on a project where I'm using an Arduino Mega to send ASCII commands to 2 syringe pump and valve units.

I've been writing a code that communicates with the pumps in two ways.

  1. by writing a program in the loop, and
  2. by sending commands manually from the serial monitor.

Ive been successful in writing a code to dispatch the message in the serial monitor to the proper pump/valve unit but the issue I'm having trouble with is that I can't seem to clear the last message from the serial monitor. i've tried

to explain it better ill give some examples:
(I have it in the code to echo the message for troubleshooting)

  • If I were to type /1 in the message box and send it by pressing enter i see /1 in the serial monitor, (this is okay)

  • then if I type 3444 in the message box and send it by pressing enter i see 3444 in the serial monitor, (this is okay)

  • next if I type /2 in the message box and send it by pressing enter I see /244 in the serial monitor, (this is not okay)

  • furthermore if i just type enter in the message box it resends the /2 command and I see /244 in the serial monitor. (this is not okay)

the only way to clear this is by resetting the Arduino and this isn't an option for the final implementation. I would be more than grateful if anyone whos experienced something like this before could share their insight or a solution.

I have been watching tutorials and tried changing the code in many ways including adding serial.flush()
serial.begin() again
serial.print(serial.read())

the only thing I tried that may work but wasn't successful in implementing was creating another function called serial.flushRX() in the hardware serial library (described on the sparkfun website forum) https://forum.sparkfun.com/viewtopic.php?t=32715

if its not a common problem i can upload the code if someone thinks it will help. (though its pretty clunky at the moment - im on revision 14 of this version trying to figure this out)

thanks for reading

That sound like you are saving the received text somewhere, then not clearing it before receiving the next characters from Serial. It does not sound like anything related to Serial itself.

1 Like

The Serial Monitor is about 25% of what a decent serial terminal should be, and is not really suitable if your distributing the IDE just get a terminal.

Assuming you’re entering each value on a srparate line, @david_2018’s suggestion is quite likely, or if you’re not handling and sending ‘platen & carriage control, codes, (FF BS CR VT LF etc), you could be overtyping the previous text, which leaves that preceding text on the ‘terminal’.

Once you know what’s happening, a real terminal program may help you see better results.

Seeing your code might help us understand how things are happening.

Ah, thank you both for your help.
I was saving the characters in an array called "input_cmd"
I just needed to reset the array values with a few simple lines after dispatching them. Though I assume there is a more elegant solution out there, this is functional.

input_cmd[0] = {};
input_cmd[1] = {};
input_cmd[2] = {};
input_cmd[3] = {};
...and so on

thanks!

  • you could do a memset() to zero out the buffer
  • you may just need to terminate a c-string by setting the first char to '\0'
  • Serial.readBytesUntil()

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.