-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuploader.py
221 lines (167 loc) · 5.58 KB
/
uploader.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/python
from struct import *
import sys
import os
import serial;
import io;
import time;
offsetFile = 0;
# Hardcode parameters for now
# Parameters set for XYZ from watching the traffic
DEVICE='/dev/ttyACM0'
TIMEOUT = 0.1 # We want to block the program until we get a response back
BAUDRATE = 115200
gcodeFile = "testfiles/ruler.gcode"
DEBUGMODE = 1
SERIALENABLE = 1
#Known Commands
MACHINE_INFO = 'XYZ_@3D:'
MACHINE_LIFE = MACHINE_INFO + '5'
EXTRUDER1_INFO = MACHINE_INFO + '6'
EXTRUDER2_INFO = MACHINE_INFO + '7'
STATUS_INFO = MACHINE_INFO + '8'
SEND_FIRMWARE = MACHINE_INFO + '3'
SEND_FILE = MACHINE_INFO + '4'
#ReturnedStrings:
#SEND_FILE_RESP1 = "OFFLINE_OK" + "\n"
#Open our file
gcode = os.fdopen(os.open(gcodeFile,os.O_RDONLY))
#Open serial port
if SERIALENABLE == 1:
sport = serial.Serial(DEVICE,baudrate=BAUDRATE,timeout=TIMEOUT,xonxoff=1,bytesize=serial.EIGHTBITS,parity=serial.PARITY_NONE);
# Need a raw IO for sending data
rsio = io.BufferedRWPair(sport,sport)
# For our return statements and such, note that the EOL is just Linefeed character (0x0a)
sio = io.TextIOWrapper(rsio,newline='\n')
def errprint(message):
if DEBUGMODE >= 1:
sys.stderr.write(message + "\n");
def writeData(data):
errprint("Sending: " + data)
if SERIALENABLE == 1:
sio.write(data)
sio.flush()
def rawWriteData(data):
errprint("RAW-Sending: " + data)
if SERIALENABLE == 1:
rsio.write(data)
rsio.flush()
def readData():
time.sleep(1)
returnData = ''
if SERIALENABLE == 1:
returnData = sio.read()
else:
returnData = "Serial Disabled"
errprint("Received: " + returnData)
return returnData;
def checkReturnedData(expectedData,returnedData):
errprint("Is expected: " + expectedData + " equal to " + returnedData + " ?")
if expectedData == returnedData:
errprint("Returned Data expected!")
return 0
else:
errprint("Returned Data not expected!")
return 1
def checkSum(st):
# Generate checksum for the given data by adding up all the values
return sum(ord(c) for c in st)
def readChunk(fileOffset):
#Seek to the given location and read 10236 bytes of data
gcode.seek(fileOffset)
dataChunk = gcode.read(10236);
return dataChunk;
def generateSerialChunk(byteChunk):
# Convert the string into a byte array
dataStream = bytearray(byteChunk,"ascii");
# Generate our checksum
checkData = checkSum(byteChunk)
# Convert the integer into a byte array
ckStream = bytearray(pack('>i',checkData));
#Check to see if the length is less than 10236. If so, add our special end value (0x86)
# if len(byteChunk) < 10236 :
# errprint("Last chunk of data... added EOF marker")
# ckStream = ckStream + bytearray.fromhex('86')
errprint("Checksum for chunk is: " + str(checkData));
# Concatenate the two byte arrays and return
return dataStream + ckStream
# This sends our required? header
def sendGCodeHeader(fileName,fileLen):
# Write out our predetermined string
#writeData(unicode(SEND_FILE))
#readData()
#flush buffers
#sio.flush()
rawWriteData("M1:{:s},{:d},1.3.49,EE1_OK,EE2_OK".format(fileName,fileLen))
#time.sleep(5)
returnData = readData();
if checkReturnedData("OFFLINE_OK",returnData.strip()) == 1:
errprint("Data doesn't match expected returned?!")
#sys.exit("Dying because things don't match up!")
#time.sleep(1)
# This sends the GCode chunk and makes sure we get a OK response back.
def sendSerialChunk(serialDataChunk):
# Write our 10K chunk out to the serial port
rawWriteData(serialDataChunk);
rsio.flush()
# See if the response we get back is good
# statusData = readData()
def readFile():
global DEBUGMODE
# Set our fileOffset so we know where we are in the file
fileOffset = 0;
# Get file length for percentage calculator
fileLen = os.stat(gcodeFile).st_size;
sendGCodeHeader("MyTest",fileLen)
# Loop until we run out of file
while True:
# Read our first chunk of file
chunkOfFile = readChunk(fileOffset)
serialDataChunk = generateSerialChunk(chunkOfFile)
# In the main program, serial handling code and upload code goes here
#sys.stdout.write(serialDataChunk)
DEBUGMODE = 1
sendSerialChunk(serialDataChunk)
DEBUGMODE = 1
returnedData = readData();
if checkReturnedData("CheckSumOK",returnedData.strip()) == 1:
errprint("Data doesn't match expected returned?!")
#sys.exit("Dying because of unexpected data!")
# Check to see if the chunk is smaller than 10236, if so break because we're done uploading.
if len(chunkOfFile) != 10236:
break
# Update our offset point
fileOffset = fileOffset + 10236
# Let the user know how far we are through the file
errprint("Sent {:0.2f}% of file.".format((fileOffset / float(fileLen) * 100)))
rsio.flush()
sport.close()
errprint("Finished")
def sendfileToPrinter():
# Notify our printer that we want to send it a file.
writeData(unicode(SEND_FILE));
returnedData = readData();
if checkReturnedData("OFFLINE_OK",returnedData.strip()) == 1:
errprint("Data doesn't match expected returned?!")
sys.exit("Dying because of unexpected returned data!")
readFile()
# Check to see if our serial port is open...
#print sport.isOpen();
writeData(unicode(MACHINE_INFO));
readData()
#print "Sleeping 5 seconds..."
#time.sleep(5)
# Returns two numbers?
writeData(unicode(MACHINE_LIFE));
readData()
# Returns the following infor:
# unknown, unknown, unknown, unknown, Filament_Cart_Length, Filament_Remaining_Length, Extruder_Temp, Bed_Temp,unknown, unknown,unknown, unknown
writeData(unicode(EXTRUDER1_INFO));
readData()
#Returns Status Info
writeData(unicode(STATUS_INFO));
readData()
#print "Sleeping 5 seconds..."
#time.sleep(5)
#readFile()
sendfileToPrinter()