सीरियललिंक#
परिचय#
सीरियललिंक दो EXP ब्रेन को VEX लिंक पर रॉ सीरियल डेटा भेजकर और प्राप्त करके संवाद करने की सुविधा देता है। लेबल किए गए संदेशों के बजाय, सीरियललिंक एक बफर (एक स्ट्रिंग या एक बाइटएरे) संचारित करता है। यह इसे उन्नत रोबोट-टू-रोबोट संचार के लिए उपयोगी बनाता है, जैसे सेंसर डेटा स्ट्रीमिंग, कस्टम पैकेट प्रारूप भेजना, या बाइट्स के आधार पर अपना स्वयं का प्रोटोकॉल बनाना।
send के प्रत्येक उपयोग से लिंक्ड EXP ब्रेन के रिसीव बफर में डेटा जुड़ जाता है। डेटा को उसके आने के क्रम में पढ़ा जाता है (फर्स्ट-इन, फर्स्ट-आउट)। यदि दूसरे EXP ब्रेन द्वारा receive का उपयोग करने से पहले कई बफर भेजे जाते हैं, तो वे कतार में बने रहते हैं और भेजे जाने के क्रम में एक-एक करके वापस भेजे जाते हैं। क्योंकि डेटा कतार में लग सकता है, प्रत्येक लूप में बार-बार बड़े बफर भेजने से बैकलॉग हो सकता है; समय-संवेदनशील लिंक के लिए, केवल उतना ही भेजें जितना आवश्यक हो।
महत्वपूर्ण: दोनों रोबोटों को एक ही समय में SerialLink का उपयोग करने वाले प्रोजेक्ट चलाने होंगे, अन्यथा कोई डेटा भेजा/प्राप्त नहीं किया जाएगा।
इस पृष्ठ पर उदाहरण के तौर SerialLink नाम के रूप में link का उपयोग किया गया है। आवश्यकतानुसार इसे अपने स्वयं के कॉन्फ़िगर किए गए नाम से बदलें।
नीचे उपलब्ध विधियों की सूची दी गई है:
is_linked– यह बताता है कि क्या यह ब्रेन अपने युग्मित ब्रेन से सक्रिय रूप से जुड़ा हुआ है।send– युग्मित ब्रेन को एक छोटा संदेश (वैकल्पिक int/float डेटा के साथ) भेजता है।receive– अगले आने वाले संदेश की प्रतीक्षा करता है और उसे लौटाता है।received– जब भी कोई नया संदेश प्राप्त होता है, तो कॉल करने के लिए एक फ़ंक्शन पंजीकृत करता है।
एक लिंक बनाएं#
दोनों 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)
उपलब्ध विधियाँ#
एक बार जब आपका सीरियललिंक सेटअप हो जाए और ब्रेन आपस में जुड़ जाएं, तो आप नीचे दिए गए तरीकों का उपयोग करके रॉ सीरियल डेटा भेज सकते हैं, प्राप्त कर सकते हैं और उस पर प्रतिक्रिया दे सकते हैं।
is_linked#
is_linked विधि यह लौटाती है कि क्या सीरियललिंक पर EXP ब्रेन एक दूसरे के साथ युग्मित हैं।
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 पैरामीटर द्वारा निर्दिष्ट बाइट्स की संख्या एकत्र कर ली हो। इसलिए, यदि आपका कोड हमेशा receive एक निश्चित lengthके साथ कॉल करता है, तो आपको यह सुनिश्चित करना चाहिए कि send के साथ भेजा गया प्रत्येक संदेश ठीक उतने ही बाइट्स का हो (अक्सर पैडिंग द्वारा) ताकि रिसीवर अतिरिक्त बाइट्स की प्रतीक्षा में अवरुद्ध न हो।
अगर आप सटीक बाइट गणना पर निर्भर नहीं रहना चाहते हैं, तो इसके बजाय received का उपयोग करें; यह आपको विशिष्ट लंबाई की आवश्यकता के बिना जो भी बाइट्स उपलब्ध हैं उन्हें संभालने देता है।
Usage:
send(buffer)
पैरामीटर |
विवरण |
|---|---|
|
दूसरे लिंक किए गए EXP ब्रेन को भेजा जाने वाला डेटा। यह एक |
रोबोट 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 ब्रेन से FIFO क्रम में सीरियल डेटा लौटाता है (सबसे पुराना अपठित डेटा पहले)।
क्योंकि receive सटीक बाइट संख्या की प्रतीक्षा करता है, इसलिए send का उपयोग करते समय निश्चित लंबाई के संदेशों में अक्सर पैडिंग की आवश्यकता होती है ताकि प्रत्येक संदेश अपेक्षित आकार तक पहुँच सके। यदि प्रेषक length बाइट्स से कम भेजता है, तो receive शेष बाइट्स की प्रतीक्षा करता रहेगा और कुछ भी वापस नहीं करेगा।
यदि कतार खाली है और आप timeout, receive मिलीसेकंड तक नए डेटा की प्रतीक्षा करता है। यदि उस समयावधि में कुछ भी प्राप्त नहीं होता है, तो यह Noneलौटाता है। इसके बाद भेजा गया कोई भी डेटा बाद में पढ़ने के लिए कतार में बना रहता है।
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 एक फ़ंक्शन को पंजीकृत करता है जिसे EXP ब्रेन द्वारा सीरियल डेटा प्राप्त होने पर कॉल किया जाएगा।
Usage:
link.received(callback)
पैरामीटर |
विवरण |
|---|---|
|
एक पूर्व-परिभाषित फ़ंक्शन जो EXP ब्रेन द्वारा सीरियल डेटा प्राप्त होने पर निष्पादित होता है। |
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)