Skip to content

Instantly share code, notes, and snippets.

@tcr
Last active August 24, 2020 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcr/3246f420dc3dc35306815596d69def26 to your computer and use it in GitHub Desktop.
Save tcr/3246f420dc3dc35306815596d69def26 to your computer and use it in GitHub Desktop.
Monoprice Cadet Wifi-only Upload Script in Python
# MonoPrice Cadet script for uploading .gcode files
# and listing the contents of the SD card
#
# Usage: python mpcadet.py 192.168.1.X
#
# Uploads the local CADET.gcode file in the same directory
# as your script to the device and then kicks off a build.
#
# A limited number of GCODE commands seem possible:
# https://marlinfw.org/docs/gcode/M023.html
import socket
import binascii
import time
import math
import sys, os
import codecs
# http://gist.github.com/eaydin
def AddToCRC(b, crc):
if (b < 0):
b += 256
for i in range(8):
odd = ((b^crc) & 1) == 1
crc >>= 1
b >>= 1
if (odd):
crc ^= 0x8C # this means crc ^= 140
return crc
# http://gist.github.com/eaydin
def calc_crc8_maxim(incoming):
# convert to bytearray
if sys.version_info[0] == 3:
hex_data = codecs.decode(incoming, "hex_codec")
else:
hex_data = incoming.decode("hex")
msg = bytearray(hex_data)
check = 0
for i in msg:
check = AddToCRC(i, check)
return check
IP_ADDR_FIRST_ARGUMENT = sys.argv[1]
CADET_ADDR_UDP = (IP_ADDR_FIRST_ARGUMENT, 5060)
CADET_ADDR_TCP = (IP_ADDR_FIRST_ARGUMENT, 5050)
def out_frame(f):
check = calc_crc8_maxim(f.hex())
return bytes.fromhex("eb") + bytes.fromhex("{:02x}".format(len(f))) + f + bytes.fromhex("{:02x}".format(check)) + bytes.fromhex("0a")
def in_frame(frame):
return frame[2:-2]
BUFFER_SIZE = 1024*64
# UDP socket
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind(('', 9090))
def udp_send(m):
UDPServerSocket.sendto(out_frame(m), CADET_ADDR_UDP)
def udp_recv():
return in_frame(UDPServerSocket.recvfrom(BUFFER_SIZE)[0])
def expect(res, compare):
if res != compare:
print('error:', res, 'expected:', compare)
raise res
return res
# TCP socket
print('Connecting to TCP...')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(CADET_ADDR_TCP)
print('Connected.')
def tcp_send(m):
s.send(out_frame(m))
def tcp_recv():
return in_frame(s.recv(BUFFER_SIZE))
# Cadet Commands
def cadet_version():
udp_send(b'V')
return udp_recv()
def cadet_idk1():
udp_send(b'0')
return udp_recv()
# TT0:31 T1:0 B:0 C:0Q
def cadet_idk2():
udp_send(b'T')
return udp_recv()
# SF P:0% B:0 C:0
def cadet_idk3():
udp_send(b'S')
return udp_recv()
def cadet_list():
udp_send(b'B')
while True:
cmd = udp_recv()
if cmd == b'B0':
break
while True:
cmd = udp_recv()
if cmd == b'B1':
break
# File entry
print(cmd)
def cadet_upload(file):
tcp_send(b'W/CADET.GCO\x00')
print(tcp_recv())
with open(file, 'rb') as f:
size = os.path.getsize(file)
parts = math.ceil(size / 250)
i = 0
while True:
piece = f.read(250)
if not piece:
break
print('Uploading piece {}/{}...'.format(i + 1, parts))
tcp_send(b'D' + piece)
expect(tcp_recv(), b'D0')
i += 1
tcp_send(b'E')
print(expect(tcp_recv(), b'E0'))
def cadet_print(file):
udp_send(b'GM23 ' + file.encode() + b'\x00')
print(expect(udp_recv(), b'G0'))
udp_send(b'GM24\x00')
print(expect(udp_recv(), b'G0'))
print(cadet_version())
cadet_list()
# Poloprint loop
# while True:
# print(cadet_idk1())
# print(cadet_idk2())
# print(cadet_idk3())
# print()
# time.sleep(0.5)
cadet_upload('CADET.gcode')
cadet_print('CADET.GCO')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment