Difference between revisions of "Lesson 22--Microduino "Serial port debugging""
Line 4: | Line 4: | ||
| | | | ||
==Objective== | ==Objective== | ||
− | In | + | 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== | ==Equipment== | ||
Line 40: | Line 40: | ||
} | } | ||
</source> | </source> | ||
− | + | 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. | |
[[File:lesson22-send.jpg|600px|center|thumb]] | [[File:lesson22-send.jpg|600px|center|thumb]] | ||
[[File:lesson22-receive.jpg|600px|center|thumb]] | [[File:lesson22-receive.jpg|600px|center|thumb]] | ||
− | ''' | + | '''Note:''' |
− | *String type variable converts the characters into the string | + | *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. | *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. | *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 = "": 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 defines the character string, that is array, you can use comdata[0], comdata[1]...comdata[n] to quote every data. |
==Experiment 2== | ==Experiment 2== | ||
*Serial control LED | *Serial control LED | ||
− | + | Program: | |
<source lang="cpp"> | <source lang="cpp"> | ||
String comdata = "";//Define a String variable | String comdata = "";//Define a String variable | ||
Line 83: | Line 83: | ||
} | } | ||
</source> | </source> | ||
− | + | Result: | |
− | *LED | + | *LED monitor: |
− | Pin D13 connects to a LED. In serial monitor box, write "on" in send window, then click send (or press enter) | + | 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 | *Serian monitor | ||
− | In serial monitor box, write "on" in send window, then click send (or press enter) | + | 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. |
[[File:lesson22-command.jpg|600px|center|thumb]] | [[File:lesson22-command.jpg|600px|center|thumb]] | ||
Serial receives character string, please refer to:http://www.geek-workshop.com/thread-158-1-1.html | 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 | + | Serial receives character string and convert to array, please refer to: http://www.geek-workshop.com/thread-260-1-1.html |
==Video== | ==Video== | ||
|} | |} |
Latest revision as of 07:33, 12 September 2016
Language: | English • 中文 |
---|
ObjectiveIn 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. 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 |