I've made a small interface using the asio C++ library to open serial ports and do I/O from/to them. It works with my Arduino Uno and other hardware as well (industrial weighing machines), but I only specify the port and baud rate.

The following code is run in the client application, which runs both on my Windows 10 Student Edition x86_64 machine, and on old 32 bit Win XP machines that communicate with the weighing machines:

/*this is inside a constructor*/ 
/*iosvc gets passed by reference, port_ is a member variable of type asio::serial_port,
 npuerto is the name of the port (e.g. "COM3") */

port_(iosvc_, npuerto),



The Arduino is programmed to do a serial echo of whatever is sent to it, for testing purposes. This is the echo code:

void setup() {

void loop() 

It works fine with my Arduino until I unplug it and plug it back in. After replugging it all the values returned are garbage, specifically some sort of '?' character inside a black box, don't know the bit value though, but it's constant. The problem gets fixed when I open an arduino IDE Serial Port monitor. If I close it, and once again use my own software to open up the serial port, all the values are fine.

From this I concluded that the Arduino IDE Serial Port monitor is doing something when it opens up the port that I'm not doing.

Any clues? Flow control? Handshake? Thanks

  • 2
    Which "Arduino" are you using? For example, is it an Uno, with a separate USB-Serial interface between the USB port and the serial interface on the MCU, or is it something like a Leonardo, with on-board USB support on the microcontroller? The "unplug it and plug it back in" behaviour is rather different between the two.
    – cjs
    Apr 3 '17 at 15:14
  • It is an UNO, and the garbage values are the exact number of characters that I send it, e.g. if I send "Hello", I'll get "?????" where the '?' character is the same I described above. Like a black box with a '?' inside.
    – Turambar
    Apr 3 '17 at 15:20
  • 1
    If it's the exact same number of characters, that leads me to guess that the bps rate is properly matched and you have some problem with corruption of the data. I suspect that the '?' in a black box is not actually a particular value but an "I don't want to interpret this" value. Anyway, can you update the question to have a copy of your "echo" program code, mention the host PC platform (Win, Linux, etc.) you're using, and what program (that's not the monitor) you're using to view the serial port traffic?
    – cjs
    Apr 3 '17 at 15:35
  • What bugs me is that the Arduino Serial Monitor does something which "fixes" the serial port. As if it aligned some values or something. My custom program will work fine after using the Monitor at least once if the Arduino gets reset. It works fine with the weighing machines, which are in a textile factory, so I guess the code is OK. Those machines are connected through an old fashioned serial port connector though.
    – Turambar
    Apr 3 '17 at 16:37
  • Could it be related to handshake protocol? I don't know much about it.
    – Turambar
    Apr 3 '17 at 17:19

Can you include all the other calls you make related to setting up the serial port along with asio::serial_port_base::baud_rate(baudios)? Ideally, split that out into a separate function (that takes parameters such as baud rate) that you know is the only thing doing this setup.

As I mentioned in the comment, getting back the right number of chararacters but incorrect characters looks as if the baud rate is correct but something else is wrong. The first thing that comes to mind is the bits/parity/stop bits settings. Typically you want to be using 8 bits, no parity, often referred to as "8N1".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.