1

I have this code.

String incoming;   // for incoming serial data

void setup() {
    Serial.begin(9600);  // opens serial port, sets data rate to 9600 bps
}

void loop() {

    // send data only when you receive data:
    if (Serial.available() > 0) {
            // read the incoming byte:
            incoming = Serial.read();
            String mysrr = String(incoming);
            // say what you got:
            Serial.print("I received: ");
            Serial.println(mysrr);
    }
}

A few of you will recognise this as an Arduino example.

However, when I run it ( I sent it the message "hi"), I get this in the serial:

I received: 104
I received: 105

Why is this happening?

0

Serial.read() returns an int not a String. https://www.arduino.cc/en/Serial/Read

Use an int for incoming (as the example). Or even better use a char and skip all the strings.

Cheers!

Your Answer

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