• VEXcode Robotics Logo VEX Robotics Logo
  • VEX API Home Button VEX API Home Button
  • VEX 123 logo in purple VEX 123 logo in white
  • VEX GO logo in lime green VEX GO logo in white
  • VEXcode AIM logo in blue VEXcode AIM logo in white
  • VEX IQ logo in blue VEX IQ logo in white
  • VEX EXP logo in red VEX EXP logo in white
  • VEX V5 logo in red VEX V5 logo in white
  • VEX CTE logo in green VEX CTE logo in white
  • VEX AIR logo in orange VEX AIR logo in white
  • VEXcode VR logo in gold VEXcode VR logo in white
ข้ามไปยังเนื้อหาหลัก
Ctrl+K

< Back to Platform Select

  • VEX V5
  • เอกสารอ้างอิง API VEX V5
  • วีเอ็กซ์ลิงก์
  • ซีเรียลลิงก์
ไทย
  • English
  • العربية
  • বাংলা
  • Česky
  • Deutsch
  • Ελληνικά
  • Español
  • Español Latinoamericano
  • Suomi
  • Français
  • Gaeilge
  • हिंदी
  • Magyar
  • Bahasa Indonesia
  • 日本語
  • 한국어
  • Lietuvių
  • Latviešu
  • മലയാളം
  • Nederlands
  • Polski
  • Português
  • Русский
  • Svenska
  • Türkçe
  • Українська
  • Tiếng Việt
  • 简体中文
  • 繁體中文
  • VEXcode Robotics Logo

การนำทางส่วนต่างๆ

  • บล็อก
  • Python
    • ระบบขับเคลื่อน
    • การเคลื่อนไหว
    • วิสัยทัศน์
    • หน้าจอ
    • ตัวควบคุม
    • การรับรู้
    • เฉื่อย
    • อุปกรณ์ 3 สาย
    • ระบบนิวแมติกส์
    • สมอง
    • เสียง
    • การ์ด SD
    • วีเอ็กซ์ลิงก์
      • ข้อความลิงก์
      • ซีเรียลลิงก์
    • คอนโซล
    • ตรรกะ
    • แขนกล 6 แกน
    • แม่เหล็ก
    • การแข่งขัน
    • ไลบรารี MicroPython
  • C++
  • บทเรียน VEXcode

การนำทางแพลตฟอร์ม

  • VEX 123 logo in purple VEX 123 logo in white
  • VEX GO logo in lime green VEX GO logo in white
  • VEXcode AIM logo in blue VEXcode AIM logo in white
  • VEX IQ logo in blue VEX IQ logo in white
  • VEX EXP logo in red VEX EXP logo in white
  • VEX V5 logo in red VEX V5 logo in white
  • VEX CTE logo in green VEX CTE logo in white
  • VEX AIR logo in orange VEX AIR logo in white
  • VEXcode VR logo in gold VEXcode VR logo in white

ซีเรียลลิงก์#

  • การแนะนำ

  • สร้างลิงก์

  • วิธีการที่มีอยู่

    • เชื่อมโยง

    • ส่ง

    • รับ

    • ได้รับ

การแนะนำ#

SerialLink ช่วยให้ V5 Brain สองตัวสามารถสื่อสารกันได้โดยการส่งและรับข้อมูลอนุกรมดิบผ่าน VEX Link แทนที่จะส่งข้อความที่มีป้ายกำกับ SerialLink จะส่งบัฟเฟอร์ (สตริงหรืออาร์เรย์ไบต์) ซึ่งทำให้มีประโยชน์สำหรับการสื่อสารระหว่างหุ่นยนต์ขั้นสูง เช่น การสตรีมข้อมูลเซ็นเซอร์ การส่งรูปแบบแพ็กเก็ตแบบกำหนดเอง หรือการสร้างโปรโตคอลของคุณเองบนพื้นฐานของไบต์

