import socket #调用python的socket通讯函数库import json #调用python的json字符串函数库robot_ip = "127.0.0.1" #这只是自定义的一个变量,127.0.0.1为仿真的IP。依据实际设置修改rmi_port = 16001 #这只是自定义的一个变量,16001是RMI的固定值端口号s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #这是python的socket函数指令,以S替代此指令,以避免过长输入try: #连接机器人 s.connect ((robot_ip, rmi_port)) #括号中的变量就是上述定义的值 print("连接成功!") #这一步成功仅是socket通讯建立,实际还没有发字符串给机器人. #这一行只是验证,能运行到这一行,说明上一句也运行结束了,说明通讯成功。 handshake = json.dumps({"communication":"FRC_CONNECT"}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是进行通讯建立 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-连接:",resp)except Exception as e: #Python的格式要求,有了TRY,必须跟except或者finally print("连接失败!")s.close()rmi_port = 16002 #上述执行完成后,机器人反馈了16002端口号,所以后面交互得更新端口号16002s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #由于上面已经执行了s.close,所以此处必须再次定义。try: #设置机器人倍率 s.connect ((robot_ip, rmi_port)) #括号中的变量就是上述定义的值 handshake = json.dumps({"Command":"FRC_SetOverRide","Value":66}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是修改机器人倍率为66% #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-倍率:",resp) #程序中止-程序运行中异常停止时,中止程序,以从轨迹开始运动 handshake = json.dumps({"Command":"FRC_Abort"}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是中止程序 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-程序中止:",resp) #初始化远程运动数据包 handshake = json.dumps({"Command":"FRC_Initialize"}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是初始化,也是启动机器人 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-初始化:",resp) #发送点位1-直线运动 handshake = json.dumps({"Instruction":'FRC_LinearMotion','Sequenceid':1,'Configuration': {'UToolNumber': 1, 'UFrameNumber': 0, 'Front': 1, 'Up': 1, "Left": 0, "Flip": 0, "Turn4": 0, "Turn5": 0, "Turn6": 0}, "Position": {"X": 1000.000, "Y": 0.000, "Z": 1050.000, "W": -180.000, "P": 0.000, "R": 0.000, "Ext1": 0.000, "Ext2": 0.000, "Ext3": 0.000}, 'SpeedType':'mmSec','Speed':500,'TermType':'FINE'}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是发送点位 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-P1:",resp) #发送点位2-直线运动 handshake = json.dumps({"Instruction":'FRC_LinearMotion','Sequenceid':2,'Configuration': {'UToolNumber': 1, 'UFrameNumber': 0, 'Front': 1, 'Up': 1, "Left": 0, "Flip": 0, "Turn4": 0, "Turn5": 0, "Turn6": 0}, "Position": {"X": 1500.000, "Y": 100, "Z": 900.000, "W": -180.000, "P": 0.000, "R": 0.000, "Ext1": 0.000, "Ext2": 0.000, "Ext3": 0.000}, 'SpeedType':'mmSec','Speed':500,'TermType':'FINE'}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是发送点位 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-P2:",resp) #发送点位3-直线运动 handshake = json.dumps({"Instruction":'FRC_LinearMotion','Sequenceid':3,'Configuration': {'UToolNumber': 1, 'UFrameNumber': 0, 'Front': 1, 'Up': 1, "Left": 0, "Flip": 0, "Turn4": 0, "Turn5": 0, "Turn6": 0}, "Position": {"X": 1000.000, "Y": -100, "Z": 500.000, "W": -180.000, "P": 0.000, "R": 0.000, "Ext1": 0.000, "Ext2": 0.000, "Ext3": 0.000}, 'SpeedType':'mmSec','Speed':500,'TermType':'FINE'}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是发送点位 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-P3:",resp) #发送点位4-关节运动 handshake = json.dumps({"Instruction":'FRC_JointMotion','Sequenceid':4,'Configuration': {'UToolNumber': 1, 'UFrameNumber': 0, 'Front': 1, 'Up': 1, "Left": 0, "Flip": 0, "Turn4": 0, "Turn5": 0, "Turn6": 0}, "Position": {"X": 1592.000, "Y": 0, "Z": 1085.000, "W": -180.000, "P": 0.000, "R": 0.000, "Ext1": 0.000, "Ext2": 0.000, "Ext3": 0.000}, 'SpeedType':'Percent','Speed':50,'TermType':'FINE'}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是发送点位 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-P4:",resp) #断开连接 handshake = json.dumps({"Communication":"FRC_DISCONNECT"}) + "\r\n" #{}内容是机器人RMI手册固定指令,此处是断开连接 #json.dumps = 变成字符串 #\r\n这是格式要求,必须以此结尾,机器人才知道发送结束 s.sendall(handshake.encode('utf-8')) #发送数据,转成网络能传输的字节格式 resp = s.recv (1024).decode ('utf-8').strip () #接收机器人发回来的消息,decode = 把字节变回文字,strip() = 去掉多余空格换行 print("机器人返回-断开连接:",resp)finally: #Python的格式要求,有了TRY,必须跟except或者finally s.close()