SerialLink#
مقدمة#
تتيح تقنية SerialLink لوحدتي تحكم EXP Brains التواصل عبر إرسال واستقبال بيانات تسلسلية خام باستخدام 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 لإنشاء قناة اتصال بينهما - إما لاسلكيًا باستخدام أجهزة راديو الروبوت V5 أو من خلال كابل ذكي سلكي - حتى تتمكن الروبوتات من إرسال بيانات تسلسلية خام إلى بعضها البعض.
SerialLink(smartport, name, linktype, wired)
المعلمة |
وصف |
|---|---|
|
المنفذ الذكي المستخدم لهذا الاتصال هو المنفذ الذي يتصل به جهاز راديو الروبوت V5 (لاسلكيًا) أو المنفذ الذي يتم توصيل الكابل الذكي به (سلكيًا). يُكتب على النحو التالي: |
|
الاسم الفريد الذي يُطلق على هذا الرابط كسلسلة نصية. |
|
الدور الذي سيستخدمه هذا الدماغ لهذا الزوج من الروابط. يجب أن يتضمن كل زوج مديرًا واحدًا وعاملًا واحدًا:
|
|
اختياري. ما إذا كان هذا الاتصال سلكيًا أم لاسلكيًا:
|
كود للروبوت 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 الخاص بك وربط وحدات المعالجة، يمكنك استخدام الطرق أدناه لإرسال واستقبال والرد على البيانات التسلسلية الخام.
is_linked#
is_linked تُرجع هذه الطريقة ما إذا كانت وحدات EXP Brains الموجودة على SerialLink مقترنة ببعضها البعض.
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 Brain الأخرى المرتبطة.
When reading that data with receive, receive will only return once it has collected the number of bytes specified by its length parameter. Because of that, if your code always calls receive with a fixed length, you should ensure each message sent with send is exactly that many bytes (often by padding) so the receiver doesn’t block waiting for additional bytes.
إذا كنت لا تريد الاعتماد على عدد البايتات الدقيق، فاستخدم 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 (أقدم البيانات غير المقروءة أولاً).
Because receive waits for an exact byte count, fixed-length messaging often requires padding when using send so each message reaches the expected size. If the sender transmits fewer than length bytes, receive will continue waiting for the remaining bytes and not return anything.
If the queue is empty and you provide a timeout, receive waits up to that many milliseconds for new data. If nothing arrives in that window, it returns None. Any data sent afterward remains queued to be read later.
Usage:
receive(length, timeout)
حدود |
وصف |
|---|---|
|
عدد البايتات المطلوب استقبالها. |
|
اختياري. مدة الانتظار بالمللي ثانية |
كود للروبوت 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)
حدود |
وصف |
|---|---|
|
A previously defined function that executes when the EXP Brain receives serial data. |
تم استلام 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)