获取UR5机械臂TCP坐标时报错 struct.error: unpack requires a buffer of 48 byte

问题:

使用如下代码获取UR5机器人TCP坐标时报错“ position = struct.unpack(‘!6d’, data[444:492])
struct.error: unpack requires a buffer of 48 bytes”

import numpy as np
import socket
import time
import struct
import util
import rtde
from time import sleep

HOST = '192.168.1.2'
PORT = 30001

robot = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
robot.connect((HOST, PORT))

def get_current_tcp():
    tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    tcp_socket.connect((HOST, PORT))
    data = tcp_socket.recv(1108)
    print(len(data))
    position = struct.unpack('!6d', data[444:492])
    tcp_socket.close()
    return np.asarray(position)

原因:

端口号错了,端口号写成了:PORT = 30001

解决方法:

将端口号修改为30003,即PORT = 30003,即可。

你可能感兴趣的:(tcp/ip,网络协议,网络)