What characters are sent to Arduino via the serial port from Monitor?

Zápisník experimentátora

For the beginner, it is not easy to understand that, in addition to the visible characters, the invisible characters are also found in the ASCII table. With Serial Port Monitor, we can easily communicate with the Arduino. This program shows what you're sending to the Arduino. Each character sent is displayed along with its hexadecimal value. I have visualized each character so that each value is best seen. The special invisible characters \r and \n are displayed as they are written to the source code.

The sketch

In the Serial Port Monitor, try different line breaks and watch output from the program.

int received;
char buffer[20];

void setup() {
  Serial.begin(9600);
}

void loop() {
  if (Serial.available() > 0) {
    received = Serial.read();
    switch (received) {
      case '\r':
        sprintf(buffer, "\\r [%02x] ", received);
        Serial.print(buffer);
        break;
      case '\n':
        sprintf(buffer, "\\n [%02x]", received);
        Serial.print(buffer);
        Serial.println();
        break;
      default:
        Serial.write(received);
        sprintf(buffer, " [%02x] ", received);
        Serial.print(buffer);
        break;
    }
  }
}

Source code

The source code is located on the GitHub server.


05.03.2018


Menu