Skip to Content
《Arduino 烹饪书》第三版
book

《Arduino 烹饪书》第三版

by Michael Margolis, Brian Jepson, Nicholas Robert Weldin
May 2025
Beginner to intermediate
798 pages
10h 10m
Chinese
O'Reilly Media, Inc.
Content preview from 《Arduino 烹饪书》第三版

串行通信

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

4.0 引言

串行通信为 Arduino 板与计算机和其他设备进行交互提供了一种简单灵活的方式。本章将介绍如何使用这一功能发送和接收信息。

第 1 章介绍了如何将 Arduino USB 串口连接到电脑以上传草图。上传过程 ,将数据从电脑发送到 Arduino,Arduino 将状态信息发回电脑,以确认传输正常。这里的配方展示了如何使用相同的通信链路在 Arduino 和电脑或其他串口设备之间发送和接收任何信息。

串行通信也是一种方便的调试工具。 您可以从 Arduino 向计算机发送调试信息,并将其显示在计算机屏幕上,或发送到另一个设备,如 Raspberry Pi 或另一个 Arduino。您也可以使用外部 LCD 显示器来显示这些信息,但您很可能需要使用 I2C 或 SPI 与此类显示器进行通信(参见第 13 章)。

Arduino IDE(在配方 1.3 中描述)提供了一个串行监视器 (如图 4-1 所示),用于显示从 Arduino 发送的串行数据 。您还可以通过在发送按钮左侧的文本框中输入文本,将数据从串行监视器 发送到 Arduino。Arduino 还包含一个串行绘图仪 ,可以将 Arduino 发送的串行数据绘制成图形(参见配方 4.1)。

Arduino 串行监视器屏幕

您可以使用右下角的下拉框设置数据传输的速度(波特率, ,单位为比特/秒)。请确保将其设置为您在Serial.begin() 中使用的任何值。 默认速率为每秒 9,600 比特,这在很多情况下都没问题,但如果您使用的设备需要更高的速度,您可以将高于 9,600 比特的数字传给 。Serial.begin()

您可以使用波特率左侧的下拉菜单在每条信息的末尾自动发送换行符(ASCII 字符 10)、 回车符(ASCII 字符 13)、 换行符和回车符的组合("Both NL & CR")或不发送结束符("No line ending")。

您的 Arduino 草图可以使用串口间接访问(通常是通过用 Processing 或 Python 等语言编写的代理程序)计算机所拥有的所有资源(内存、屏幕、键盘、鼠标、网络连接等)。您的电脑还可以使用串行链接与某些传感器或连接到 Arduino 的其他设备进行交互。如果想使用串行通信与多个设备对话,要么需要多个串行端口,要么需要使用软件串行来使用 Arduino 引脚模拟串行端口(参见"使用数字引脚模拟串行硬件")

备注

许多支持串行通信的传感器和输出设备也支持 SPI 或 I2C(参见第 13 章)。虽然串行通信广为人知且具有一定的通用性,但如果要连接的传感器或输出设备支持 SPI 或 I2C ,则应考虑使用这两种协议。在与多个设备通信时,这两种协议都具有更大的灵活性。

串行通信的实现涉及硬件和软件。硬件在 Arduino 和与其对话的设备之间提供电信号。软件使用硬件发送字节或比特,连接的硬件可以理解这些字节或比特。Arduino 串行库使您免于接触大部分复杂的硬件,但了解基础知识对您还是有帮助的,尤其是当您需要排除项目中串行通信的任何故障时。

串行硬件

串行硬件发送和接收的数据是代表连续比特的电脉冲。构成字节信息的 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

技术主管的进阶之路 (Chinese Edition)

技术主管的进阶之路 (Chinese Edition)

Anemari Fiser

Publisher Resources

ISBN: 9798341659582