-
Notifications
You must be signed in to change notification settings - Fork 3
/
send_HPGL_wifi.py
49 lines (40 loc) · 1.39 KB
/
send_HPGL_wifi.py
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
#this script is written for python3
import tkinter as tk
from tkinter import filedialog
import socket
WIFI_INPUT_BUFFER_SIZE_ESP32 = 255 #must be the same as in esp32 plotterfeeder code
#helper function to split string in chunks
def chunks(lst, n):
for i in range(0, len(lst), n):
yield lst[i:i+n]
###start main code###
filename = "positions_test.txt"
# #query filename to send via gui, comment out to use manual name above
# root = tk.Tk()
# root.withdraw()
# filename = filedialog.askopenfilename()
# root.destroy()
#load data
send_data =""
f= open(filename,"rb")
send_data =f.read()
f.close()
#print(send_data)
#connect to plotter
sock = socket.socket()
host = "harryplotter" #Plotter Server Hostname or IP in local network
port = 1337 #Plotter Server Port
sock.connect((host, port))#connect to plotter, TODO: this hangs on "no reply"
#send data in chunks
print("Sending data...")
for chunk in chunks(send_data, (WIFI_INPUT_BUFFER_SIZE_ESP32-1)):#dont forget added null terminator
print(chunk.decode("utf-8")) #print data that was sent
sock.send(chunk)
reply = sock.recv(2) #TODO: this hangs on "no reply"
#print(reply.decode("utf-8"))
if (reply != b'OK'):#we expect an "OK" when the data has been sent to plotter
sock.close()
print("Error: Did not receive 'OK' after data chunk. Aborting.")
break
sock.close()
print("Done.")