Question

For senior design, I have labview sending the encoder positions to another python script that take those and performs forward kinematics and implements the haptic boundaries. I need to take that data and make a GUI. But I'm getting this error. Any thoughts?

-GUIserver.py

# Echo server program
import socket
import pickle 
import Tkinter


HOST = 'localhost'                 # Symbolic name meaning all available interfaces
PORT = 5000              # Arbitrary non-privileged port
addr = (HOST,PORT)

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr
data = conn.recv(1024)
print(data)
conn.sendall("Welcome Haptic Client: Please send bounrary array via encoded pickle")
data = conn.recv(1024)
boundaryarray = pickle.loads(data)
print repr(boundaryarray)
conn.sendall("GUI loading")


root = Tkinter.Tk()
root.title("Haptic GUI")
w = Tkinter.Canvas(root, width=640, height=480)
w.pack()

#For loops to write all boundary data 
for x,y in boundaryarray:
    print x
previousx = x
print y
previousy = y
firstx = x
firsty = y
del boundaryarray[0]
break
for x,y in boundaryarray:
print x
print y
print str(previousx) + "x previous"
print str(previousy) + " y previous"
w.create_line(previousx, previousy, x, y, fill="red")
previousx = x
previousy = y

w.create_line(boundaryarray[-1], boundaryarray[-1], firstx, firsty, fill="red")


conn.sendall("The boundary has been plotted, read to take pen data. Send me the current point and the point before that only if the pen array is longer then 2 element")
conn.sendall("To shut down the GUI and socket, send GUI SERVER OFF")

while True:
data = conn.recv(1024)
if data == "GUI SERVER OFF":
    break
penarray = pickle.loads(str(data))
for x,y in boundaryarray:
    firstx = x
    firsty = y
    del boundaryarray[0]
    break
for x,y in boundaryarray:
    secondx = x
    secondy = y
w.create_line(firstx, firsty, secondy, secondx, fill="red")
w.canvas.draw()

conn.sendall("plotted")


root.mainloop()

-hapticclient.py

import socket
import pickle
import random

HOST = 'localhost'    # The remote host
PORT = 5000           # The same port as used by the server
addr = (HOST,PORT)
array = [(6.0, 6.0000000674792586), (6.8888888888888893, 8.514157444218835), (7.7777777777777777, 9.3259176771323915), (8.6666666666666661, 9.7712361663282543), (9.5555555555555554, 9.9752319599996255), (10.444444444444445, 9.9752319599996255), (11.333333333333332, 9.7712361663282543), (12.222222222222221, 9.3259176771323933), (13.111111111111111, 8.5141574442188368), (14.0, 6.0000000674792586), (6.0, 5.9999999325207414), (6.8888888888888893, 3.4858425557811645), (7.7777777777777777, 2.6740823228676081), (8.6666666666666661, 2.2287638336717466), (9.5555555555555554, 2.0247680400003749), (10.444444444444445, 2.0247680400003749), (11.333333333333332, 2.2287638336717466), (12.222222222222221, 2.6740823228676072), (13.111111111111111, 3.4858425557811636), (14.0, 5.9999999325207414)]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, GUI. This is haptic')
data = s.recv(1024)
if data == "Welcome Haptic Client: Please send bounrary array via encoded pickle":
#We serialize the data with picle here 
s.sendall(pickle.dumps(array))
penarray = []
data = s.recv(1024)
if data == "The boundary has been plotted, read to take pen data. Send me the current point and the point before that only if the pen array is longer then 2 element":

while True:
    pen_array_to_send = []
    location = (random.randint(10, 100), random.randint(10, 100))
    print location
    penarray.append(location)
    if len(penarray) > 2:
        pen_array_to_send.append(penarray[-1])
        pen_array_to_send.append(penarray[-2])
        #s.sendall(pickle.dumps(pen_array_to_send)) 
        print pen_array_to_send
        data = s.recv(1024)

    print 'Received', repr(data)



s.close()
print 'Received', repr(data)
Was it helpful?

Solution

The EOFError occurs if you do not pass the whole string to pickle but only parts of it. This can happen when you read incomplete strings from the socket with conn.recv(1024).

I would change

data = conn.recv(1024)
boundaryarray = pickle.loads(data)

because conn.recv(1024) receives 0 to 1024 bytes. I would change it to

f = conn.makefile("r")
boundaryarray = pickle.load(f)

Because it reads all that pickle wants, not more not less.

And if you dump something use

f = conn.makefile("w")
boundaryarray = pickle.dump(...)

with the file.

I would also recomment looking at this: Python implementing simple web data storage

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top