การใช้งาน send ทุกครั้งจะเพิ่มข้อมูลลงในบัฟเฟอร์รับของ V5 Brain ที่เชื่อมโยง ข้อมูลจะถูกอ่านตามลำดับที่มาถึง (เข้าก่อนออกก่อน) หากมีการส่งบัฟเฟอร์หลายรายการก่อนที่ V5 Brain อีกตัวจะใช้ receive บัฟเฟอร์เหล่านั้นจะยังคงอยู่ในคิวและจะถูกส่งกลับทีละรายการตามลำดับที่ส่ง เนื่องจากข้อมูลสามารถอยู่ในคิวได้ การส่งบัฟเฟอร์ขนาดใหญ่ซ้ำๆ ในแต่ละรอบอาจทำให้เกิดข้อมูลค้าง สำหรับลิงก์ที่ต้องการเวลาอย่างเร่งด่วน ให้ส่งเฉพาะสิ่งที่คุณต้องการเท่านั้น

สำคัญ: หุ่นยนต์ทั้งสองตัวต้องรันโปรเจกต์ที่ใช้ SerialLink พร้อมกัน มิฉะนั้นจะไม่มีการส่ง/รับข้อมูล

หน้านี้ใช้ลิงก์ link เป็น SerialLink โปรดแทนที่ด้วยชื่อที่คุณกำหนดเองตามต้องการ

ด้านล่างนี้คือรายการวิธีการที่มีให้เลือกใช้:

  • is_linked – ส่งคืนว่าสมองนี้เชื่อมต่อกับสมองคู่ของมันอยู่หรือไม่

  • send – ส่งข้อความสั้น (พร้อมข้อมูลจำนวนเต็ม/ทศนิยมเสริม) ไปยัง Brain ที่จับคู่กัน

  • receive – รอและส่งคืนข้อความขาเข้าถัดไป

  • received – ลงทะเบียนฟังก์ชันที่จะถูกเรียกเมื่อใดก็ตามที่ได้รับข้อความใหม่

สร้างลิงก์#

สร้าง SerialLink บน V5 Brain ทั้งสองตัวเพื่อสร้างช่องทางการสื่อสารระหว่างกัน ไม่ว่าจะเป็นแบบไร้สายโดยใช้ V5 Robot Radios หรือผ่านสาย Smart Cable เพื่อให้หุ่นยนต์สามารถส่งข้อมูลอนุกรมดิบระหว่างกันได้

SerialLink(smartport, name, linktype, wired)

พารามิเตอร์

คำอธิบาย

smartport

พอร์ตอัจฉริยะที่ใช้สำหรับลิงก์นี้ คือพอร์ตที่เชื่อมต่อกับวิทยุ V5 Robot (แบบไร้สาย) หรือเสียบสายอัจฉริยะ (แบบมีสาย) เขียนในรูปแบบ Ports.PORTx โดยที่ x คือหมายเลขพอร์ต

name

ชื่อเฉพาะที่จะตั้งให้กับลิงก์นี้เป็นสตริง
หมายเหตุ: ชื่อนี้ควรมีความยาวเพียงพอที่จะสร้างรหัสเฉพาะตัว เพื่อป้องกันไม่ให้ไปซ้ำกับลิงก์ของทีมอื่นโดยไม่ได้ตั้งใจ

linktype

บทบาทที่ Brain นี้จะใช้สำหรับคู่ลิงก์นี้ แต่ละคู่ต้องประกอบด้วย ผู้จัดการหนึ่งคน และ ผู้ปฏิบัติงานหนึ่งคน:

  • VexlinkType.MANAGER – ใช้สำหรับหุ่นยนต์ที่จะส่งข้อความจำนวนมากหรือขนาดใหญ่กว่า ผู้จัดการมีแบนด์วิดท์ขาออกสูงกว่า (~1040 ไบต์/วินาที)
  • VexlinkType.WORKER – ใช้สำหรับหุ่นยนต์ที่จะส่งข้อความจำนวนน้อยหรือขนาดเล็กกว่า ผู้ปฏิบัติงานมีแบนด์วิดท์ขาออกต่ำกว่า (~520 ไบต์/วินาที)

