about

about

Technology

Total Pageviews

Popular Posts

Cloud Labels

Labels

Search This Blog

Cloud Labels

Featured Listings

3agv2004.info main website

Simple Serial Communication

Share it:

Simple serial Communication 

There are two important functions related to the serial input in the code hereinafter, and that's Serial.available() and Serial.read().
  • Serial.available() returns the number of characters (i.e. bytes of data) which have arrived in the serial buffer and that are ready to be read.
  • Serial.read() returns the first (oldest) character in the buffer and removes that byte of data from the buffer.
So when all the bytes of data are read and no new serial data have arrived, the buffer is empty and Serial.available() will return 0. If we send more than one character over serial with this code, the output will look like this:
Let us begin. The following code will make the Arduino ECHO anything you send to it. Therefore, if you type a 3, the Arduino will send back a 3. If you type a letter F, the Arduino will send back a letter F.

Enter the following code into your Arduino IDE and upload it to your Arduino.
/* Simple Serial ECHO script */

/* Use a variable called byteRead to temporarily store
   the data coming from the computer */
byte byteRead;

void setup() {                
// Turn the Serial Protocol ON
  Serial.begin(9600);
}

void loop() {
   /*  check if data has been sent from the computer: */
  if (Serial.available()) {
    /* read the most recent byte */
    byteRead = Serial.read();
    /*ECHO the value that was read, back to the serial port. */
    Serial.write(byteRead);
  }
}
  1. Once the Arduino sketch has been uploaded to the Arduino. Open the Serial monitor, which looks like a magnifying glass at the top right section of the Arduino IDE. Please note, that you need to keep the USB connected to the Arduino during this process, as the USB cable is your communication link between your computer and the Arduino.
  2. Type anything into the top box of the Serial Monitor and press on your keyboard. This will send a series of bytes to the Arduino. The Arduino will respond by sending back your typed message in the larger textbox.
But what if you want to send more than one character in handle it in a sensible way? No problem!
Here we’ve introduced the readStringUntil() function, which makes it possible to combine all the characters in the sent message into a single Arduino string. In this case we’re waiting for the \n character, which is the newline character that comes at the end of a string sent in the Arduino serial monitor.
String my_name;
 
void setup() {
    Serial.begin(9600);
 
    delay(2000);
 
    Serial.println("What is your name?");
}
 
 
 
void loop() {
    if(Serial.available()){
        my_name = Serial.readStringUntil('\n');
 
        Serial.println("Nice to meet you, " + my_name + "!");
    }
}

Sending Commands.

A more usable scenario could be to send commands to the Arduino. Depending on what you send, the Arduino will perform different task. Here’s a somewhat abstract example on how to do this:
String command;
 
void setup() {
    Serial.begin(9600); 
}
 
void loop() {
    if(Serial.available()){
        command = Serial.readStringUntil('\n');
         
        if(command.equals("init")){
            initialize();
        }
        else if(command.equals("send")){
            send_message();
        }
        else if(command.equals("data")){
            get_data();
        }
        else if(command.equals("reboot")){
            reboot();
        }
        else{
            Serial.println("Invalid command");
        }
    }
}
Notice the use of the equals() function. This is a function in the Arduino String class which returns true if the string in question is equal to the parameter string.
Share it:

Arduino

Post A Comment:

0 comments: