-
Notifications
You must be signed in to change notification settings - Fork 5
/
chunkSum.py
71 lines (50 loc) · 1.77 KB
/
chunkSum.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
#!/usr/bin/python
from struct import *
import sys
import os
offsetFile = 0;
#Open our file
gcodeFile = "testfiles/test.gcode"
gcode = os.fdopen(os.open(gcodeFile,os.O_RDONLY))
def errprint(message):
sys.stderr.write(message + "\n");
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));
# sys.stderr.write("Checksum for chunk is: " + str(checkData) + "\n")
errprint("Checksum for chunk is: " + str(checkData));
# Concatenate the two byte arrays and return
return dataStream + ckStream
def readFile():
# 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;
# Loop until we run out of file
while True:
# Read our first chunk of file
chunkOfFile = readChunk(fileOffset)
serialDataChunk = generateSerialChunk(chunkFile)
# In the main program, serial handling code and upload code goes here
sys.stdout.write(serialDataChunk)
# Check to see if the chunk is smaller than 10236, if so, break.
if len(chunkFile) != 10236:
break
# Update our offset point
fileOffset = fileOffset + 10236
# Let the user know how far we are through the file
errprint("Processed {:0.2f}% of file.".format((fileOffset / float(fileLen) * 100)))
errprint("Finished")
readFile()