wired

ตัวเลือกเสริม การเชื่อมต่อนี้จะเป็นแบบใช้สายหรือไร้สาย:

  • True – V5 Brains เชื่อมต่อกันผ่านสาย Smart Cable
  • False (ค่าเริ่มต้น) – V5 Brains เชื่อมต่อแบบไร้สายโดยใช้ V5 RObot Radios

รหัสสำหรับหุ่นยนต์ตัวที่ 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 จะส่งคืนค่าว่า V5 Brains บน SerialLink นั้นจับคู่กันหรือไม่

  • True – สมอง V5 ทั้งสองตัวจับคู่กันและสื่อสารกันผ่านลิงก์นี้

  • False – หน่วยประมวลผล V5 สองตัวไม่ได้จับคู่กันในลิงก์นี้

หมายเหตุ: ควรตรวจสอบให้แน่ใจเสมอว่าได้เชื่อมต่อ V5 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 จะส่งข้อมูลไบต์ดิบแบบอนุกรมไปยัง V5 Brain อีกตัวที่เชื่อมต่ออยู่

เมื่ออ่านข้อมูลนั้นด้วย receive receive จะส่งคืนค่าก็ต่อเมื่อได้รวบรวมจำนวนไบต์ที่ระบุโดยพารามิเตอร์ receive เท่านั้น ด้วยเหตุนี้ หากโค้ดของคุณเรียก length ด้วยความยาว lengthเสมอ คุณควรตรวจสอบให้แน่ใจว่าข้อความแต่ละข้อความที่ส่งด้วย send มีจำนวนไบต์เท่ากับความยาวที่กำหนด (โดยมักจะใช้การเติมข้อมูล) เพื่อไม่ให้ตัวรับบล็อกรอไบต์เพิ่มเติม

หากคุณไม่ต้องการพึ่งพาจำนวนไบต์ที่แน่นอน ให้ใช้ received แทน ซึ่งจะช่วยให้คุณจัดการกับไบต์ที่มีอยู่ได้โดยไม่ต้องกำหนดความยาวที่เฉพาะเจาะจง

Usage:
send(buffer)

พารามิเตอร์

คำอธิบาย

buffer

The data to send to the other linked V5 Brain. This can be a string or a bytearray.

รหัสสำหรับหุ่นยนต์ตัวที่ 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 ส่งข้อมูลอนุกรมจาก V5 Brain ที่เชื่อมต่ออีกตัวหนึ่งตามลำดับ FIFO (ข้อมูลที่ยังไม่ได้อ่านเก่าที่สุดจะอยู่ลำดับแรก)

เนื่องจาก receive รอจำนวนไบต์ที่แน่นอน การส่งข้อความความยาวคงที่จึงมักต้องมีการเติมข้อมูลเมื่อใช้ receive เพื่อให้แต่ละข้อความมีขนาดตามที่คาดไว้ หากผู้ส่งส่งข้อมูลน้อยกว่า send ไบต์ length จะยังคงรอไบต์ที่เหลือและไม่ส่งอะไรกลับมา

หากคิวว่างเปล่าและคุณระบุค่าหมดเวลาเป็น timeout, receive จะรอข้อมูลใหม่เป็นเวลาสูงสุดตามจำนวนมิลลิวินาทีที่ระบุ หากไม่มีข้อมูลใดเข้ามาในช่วงเวลานั้น ฟังก์ชันจะส่งคืนค่า Noneข้อมูลใด ๆ ที่ส่งมาหลังจากนั้นจะยังคงอยู่ในคิวเพื่อรออ่านในภายหลัง

Usage:
receive(length, timeout)

พารามิเตอร์

คำอธิบาย

length

The number of bytes to receive. receive waits until this many bytes have arrived, then returns them as a bytearray. If length is 0, receive returns None immediately. If you request more bytes than the sender transmits, receive will keep waiting. If the sender transmits more than this, the returned buffer is limited to length bytes.

timeout

