Lesson 22--Microduino "Serial port debugging"

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

Objective

In previous experiment, serial port is used to monitor data that means program write data to serial. This lesson will show how to read data the serial port received.

Equipment

Microduino-Core Microduino-FT232R

  • Other hardware equipment
    • Breadboard Jumper one box
    • Breadboard one piece
    • LED one
    • 220Ω resistor one
    • USB Data cable one

Experiment 1

  • Serial receives character string

Only need Microduino core and Microduino Ft232RL moduile, no other module. Program:

String comdata = "";//Define a String variable
void setup()
{
 Serial.begin(115200);//Set the baud rate
}
void loop()
{
    while (Serial.available() > 0)  // If there is serial buffer data, continuous cycle
    {        
comdata += char(Serial.read());   //Read data, only read one byte every time
     delay(2);//serial buffer time
    }
    if (comdata.length() > 0)
    {
Serial.println(comdata);  //Print serial data   
   comdata = "";//Clean the data
    }
}

Result: In serial monitor box, fill in any data in send window, then click send(or press enter),at the receive data window will display the data.

Lesson22-send.jpg
Lesson22-receive.jpg

Note:

  • String type variable converts the characters into the string simply and can directly output a string or assignment.
  • while (Serial.available() > 0): "While" can't change to "if", otherwise only can read one character.
  • delay(2): Can't delete this, otherwise the serial buffer doesn't have enough time to receive new data.
  • comdata = "": Can't delete this, otherwise serial port data will be added continuously.
  • comdata defines the character string, that is array, you can use comdata[0], comdata[1]...comdata[n] to quote every data.

Experiment 2

  • Serial control LED

Program:

String comdata = "";//Define a String variable
int led=LOW;
void setup()
{
  Serial.begin(115200);//Set the baud rate
  pinMode(13, OUTPUT);
}
void loop()
{
  while (Serial.available() > 0)  //If there is serial buffer data, continuous cycle
  {        
    comdata += char(Serial.read());   //Read data, only read one byte every time
    delay(2);//serial buffer time
  }
  if (comdata=="on")
    led = HIGH;
  else if(comdata=="off")
    led = LOW;
  digitalWrite(13,led);
  if (comdata.length() > 0)
  {
    Serial.print(comdata);  //Print serial data
    Serial.print("   led:");  //Print serial data
    Serial.println(led);  //Print LED state

    comdata = "";//Clean data
  }
}

Result:

  • LED monitor:

Pin D13 connects to a LED. In serial monitor box, write "on" in send window, then click send (or press enter), LED light. Write "off", click send (or press enter), LED off. LED keep the last state in other situation.

  • Serian monitor

In serial monitor box, write "on" in send window, then click send (or press enter), return ""on led:1". Write "off", click send (or press enter), return "off led:0". In other situation, return sent data and LED keep the last state.

Lesson22-command.jpg

Serial receives character string, please refer to:http://www.geek-workshop.com/thread-158-1-1.html

Serial receives character string and convert to array, please refer to: http://www.geek-workshop.com/thread-260-1-1.html

Video