시리얼링크#
소개#
SerialLink는 두 EXP Brain이 VEX Link를 통해 원시 직렬 데이터를 송수신하여 통신할 수 있도록 합니다. 레이블이 지정된 메시지 대신 SerialLink는 버퍼(문자열 또는 바이트 배열)를 전송합니다. 따라서 센서 데이터 스트리밍, 사용자 지정 패킷 형식 전송 또는 바이트 기반의 자체 프로토콜 구축과 같은 고급 로봇 간 통신에 유용합니다.
send를 사용할 때마다 연결된 EXP Brain의 수신 버퍼에 데이터가 추가됩니다. 데이터는 도착 순서대로 읽힙니다(선입선출). 다른 EXP Brain이 receive를 사용하기 전에 여러 버퍼가 전송되면 해당 버퍼는 큐에 남아 전송된 순서대로 하나씩 반환됩니다. 데이터가 큐에 쌓일 수 있으므로 매 루프마다 대용량 버퍼를 반복적으로 전송하면 백로그가 발생할 수 있습니다. 시간이 중요한 링크의 경우 필요한 데이터만 전송하십시오.
중요: 두 로봇 모두 동시에 SerialLink 하는 프로젝트를 실행해야 합니다. 그렇지 않으면 데이터 송수신이 이루어지지 않습니다.
This page uses link as the example SerialLink name. Replace it with your own configured name as needed.
다음은 사용 가능한 방법 목록입니다.
링크 생성#
EXP Brain 두 대 모두에 SerialLink를 생성하여 V5 로봇 라디오를 사용한 무선 통신 또는 스마트 케이블을 사용한 유선 통신을 통해 로봇 간 통신 채널을 구축하십시오. 이렇게 하면 로봇들이 서로에게 원시 직렬 데이터를 전송할 수 있습니다.
SerialLink(smartport, name, linktype, wired)
매개변수 |
설명 |
|---|---|
|
이 링크에 사용되는 스마트 포트는 V5 로봇 라디오가 연결된 포트(무선) 또는 스마트 케이블이 꽂힌 포트(유선)입니다. 형식은 |
|
이 링크에 부여할 고유한 문자열 이름입니다. |
|
이 브레인이 해당 링크 쌍에 사용할 역할입니다. 각 쌍에는 관리자 1명과 작업자 1명이 포함되어야 합니다.
|
|
선택 사항입니다. 이 연결이 유선인지 무선인지 여부를 지정합니다.
|
로봇 1용 코드
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Manager """ link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER)로봇 2용 코드
""" Create a wireless link in Port 1 name: VEXRoboticsLink123456789 linktype: Worker """ link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER)
사용 가능한 방법#
SerialLink 설정이 완료되고 Brains가 연결되면 아래 방법을 사용하여 원시 직렬 데이터를 송수신하고 응답할 수 있습니다.
is_linked#
is_linked 메서드는 SerialLink의 EXP Brain이 서로 쌍을 이루고 있는지 여부를 반환합니다.
True– 두 개의 EXP 브레인이 이 링크를 통해 연결되어 통신하고 있습니다.False– 이 링크에서는 두 개의 EXP 브레인이 쌍을 이루고 있지 않습니다.
참고: 프로젝트 시작 시 추가 코드를 실행하기 전에 EXP Brains가 제대로 연결되었는지 항상 확인하는 것이 좋습니다.
Usage:
link.is_linked()
매개변수 |
설명 |
|---|---|
이 메서드에는 매개변수가 없습니다. |
로봇 1용 코드
# Send a message to the other robot # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") link.send("HELLO")로봇 2용 코드
# Display the message that Robot 1 sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() def received_message(bytearray, length): # Slice the bytearray to the valid data data = bytearray[:length] # Decode and strip the message before displaying it text = data.decode().strip() brain.screen.print("Got: ", text) # Call received_message whenever a message is received link.received(received_message)
send#
send 전송하여 연결된 다른 EXP 브레인으로 원시 직렬 바이트를 전송합니다.
receive로 해당 데이터를 읽을 때 receive length 매개변수로 지정된 바이트 수를 수집한 후에만 반환됩니다. 따라서 코드가 항상 고정된 length로 receive 호출하는 경우 send 로 전송되는 각 메시지가 정확히 그 바이트 수(종종 패딩을 통해)인지 확인하여 수신자가 추가 바이트를 기다리며 차단되지 않도록 해야 합니다.
정확한 바이트 수에 의존하고 싶지 않다면 대신 received를 사용하세요. 이렇게 하면 특정 길이를 요구하지 않고 사용 가능한 바이트를 처리할 수 있습니다.
Usage:
send(buffer)
매개변수 |
설명 |
|---|---|
|
연결된 다른 EXP Brain으로 보낼 데이터입니다. 이는 |
로봇 1용 코드
# Send a message to the other robot # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") link.send("HELLO")로봇 2용 코드
# Display the message that Robot 1 sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() def received_message(bytearray, length): # Slice the bytearray to the valid data data = bytearray[:length] # Decode and strip the message before displaying it text = data.decode().strip() brain.screen.print("Got: ", text) # Call received_message whenever a message is received link.received(received_message)
receive#
receive 연결된 EXP Brain에서 직렬 데이터를 FIFO 순서(가장 오래된 읽지 않은 데이터부터)로 반환합니다.
receive 정확한 바이트 수를 기다리기 때문에, 고정 길이 메시징에서는 send 사용할 때 각 메시지가 예상 크기에 도달하도록 패딩이 필요한 경우가 많습니다. 송신자가 length 바이트보다 적은 바이트를 전송하면 receive 나머지 바이트를 계속 기다리고 아무것도 반환하지 않습니다.
큐가 비어 있고 타임아웃 값을 timeout, receive 초 동안 새 데이터가 도착할 때까지 기다립니다. 해당 시간 내에 데이터가 도착하지 않으면 None반환합니다. 이후에 전송되는 모든 데이터는 나중에 읽을 수 있도록 큐에 남아 있습니다.
Usage:
receive(length, timeout)
매개변수 |
설명 |
|---|---|
|
The number of bytes to receive. |
|
선택 사항입니다. |
로봇 1용 코드
# Send a math problem for the other Brain to solve # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") """ Change whether to "add" or "subtract", then what numbers to use in the operation. """ packet = "add,7,2.5" # Pad with spaces until it's exactly 16 bytes while len(packet) < 16: packet = packet + " " link.send(packet)로봇 2용 코드
# Solve the math problem that the other Brain sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() # Receive exactly 16 bytes buffer = link.receive(16) # Remove the added " " padding text = buffer.decode().strip() # Separate the operator and numbers parts = text.split(",") operator = parts[0] a = float(parts[1]) b = float(parts[2]) # Use what operator was sent if operator == "add": result = a + b elif operator == "subtract": result = a - b # If not "add" or "subtract", do nothing else: pass brain.screen.print("Result = ", result)
received#
received 에 등록합니다.
Usage:
link.received(callback)
매개변수 |
설명 |
|---|---|
|
EXP Brain이 직렬 데이터를 수신할 때 실행되는 이전에 정의된 함수. |
received 인수를 받아 호출되었습니다.
콜백 시그니처:
callback(buffer, length)
논쟁 |
설명 |
|---|---|
|
수신된 직렬 데이터를 포함하는 |
|
이 콜백에 대해 |
로봇 1용 코드
# Send a message to the other robot # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.MANAGER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 1 - Manager") link.send("HELLO")로봇 2용 코드
# Display the message that Robot 1 sends # Create the link link = SerialLink(Ports.PORT1, "VEXRoboticsLink123456789", VexlinkType.WORKER) # Do not run code UNTIL the Brains are linked while not link.is_linked(): wait(0.1, SECONDS) brain.screen.print("Robot 2 - Worker") brain.screen.next_row() def received_message(bytearray, length): # Slice the bytearray to the valid data data = bytearray[:length] # Decode and strip the message before displaying it text = data.decode().strip() brain.screen.print("Got: ", text) # Call received_message whenever a message is received link.received(received_message)