ตัวเลือกเสริม: ระบุระยะเวลาเป็นมิลลิวินาที receive เฉพาะในกรณีที่คิวว่างเท่านั้น ค่าเริ่มต้นคือ 300000 (300 วินาที)

รหัสสำหรับหุ่นยนต์ตัวที่ 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 ลงทะเบียนฟังก์ชันที่จะถูกเรียกใช้เมื่อใดก็ตามที่ V5 Brain ได้รับข้อมูลอนุกรม

Usage:
link.received(callback)

พารามิเตอร์

คำอธิบาย

callback

A previously defined function that executes when the V5 Brain receives serial data.

ได้รับ received ฟังก์ชัน โดยแต่ละฟังก์ชันจะมีอาร์กิวเมนต์ 2 ตัว:

รูปแบบการเรียกใช้ฟังก์ชัน Callback:
callback(buffer, length)

การโต้แย้ง

คำอธิบาย

buffer

อาร์เรย์ไบต์ bytearray อาจมีขนาดใหญ่กว่าข้อความที่ได้รับ

length

จำนวนไบต์ที่ถูกต้องที่ได้รับในบัฟเฟอร์ buffer สำหรับฟังก์ชันเรียกกลับนี้ ใช้บัฟเฟอร์ 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)

previous

ข้อความลิงก์

next

คอนโซล

On this page
  • การแนะนำ
  • สร้างลิงก์
  • วิธีการที่มีอยู่
    • is_linked
    • send
    • receive
    • received
Innovation First, International
​

VEX และ VEX Robotics เป็นเครื่องหมายการค้าหรือเครื่องหมายบริการของ Innovation First, Inc. ลิขสิทธิ์ ©2026สงวนลิขสิทธิ์ทุกประการ VEX Robotics, Inc. เป็นบริษัทในเครือของ Innovation First International, Inc. ชื่อผลิตภัณฑ์/เครื่องหมายอื่นๆ ทั้งหมดเป็นทรัพย์สินของเจ้าของที่เกี่ยวข้อง สิทธิบัตรและ/หรือสิทธิบัตรที่อยู่ระหว่างการยื่นขอ - innovationfirst.com/patents
นโยบายความเป็นส่วนตัวของเว็บไซต์ / ข้อกำหนดการใช้งานเว็บไซต์ / นโยบายคุกกี้ / นโยบายความเป็นส่วนตัวของซอฟต์แวร์

เยี่ยมชมเพจ Facebook ของ VEX Robotics เยี่ยมชมหน้าทวิตเตอร์ของ VEX Robotics เยี่ยมชมเพจ Instagram ของ VEX Robotics ได้ที่นี่ เยี่ยมชมเพจ YouTube ของ VEX Robotics
แบบฟอร์มแสดงความคิดเห็น VEX API

เราให้ความสำคัญกับข้อเสนอแนะของคุณ! โปรดใช้แบบฟอร์มนี้เพื่อแบ่งปันข้อเสนอแนะ คำชม หรือรายงานข้อผิดพลาดเกี่ยวกับ VEX API ข้อมูลของคุณช่วยเราปรับปรุงเอกสารประกอบการใช้งาน VEX API ให้ดียิ่งขึ้น

​

หากคุณประสบปัญหาทางเทคนิคหรือต้องการความช่วยเหลือจากฝ่ายบริการลูกค้า โปรดไปที่ support.vex.com.

  • Send Happy Feedback
  • Send Sad Feedback

หมายเหตุ: URL ปัจจุบันจะถูกส่งไปพร้อมกับข้อความของคุณ

เมื่อคุณระบุที่อยู่อีเมล คุณยินยอมให้ VEX ส่งอีเมลถึงคุณหากเรามีข้อสงสัยเกี่ยวกับข้อเสนอแนะของคุณ
นโยบายความเป็นส่วนตัว >
โปรดให้ข้อเสนอแนะของคุณ ส่งข้อเสนอแนะเรียบร้อยแล้ว!
Choose Which VEX IQ Generation to View

VEX IQ (1st gen)

VEX IQ (2nd gen)