After discussions, It became apparent that the I2C might not be great for data transfer (as it freezes up the Arduino to perform the transfer. So I'm sticking with the socket code. Question now is how do I fix the code below.
My system was recently updated to python 3.6.5 to take advantage of some of the newer libraries but it broke the transfer code which was written for python 2.7.0. As I didn't write it and it isn't documented, I have to start at square one to get it to work.
Below is the code for the client (I added the comments)
import socket socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server = ('192.168.1.130',5000) socket.connect(server) print "Connected to: ",server running = True while running: socket.recv(1024) f = open(filename,"rb") #raw_cmd = "put "+re.search(r'\w+\.?\w+?$',filename).group(0) raw_cmd = "put "+ filename # Put is a server side command to place file somewhere on computer socket.send(raw_cmd) # send command chunk = f.read(1024) # start pulling chunks from file to send while(chunk): # Loop while data is available in file socket.send(chunk) # Send chunk from file if socket.recv(1024) != "ACK": # If package was not received, confirmation is receiving 'ACK', print below print("Failed transfer") chunk = f.read(1024) # Grab next chunk from file for sending f.close() # Close file after sending all chunks socket.send('EOF') # 'EOF' is a kill command for server loop running = False socket.send("ACK") # Send 'ACK' to tell server if connection still exists
and here is the server code
import socket import os serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) serversocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #serversocket.bind(('localhost',4040)) # change localhost and 4040 to IP and PORT# of server #serversocket.bind(('192.168.1.121',5000)) serversocket.bind(('192.168.1.166',5000)) serversocket.listen(5) while True: c, addr = serversocket.accept() print('Connected to ', addr) running = True while running: c.send(os.getcwd()) # client needs to get the current directory, so we send it here raw_msg = c.recv(1024) if not raw_msg: print("Client disconnected") c.close() break msg = raw_msg.split() cmd = msg print('Received cmd ', cmd) if cmd == "ls": lsDir = os.listdir(os.getcwd()) #Check for empty directory if not lsDir: string = "*empty*" else: string = '\n'.join(lsDir) print("string: ",string) c.send(string) elif cmd == "cd": target = msg os.chdir(target) elif cmd == "mkdir": target = msg newDir = os.getcwd() + "/" + target os.mkdir(newDir) elif cmd == "get": target = msg f = open(target,"rb"); chunk = f.read(1024) while(chunk): #print("sending stuff ",sys.getsizeof(chunk)) c.send(chunk) if c.recv(1024) != "ACK": print "Failed transfer" chunk = f.read(1024) f.close() c.send('EOF') elif cmd == "put": target = msg f = open(target,"wb"); print("file ok") chunk = c.recv(1024) while(chunk != 'EOF'): print("got stuff") f.write(chunk) c.send("ACK") chunk = c.recv(1024) f.close() elif cmd == "terminate": c.close() running = False else: print "Received unkown cmd: "+cmd if c.recv(1024) != "ACK": print "Detected desynchronisation"
Code is full of system commands and calls and other details that I'm busy reading up on. any help explaining the code would be grateful.