Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arduino can't read Serial properly

Alright, I've googled getting a string from Serial with Arduino and I've had no luck even copy and pasting examples.

I'm trying to get a string from the Serial. Here's my code:

void setup() {
    Serial.begin(9600);
    Serial.write("Power On");
}

void loop()
{
    while(!Serial.available());

    while (Serial.available() > 0) {
      Serial.write(Serial.read());
    }
    Serial.println();
}

And it's printing out character by character.

I also tried

char* read(int len) {
  while (!Serial.available());
  char str[len];
  int i = 0;
  while (i < len) {
    str[i] = '\0';
    int inByte = Serial.read();
    Serial.println(inByte);
    if (inByte == -1) {
        return str;
    } else {
      str[i++] = inByte;
    }
  }
  return str;
}

And it returns 1 character at a time (serial.print(inByte) gives -1 every other time). Why is the Serial splitting each character?

If I enter 'hello' and I call serial.read() it gives a character then says there's nothing, then gives another character and says there's nothing.

like image 701
Raekye Avatar asked Mar 28 '12 04:03

Raekye


People also ask

Why is my serial port not working Arduino?

The most common reason for the board not being displayed on a port are a failing USB connection. The board needs to be connected to your computer with a data USB cable. Make sure the USB cable is not damaged. Test your cable with a different device, or try using a different cable.

How do I reset my Arduino serial monitor?

the only way to get it back was to unplug and re-plug in the usb port, which resets the arduino and restarts the whole firing cycle from the beginning. You can generally disable the auto-reset on the UNO by connecting a 10 uF electrolytic capacitor between Reset and Ground (+ side to Reset, - side to Ground).

Why is my serial monitor Arduino blank?

You need to configure Serial Monitor to communicate at the same baud rate, otherwise you'll get gibberish or nothing at all in Serial Monitor. There is a menu for this near the bottom right corner of Serial Monitor. Make sure you have “9600” selected from that menu.


2 Answers

I figured it out.

When you open a Serial with 9600 baud (Serial.begin(9600);), it's reading/writing at 9600 bytes per second. That means at fastest it can get just under 10 bytes per millisecond. I don't know what the operating speed is, but it seems like the Arduino gets alerted of and reads the first byte before the second one arrives. So, you must add a delay(1) to "wait" for another byte in the "same stream" to arrive.

String read() {
    while (!Serial.available()); //wait for user input
    //there is something in the buffer now
    String str = "";
    while (Serial.available()) {
        str += (char) Serial.read();
        delay(1); //wait for the next byte, if after this nothing has arrived it means the text was not part of the same stream entered by the user
    }
    return str;
}

You may ask, well since you're delaying how do you know if the user is just typing very fast? You can't avoid it here, since the Serial is essentially limited at a certain speed. However, the user must be typing virtually-impossibly-fast for two inputs to be confused as one.

like image 141
Raekye Avatar answered Oct 06 '22 01:10

Raekye


I don't have access to the Arduino source files here, but the following line of code won't give you a full String for obvious reasons (let me know if it's not that obvious):

int inByte = Serial.read();

Also, using

Serial.write()

you'll be sending byte per byte. That's the oposite from

Serial.println()

in which you'll be sending full sentences.

I would try working with Serial.print() or println() rather then Serial.write().

You can check out the references:

http://arduino.cc/en/Serial/Write

http://arduino.cc/en/Serial/Println

like image 27
Cassio Avatar answered Oct 06 '22 00:10

Cassio