-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_serial.py
132 lines (115 loc) · 4.2 KB
/
fake_serial.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
#!/usr/bin/python3
##############################################################################
# Implementation of a "fake" serial module that emulates GRBL
# Mimics pySerial
#
#
# Dylan Armitage
##############################################################################
__author__ = "Dylan Armitage"
__email__ = "[email protected]"
__license__ = "MIT"
import logging
import coloredlogs
from pygcode import Machine, Line
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
#### GRBL responses ##########################################################
OK = 'ok'
SETTING_DISABLED = 'error:Setting disabled'
NOT_IDLE = 'error:Not idle'
ALARM_LOCK = 'error:Alarm lock'
LINE_OVERFLOW = 'error:Line overflow'
MODAL_VIOLATION = 'error:Modal group violation'
UNSUPPORTED_COMMAND = 'error:Unsupported command'
UNDEFINED_FEED = 'error:Undefined feed rate'
INVALID_GCODE = {'not integer': 'error:Invalid gcode ID:23',
'both xyz': 'error:Invalid gcode ID:24',
'repeated': 'error:Invalid gcode ID:25',
'no xyz': 'error:Invalid gcode ID:26',
'nline oor': 'error:Invalid gcode ID:27',
'missing pl': 'error:Invalid gcode ID:28',
'unsup wcs': 'error:Invalid gcode ID:29',
'wrong g53 mode': 'error:Invalid gcode ID:30',
'unused axis': 'error:Invalid gcode ID:31',
'no xyz arc': 'error:Invalid gcode ID:32',
'invalid target': 'error:Invalid gcode ID:33',
'arc geom rad': 'error:Invalid gcode ID:34',
'arc miss IJK': 'error:Invalid gcode ID:35',
'unused words': 'error:Invalid gcode ID:36',
'tool offset axis': 'error:Invalid gcode ID:37'
}
ALARM_HLIM = 'ALARM:Hard limit'
ALARM_SLIM = 'ALARM:Soft limit'
ALARM_ABORT = 'ALARM:Abort during cycle'
RESET_CONTINUE = '[reset to continue]'
TO_UNLOCK = '[\'$H\'|\'$X\' to unlock]'
CAUTION = '[Caution: Unlocked]'
ENABLED = '[Enabled]'
DISABLED = '[Disabled]'
IDLE = 'Idle'
RUNNING = 'Run'
ALARM = 'ALARM'
class grbl_interpret:
'''A simple python implementation of grbl
Mostly just makes sure valid gcode is being used
'''
def __init__(self):
self.state_pos = {'state': IDLE,
'mpos': [0.000, 0.000, 0.000],
'wpos': [0.000, 0.000, 0.000],
'plan buf': 0,
'rx buf': 0
}
self.mach = Machine()
def input(self, line):
pass
class Serial:
def __init__(self, port='/dev/ttyAMA0', baudrate=115200, timeout=1, bytesize=8,
parity='N', stopbits=1, xonxoff=0, rtscts=0):
self.name = name
self.port = port
self.baudrate = baudrate
self.timeout = timeout
self.bytesize = bytesize
self.parity = parity
self.stopbits = stopbits
self.xonxoff = xonxoff
self.rtscts = rtscts
self._open = True
self._received_data = ''
self._data = 'Grbl v1.1f [\'$\' for help]\n'
logger.debug('fakeSerial initialized')
def is_open(self):
return self._open
def open(self):
self._open = True
def close(self):
self._open = False
def read(self, n=1):
# Read n characters from the output buffer
try:
buff = self._data[0:n]
self._data = self._data[n:]
except IndexError:
logger.debug('No data in buffer')
buff = ''
return buff
def readline(self):
# Read individual characters until newline found
try:
newline = self._data.index('\n')
if newline != -1:
buff = self._data[0:newline+1]
self._data = self._data[newline+1:]
else:
logger.debug('No data in buffer')
buff = ''
except IndexError:
logger.debug('No data in buffer')
buff = ''
return buff
def write(self, string):
# TODO: Process the string written to append the appropriate response
logger.info('fakeSerial received %s', string)
self._received_data += string