Difference between revisions of "Lesson 22--Microduino "Serial port debugging""
Line 1: | Line 1: | ||
− | {{Language| | + | {{Language|第二十二课--Microduino_串口调试}} |
{| style="width: 800px;" | {| style="width: 800px;" | ||
|- | |- |
Revision as of 15:03, 2 March 2014
Language: | English • 中文 |
---|
ObjectiveIn perious 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. EquipmentMicroduino-Core Microduino-FT232R
Experiment 1
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. Note:
Experiment 2
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:
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.
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. 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 |