-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrm_direct_connection_plaintext_sdk.py
More file actions
55 lines (37 loc) · 1.52 KB
/
rm_direct_connection_plaintext_sdk.py
File metadata and controls
55 lines (37 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import socket
import sys
# Follow the instruction: https://robomaster-dev.readthedocs.io/en/latest/text_sdk/connection.html
# In direct connection mode, the default IP address of the robot is 192.168.2.1 and the control command port is port 40923.
host = "192.168.2.1"
port = 40923
def main():
address = (host, int(port))
# Establish a TCP connection with the control command port of the robot.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print("Connecting...")
s.connect(address)
print("Connected!")
while True:
# Wait for the user to enter control commands.
msg = input(">>> please input SDK cmd: ")
# When the user enters Q or q, exit the current program.
if msg.upper() == 'Q':
break
# Add the ending character.
msg += ';'
# Send control commands to the robot.
s.send(msg.encode('utf-8'))
try:
# Wait for the robot to return the execution result.
buf = s.recv(1024)
print(buf.decode('utf-8'))
except socket.error as e:
print("Error receiving :", e)
sys.exit(1)
if not len(buf):
break
# Disconnect the port connection.
s.shutdown(socket.SHUT_WR)
s.close()
if __name__ == '__main__':
main()