Enlace serial#
¿Qué es un enlace serial?#
VEXlink permite la comunicación entre dos cerebros EXP, lo que permite conexiones punto a punto y la creación de redes de comunicación entre varios robots. Admite comunicación inalámbrica y por cable; en estas últimas, se recomienda un cable inteligente modificado para evitar problemas de enrutamiento de energía. Para la comunicación inalámbrica, cada robot necesita una radio robótica V5 conectada a un puerto inteligente. La radio VEXlink puede usarse junto con la radio VEXnet de un controlador V5, que debe conectarse al puerto inteligente con el número más alto para evitar conflictos.
La clase SerialLink dentro de VEXlink permite enviar un flujo de datos entre robots; el contenido del flujo debe ser comprendido tanto por el robot transmisor como por el receptor; esta clase generalmente se utiliza como parte de una capa superior de software que codifica y decodifica paquetes según sea necesario.
Para crear un VEXlink, ambos cerebros V5 deben estar conectados a una radio robótica V5.
Inicializando la clase SerialLink#
Un VEXlink se crea utilizando el siguiente constructor:
SerialLink(port, name, linktype, wired)
Este constructor utiliza cuatro parámetros:
Parámetro |
Descripción |
---|---|
|
Un Puerto inteligente válido al que está conectada la radio VEXlink. |
|
El nombre de este enlace. Se recomienda que esta cadena única sea lo suficientemente larga como para que, al ser procesada por vexos, genere un ID único. Un nombre de enlace incorrecto sería algo genérico como “vexlink”, ya que podría ser utilizado por otro equipo. |
|
The type of link, either |
|
Optional. Whether or not it is a wired link. Set to |
# Construct a VEXlink "seriallink" with the SerialLink class.
seriallink = SerialLink(Ports.PORT1, "Link", VexlinkType.MANAGER)
This seriallink
object will be used in all subsequent examples throughout this API documentation when referring to SerialLink class methods.
Métodos de clase#
is_linked()#
The is_linked()
method returns the link status of the Serial Link.
Returns: True
if the Serial Link is active and connected to the paired Brain. False
if it is not.
send()#
The send()
method sends a buffer through the Serial Link.
Parámetros |
Descripción |
---|---|
buffer |
Una cadena o un conjunto de bytes. El mensaje a enviar. |
Devoluciones: Ninguna.
# Send the string 'test'.
seriallink.send('test')
receive()#
The receive(length, timeout)
method receives data from the Serial Link.
Parámetros |
Descripción |
---|---|
longitud |
La cantidad máxima de datos a esperar. |
se acabó el tiempo |
Opcional. El valor de tiempo de espera en milisegundos antes de que la función regrese. |
Devuelve: Un bytearray con los datos recibidos o Ninguno si no se recibieron datos.
# Wait for 128 bytes of data for 1000mS.
buffer = seriallink.receive(128, 1000)
received()#
The received(callback)
method registers a callback function for when data is received.
Parámetros |
Descripción |
---|---|
llamar de vuelta |
La función de devolución de llamada que se llamará cuando se reciban datos. |
Devoluciones: Ninguna.
# Define a function data_received()
def data_received():
# The Brain will print that data was received by the
# Serial Link on the Brain's screen.
Brain.screen.print("data received by Serial Link")
# Run data_received when data is received by the Serial Link.
seriallink.received(data_received)
installed()#
The installed()
method checks if the Serial Link is connected.
Returns: True
if the Serial Link is connected. False
if it is not.