
Arduino入门15: 串口监视器的使用
章节
类别和标签
许可
本教程系列是专门面向零基础的Arduino初学者,让初学者不仅对Arduino的历史,原理操作有基本的入门,而且对Arduino背后所代表的观念和思潮运动有所理解。脑震荡原创内容,请尊重作者的知识产权。
前言
在《Arduino入门14: 步进电机的控制》里,我们初步了解了步进电机的基础,以及如何通过Arduino和ULN2003芯片驱动步进电机。本篇教程在上一篇的电路上改动一下代码,来学习一下如何使用串口监视器。
串口监视器(Serial Monitor),顾名思义是用来监视串口通信的小工具。不要小看串口监视器,用好了,它就是帮你解决大多数烦恼的Swiss Army Knife(瑞士军刀)。Arduino和电脑是通过串口连接的,所有的数据通信都通过这个通道,所以串口监视器就像是这个通道上安装的安全摄像头一样。然你可以查看通信数据。Arduino IDE没有像其它高级IDE提供比较全面的debug工具,所以合理利用好串口监视器是Arduino代码debug的主要途径。
在本教程中,我们在《Arduino入门14: 步进电机的控制》的代码基础上添加一些控制串口监视器的代码来学习。所以在阅读下面章节前请先按照《Arduino入门14: 步进电机的控制》中的说明准备好实验材料并连接好电路。
串口监视器界面
首先,我们来熟悉一下如何打开串口监视器的UI,以及各个界面元素的作用。
如何打开串口监视器
串口监视器的入口在Arduino的工具栏右侧,带有一个搜索图标的按钮。点击这个按钮就会跳出串口监视器的窗口界面。
波特率(baud rate)
窗口界面各选项都比较容易理解,唯独右下方的下拉菜单中的300baud~115200baud这个选项需要解释一下。这些选项是指波特率(baud rate),即串口通信中每秒传输的数据位数。9600baud就是每秒能传输9600位(bit)。选择的波特率必须与Arduino代码中Serial.begin()函数定义的波特率一致,串口监视器才会显示正确的信息,不然会出现乱码。
串口监视器输出消息
下面我们来实践一下如何使用串口监视器输出消息,这也是串口监视器最常用的基本功能之一,通常被用来作为代码调试或者输出提示的利器。
复制《Arduino入门14: 步进电机的控制》中的代码,打开Arduino IDE,新建sketch文件黏贴代码并修改成如下:
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 512
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);
// the previous reading from the analog input
int previous = 0;
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
Serial.begin(9600);
}
void loop()
{
// get the sensor value
int val = analogRead(0);
// move a number of steps equal to the change in the
// sensor reading
stepper.step(val - previous);
Serial.print("Pot Read:");
Serial.print(val);
Serial.print(";Steps");
Serial.println(val - previous);
// remember the previous value of the sensor
previous = val;
}
使用串口监视器必须首先使用Serial.begin()在setup()函数内定义波特率。
Serial.begin(9600);
然后在你需要输出信息的地方使用 Serial.print()或者 Serial.println(),二者的差别就是 Serial.println()输出后会自动加上一个换行符”\n”。 Serial.print()函数有两种用法:
Serial.println(val)
Serial.println(val,format)
val是打印的值,可以是任意数据类型。format是输出的数据格式。比如:
Serial.print(78, BIN) //输出 "1001110"
Serial.print(78, OCT) //输出 "116"
Serial.print(78, DEC) //输出 "78"
Serial.print(78, HEX) //输出 "4E"
Serial.print(1.23456, 0) //输出 "1"
Serial.print(1.23456, 2) //输出 "1.23"
Serial.print(1.23456, 4) //输出 "1.2346"
Serial.print('N') //输出 "N"
Serial.print("Hello world.") //输出 "Hello world.”
Serial.print("Pot Read:");
Serial.print(val);
Serial.print(";Steps");
Serial.println(val - previous);
这段代码的作用时,在串口监视器里输出两个数据,val代表A0引脚读取的电位器的值,val-previous代表步进电机应该移动的步数。
我们保存代码并上传成功后,我们打开串口监视器,选择对应的波特率,就能看到串口监视器里不断地打印出数据消息。这些数据就是Arduino运行时把数据发送给串口监视器显示的,这样我们就能动态地监测某些传感器和执行器实现的功能是否正常,或者程序当前执行到哪一步了,遇到了什么样的问题等等。
串口监视器输入命令
可以看到串口监视器界面的顶部有一个输入框和“发送”按钮,没错,串口监视器不但能接受Arduino程序发送过来的数据,而且还能够向Arduino程序发送命令。下面我们再次修改代码。我们要实现的是直接通过串口监视器控制步进电机的移动的步数。
#include <Stepper.h>
// change this to the number of steps on your motor
#define STEPS 512
// create an instance of the stepper class, specifying
// the number of steps of the motor and the pins it's
// attached to
Stepper stepper(STEPS, 8, 9, 10, 11);
void setup()
{
// set the speed of the motor to 30 RPMs
stepper.setSpeed(30);
// this line is for Leonardo's, it delays the serial interface
// until the terminal window is opened
while (!Serial);
Serial.begin(9600);
}
void loop()
{
if (Serial.available())
{
int steps = Serial.parseInt();
stepper.step(steps);
}
}
同样要使用串口监视器,我们必须首先在setup()里定义好波特率。 在loop()函数中,我们加了这样的判断。
if (Serial.available())的判断的作用时告诉Arduino,如果Arduino串口收到命令,执行判断内部的代码,否则什么也不做。
int steps = Serial.parseInt();
Serial.parseInt()的作用就是读取串口接收到的整数值。
上传代码。然后在串口监视器的文本框里输入任意的整数,然后步进电机就会前进相应的步数。
以上就是串口监视器的两种常见用途。在Arduino程序调试的时候善用它会对助你事半功倍。
0个留言