ซีเรียลลิงก์#
การแนะนำ#
SerialLink ช่วยให้ EXP Brain สองตัวสามารถสื่อสารกันได้โดยการส่งและรับข้อมูลอนุกรมดิบผ่าน VEX Link แทนที่จะส่งข้อความที่มีป้ายกำกับ SerialLink จะส่งบัฟเฟอร์ (สตริงหรืออาร์เรย์ไบต์) ซึ่งทำให้มีประโยชน์สำหรับการสื่อสารระหว่างหุ่นยนต์ขั้นสูง เช่น การสตรีมข้อมูลเซ็นเซอร์ การส่งรูปแบบแพ็กเก็ตแบบกำหนดเอง หรือการสร้างโปรโตคอลของคุณเองบนพื้นฐานของไบต์
ทุกครั้งที่ใช้ send จะเพิ่มข้อมูลลงในบัฟเฟอร์รับของ EXP Brain ที่เชื่อมโยง ข้อมูลจะถูกอ่านตามลำดับที่มาถึง (เข้าก่อนออกก่อน) หากมีการส่งบัฟเฟอร์หลายรายการก่อนที่ EXP Brain อีกตัวจะใช้ receive บัฟเฟอร์เหล่านั้นจะยังคงอยู่ในคิวและจะถูกส่งกลับทีละรายการตามลำดับที่ส่ง เนื่องจากข้อมูลสามารถอยู่ในคิวได้ การส่งบัฟเฟอร์ขนาดใหญ่ซ้ำๆ ในแต่ละรอบอาจทำให้เกิดข้อมูลค้าง สำหรับลิงก์ที่สำคัญต่อเวลา ให้ส่งเฉพาะสิ่งที่คุณต้องการเท่านั้น
สำคัญ: หุ่นยนต์ทั้งสองตัวต้องรันโปรเจกต์ที่ใช้ SerialLink พร้อมกัน มิฉะนั้นจะไม่มีการส่ง/รับข้อมูล
หน้านี้ใช้ลิงก์ link เป็น SerialLink โปรดแทนที่ด้วยชื่อที่คุณกำหนดเองตามต้องการ
ด้านล่างนี้คือรายการวิธีการที่มีให้เลือกใช้:
สร้างลิงก์#
สร้าง SerialLink บน EXP Brain ทั้งสองตัวเพื่อสร้างช่องทางการสื่อสารระหว่างกัน ไม่ว่าจะเป็นแบบไร้สายโดยใช้ V5 Robot Radios หรือผ่าน Smart Cable แบบมีสาย เพื่อให้หุ่นยนต์สามารถส่งข้อมูลอนุกรมดิบระหว่างกันได้
SerialLink(smartport, name, linktype, wired)
พารามิเตอร์ |
คำอธิบาย |
|---|---|
|
พอร์ตอัจฉริยะที่ใช้สำหรับลิงก์นี้ คือพอร์ตที่เชื่อมต่อกับวิทยุ V5 Robot (แบบไร้สาย) หรือเสียบสายอัจฉริยะ (แบบมีสาย) เขียนในรูปแบบ |
|
ชื่อเฉพาะที่จะตั้งให้กับลิงก์นี้เป็นสตริง |
|
บทบาทที่ Brain นี้จะใช้สำหรับคู่ลิงก์นี้ แต่ละคู่ต้องประกอบด้วย ผู้จัดการหนึ่งคน และ ผู้ปฏิบัติงานหนึ่งคน:
|
|
ตัวเลือกเสริม การเชื่อมต่อนี้จะเป็นแบบมีสายหรือไร้สาย:
|
รหัสสำหรับหุ่นยนต์ตัวที่ 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 จะคืนค่าว่า EXP Brains บน SerialLink นั้นจับคู่กันหรือไม่
True– สมอง EXP ทั้งสองตัวจับคู่กันและสื่อสารกันผ่านลิงก์นี้False– EXP Brain ทั้งสองตัวไม่ได้จับคู่กันในลิงก์นี้
หมายเหตุ: ควรตรวจสอบให้แน่ใจเสมอว่า 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 Brain อีกตัวที่เชื่อมต่ออยู่
เมื่ออ่านข้อมูลนั้นด้วย receive receive จะส่งคืนค่าก็ต่อเมื่อได้รวบรวมจำนวนไบต์ที่ระบุโดยพารามิเตอร์ receive เท่านั้น ด้วยเหตุนี้ หากโค้ดของคุณเรียก length ด้วยความยาว lengthเสมอ คุณควรตรวจสอบให้แน่ใจว่าข้อความแต่ละข้อความที่ส่งด้วย send มีจำนวนไบต์เท่ากับความยาวที่กำหนด (โดยมักจะใช้การเติมข้อมูล) เพื่อไม่ให้ตัวรับบล็อกรอไบต์เพิ่มเติม
หากคุณไม่ต้องการพึ่งพาจำนวนไบต์ที่แน่นอน ให้ใช้ received แทน ซึ่งจะช่วยให้คุณจัดการกับไบต์ที่มีอยู่ได้โดยไม่ต้องกำหนดความยาวที่เฉพาะเจาะจง
Usage:
send(buffer)
พารามิเตอร์ |
คำอธิบาย |
|---|---|
|
The data to send to the other linked EXP Brain. This can be a |
รหัสสำหรับหุ่นยนต์ตัวที่ 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 รอจำนวนไบต์ที่แน่นอน การส่งข้อความความยาวคงที่จึงมักต้องมีการเติมข้อมูลเมื่อใช้ receive เพื่อให้แต่ละข้อความมีขนาดตามที่คาดไว้ หากผู้ส่งส่งข้อมูลน้อยกว่า send ไบต์ length จะยังคงรอไบต์ที่เหลือและไม่ส่งอะไรกลับมา
หากคิวว่างเปล่าและคุณระบุค่าหมดเวลาเป็น 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 ลงทะเบียนฟังก์ชันที่จะถูกเรียกใช้เมื่อใดก็ตามที่ EXP Brain ได้รับข้อมูลอนุกรม
Usage:
link.received(callback)
พารามิเตอร์ |
คำอธิบาย |
|---|---|
|
A previously defined function that executes when the EXP Brain receives serial data. |
ได้รับ received ฟังก์ชัน โดยแต่ละฟังก์ชันจะมีอาร์กิวเมนต์ 2 ตัว:
รูปแบบการเรียกใช้ฟังก์ชัน Callback:
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)