EV3 MicroPython 指南

EV3 MicroPython 指南 > PYBRICKS 模块 > 消息传递

消息传递

一个 EV3 积木可以使用蓝牙向另一个 EV3 积木发送信息。本页向您展示如何连接多个砖块以及如何编写脚本以在它们之间发送消息。

配对两个 EV3 积木

在两个 EV3 积木交换消息之前,它们必须配对 。您只需第一次执行此作。首先,在所有 EV3 积木上激活蓝牙,如图 19 所示。

消息传递

图 19 打开蓝牙,让蓝牙可见。

现在,您可以让一个 EV3 砖搜索另一个并与之配对,如图 20 所示。

配对后, 请勿单击出现的菜单中的连接 。运行程序时将建立连接,如下所述。

消息传递

图 20 将一个 EV3 砖与另一个 EV3 砖配对。

扫描蓝牙设备时,您会看到设备名称列表。默认情况下,所有 EV3 砖都命名为 ev3dev。单击此处了解如何更改该名称。这使得区分它们变得容易。

如果要配对两个以上的 EV3 积木,请重复图 20 中的步骤。

服务器和客户端

无线网络由充当服务器或客户端的 EV3 砖块组成。 图 21 显示了一个具有一台服务器和一个客户端的示例。消息可以通过两种方式发送:服务器可以向客户端发送消息,客户端可以向服务器发送消息。

消息传递

图 21 具有一台服务器和一台客户端的网络示例。

示例:EV3 蓝牙服务器。

#!/usr/bin/env pybricks-micropython

# Before running this program, make sure the client and server EV3 bricks are
# paired using Bluetooth, but do NOT connect them. The program will take care
# of establishing the connection.

# The server must be started before the client!

from pybricks.messaging import BluetoothMailboxServer, TextMailbox

server = BluetoothMailboxServer()
mbox = TextMailbox('greeting', server)

# The server must be started before the client!
print('waiting for connection...')
server.wait_for_connection()
print('connected!')

# In this program, the server waits for the client to send the first message
# and then sends a reply.
mbox.wait()
print(mbox.read())
mbox.send('hello to you!')

示例:EV3 蓝牙客户端。

#!/usr/bin/env pybricks-micropython

# Before running this program, make sure the client and server EV3 bricks are
# paired using Bluetooth, but do NOT connect them. The program will take care
# of establishing the connection.

# The server must be started before the client!

from pybricks.messaging import BluetoothMailboxClient, TextMailbox

# This is the name of the remote EV3 or PC we are connecting to.
SERVER = 'ev3dev'

client = BluetoothMailboxClient()
mbox = TextMailbox('greeting', client)

print('establishing connection...')
client.connect(SERVER)
print('connected!')

# In this program, the client sends the first message and then waits for the
# server to reply.
mbox.send('hello!')
mbox.wait()
print(mbox.read())

客户端和服务器之间的唯一区别是在程序开始时启动连接:

  • 必须始终首先启动服务器 。它使用 BluetoothMailboxServer 类。然后它使用 wait_for_connection 方法等待客户端。
  • 客户端使用 BluetoothMailboxClient 类。它使用 connect 方法连接到服务器。
  • 之后,在两个 EV3 积木上以相同的方式发送和接收消息。

class BluetoothMailboxServer

表示来自一个或多个远程 EV3 的蓝牙连接的对象。

远程 EV3 可以运行 MicroPython 或标准 EV3 固件。

“服务器”等待“客户端”连接到它。

wait_for_connection(count=1)

等待远程设备上的 BluetoothMailboxClient 进行连接。

参数: count (int) – 要等待的远程连接数。
异常: OSError – 建立连接时出现问题。

close()

关闭所有连接。

class BluetoothMailboxClient

表示与一个或多个远程 EV3 的蓝牙连接的对象。

远程 EV3 可以运行 MicroPython 或标准 EV3 固件。

“客户端”启动与等待的“服务器”的连接。

connect(brick)

连接到另一台设备上的 BluetoothMailboxServer。

远程设备必须配对并等待连接。看 BluetoothMailboxServer.wait_for_connection() 。

参数: 编程块 str) – 要连接的远程 EV3 的名称或蓝牙地址。
异常: OSError – 建立连接时出现问题。

close()

关闭所有连接。

Mailboxes  邮箱

邮箱用于与其他 EV3 积木之间发送数据。

邮箱有一个名称 ,类似于电子邮件的“主题”。如果两个 EV3 积木有一个同名的邮箱,它们可以在它们之间发送消息。每个 EV3 积木都可以读取自己的邮箱,并向另一个 EV3 积木上的邮箱发送消息。

根据您要交换的邮件类型(字节、布尔值、数字或文本),您可以选择以下邮箱之一。

class Mailbox(nameconnectionencode=Nonedecode=None)

表示包含数据的邮箱的对象。

您可以读取由其他 EV3 积木传递的数据,或将数据发送到具有相同邮箱的其他积木。

默认情况下,邮箱仅读取和发送字节。要发送其他数据,您可以提供一个将 Python 对象编码为字节的编码函数,以及一个将字节转换回 Python 对象的解码函数。

参数:
  • name (str) – 此邮箱的名称。
  • connection – 连接对象,例如 BluetoothMailboxClient。
  • encodecallable)——将 Python 对象编码为字节的函数。
  • decode (callable) – 从字节创建新 Python 对象的函数。

read()

获取邮箱的当前值。

返回: 当前值或无 ,如果邮箱为空。

send(valuebrick=None)

将值发送到连接设备上的此邮箱。

参数:
  • value – 将传递到邮箱的值。
  • 编程块 (str) – 要广播到所有连接设备的砖块或无的名称或蓝牙地址。
异常:

OSError – 连接有问题。

wait()

等待远程设备更新邮箱。

wait_new()

等待将新值传递到邮箱,该值不等于邮箱中的当前值。

返回: 新值。

class LogicMailbox(nameconnection)

表示包含布尔数据的邮箱的对象。

这就像常规邮箱一样,但值必须是 True 或 False。

这与 EV3-G 中的“逻辑”邮箱类型兼容。

参数:
  • name (str) – 此邮箱的名称。
  • connection – 连接对象,例如 BluetoothMailboxClient。

class NumericMailbox(nameconnection)

表示包含数字数据的邮箱的对象。

这与常规邮箱一样工作,但值必须是数字,例如 15 或 12.345

这与 EV3-G 中的“数字”邮箱类型兼容。

参数:
  • name (str) – 此邮箱的名称。
  • connection – 连接对象,例如 BluetoothMailboxClient。

class TextMailbox(nameconnection)

表示包含文本数据的邮箱的对象。

这就像普通邮箱一样,但数据必须是字符串,例如 'hello!' 或 '我的名字是 EV3'。

这与 EV3-G 中的“文本”邮箱类型兼容。

参数:
  • name (str) – 此邮箱的名称。
  • connection – 连接对象,例如 BluetoothMailboxClient。

创建更大的网络

本模块中的课程不仅限于两个 EV3 积木。例如,您可以向网络添加更多客户端。 图 22 显示了一个伪代码示例。

消息传递

图 22 具有一台服务器和两台客户端的网络示例。