-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpythondcs.py
More file actions
executable file
·77 lines (64 loc) · 2.38 KB
/
pythondcs.py
File metadata and controls
executable file
·77 lines (64 loc) · 2.38 KB
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
# Python module intended for interface to the Duet Software Framework
# Copyright (C) 2020 Danal Estes all rights reserved.
# Released under The MIT
#
# As of Jan 2020, functions for interacting with Duet Control Server are implemented,
# plus a few things specific to the virtual SD config.g
#
import socket
import json
class PythonDCS:
def openDCS(self):
self.DCSsock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
self.DCSsock.connect('/var/run/dsf/dcs.sock')
self.DCSsock.setblocking(True)
j=json.dumps({"mode":"command"}).encode()
self.DCSsock.send(j)
r=self.DCSsock.recv(128).decode()
if (-1 == r.find('{"version":')):
print("Failed to enter command mode - version not received")
print(r)
exit(8)
if (-1 == r.find('{"success":true}')): #could be in same buffer as version
r=self.DCSsock.recv(128).decode()
if (-1 == r.find('{"success":true}')):
print("Failed to enter command mode - success not received")
print(r)
exit(8)
def closeDCS(self):
self.DCSsock.close()
def gCode(self,cmd=''):
#print(cmd)
j=json.dumps({"code": cmd,"channel": 0,"command": "SimpleCode"}).encode()
self.DCSsock.send(j)
r=self.DCSsock.recv(2048).decode()
if ('Error' in r):
print('Error detected, stopping script')
print(j)
print(r)
exit(8)
return(r)
def getPos(self):
result = json.loads(self.gCode('M408'))['result']
pos = json.loads(result)['pos']
#print('getPos = '+str(pos))
return pos
def resetEndstops(self):
self.gCode('M574 X1 S1 P"nil"')
self.gCode('M574 Y1 S1 P"nil"')
self.gCode('M574 Z1 S1 P"nil"')
self.gCode('M574 U1 S1 P"nil"')
self.gCode('M558 K0 P5 C"nil"')
c = open('/opt/dsf/sd/sys/config.g','r')
for each in [line for line in c if (('M574' in line) or ('M558' in line) or ('G31' in line))]: self.gCode(each)
c.close()
def resetAxisLimits(self):
c = open('/opt/dsf/sd/sys/config.g','r')
for each in [line for line in c if 'M208' in line]: self.gCode(each)
c.close()
def __init__(self):
self.openDCS()
def __enter__(self):
return self
def __exit__(self, *args):
self.closeDCS()