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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
package org.reprap.comms;
import java.util.*;
import javax.swing.JFileChooser;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import org.reprap.utilities.Debug;
import org.reprap.Preferences;
public class GCodeWriter
{
/**
* this is if we need to talk over serial
*/
private SerialPort port;
/**
* this is for doing easy writes
*/
private PrintStream outStream;
/**
* this is our read handle
*/
private InputStream inStream;
/**
* are we printing to a file, or serial?
*/
private boolean printToFile = false;
/**
* our array of gcode commands
*/
private Vector commands;
/**
* our pointer to the currently executing command
*/
private int currentCommand = 0;
/**
* what was the last command we sent? (we may send commands faster than it can process)
*/
private int nextCommandToSend = 0;
/**
* the size of the buffer on the GCode host
*/
private int maxBufferSize = 128;
/**
* the amount of data we've sent and is in the buffer.
*/
private int bufferSize = 0;
/**
* how many commands do we have in the buffer?
*/
private int bufferLength = 0;
private String result = "";
public GCodeWriter()
{
commands = new Vector();
}
public void finish()
{
Debug.d("disposing of gcodewriter.");
if (!printToFile)
{
Debug.d("Sending serial commands.");
fillSerialBuffer();
}
try
{
if (inStream != null)
inStream.close();
if (outStream != null)
outStream.close();
} catch (Exception e) {}
}
public void queue(String cmd)
{
//cmd = "N" + commands.size() + " " + cmd;
commands.add(cmd);
if (printToFile)
outStream.println(cmd);
Debug.d("Queued: " + cmd);
}
public void fillSerialBuffer()
{
//keep trying until we send all commands.
while(nextCommandToSend < commands.size())
{
//check to see if we got a response.
readResponse();
//whats our next command?
String next = (String)commands.get(nextCommandToSend);
//will it fit into our buffer?
while (bufferSize + next.length() < maxBufferSize)
{
//send it a byte at a time.
for (int i=0; i<next.length(); i++)
{
outStream.write(next.charAt(i));
outStream.flush();
//wait 5us between bytes.
try{
Thread.sleep(5);
} catch (Exception e){}
}
//newline is our delimiter.
outStream.write('\n');
outStream.flush();
//wait between commands.
try{
Thread.sleep(5);
} catch (Exception e){}
//record it in our buffer tracker.
nextCommandToSend++;
bufferSize += next.length() + 1;
bufferLength++;
//debug... let us know whts up!
Debug.c("Sent: " + next);
Debug.d("Buffer: " + bufferSize + " (" + bufferLength + " commands)");
if (nextCommandToSend == commands.size())
break;
next = (String)commands.get(nextCommandToSend);
}
}
}
public void readResponse()
{
String cmd = "";
//read for any results.
for (;;)
{
try
{
//read a byte.
int i = inStream.read();
//nothing found.
if (i == -1)
break;
else
{
//get it as ascii.
char c = (char)i;
result += c;
//is it a done command?
if (c == '\n')
{
if (result.startsWith("ok"))
{
cmd = (String)commands.get(currentCommand);
if (result.length() > 2)
Debug.c("got: " + result.substring(0, result.length()-2) + "(" + bufferSize + " - " + (cmd.length() + 1) + " = " + (bufferSize - (cmd.length() + 1)) + ")");
bufferSize -= cmd.length() + 1;
bufferLength--;
currentCommand++;
result = "";
Debug.d("Buffer: " + bufferSize + " (" + bufferLength + " commands)");
//bail, buffer is almost empty. fill it!
if (bufferLength < 2)
break;
//we'll never get here.. for testing.
if (bufferLength == 0)
Debug.d("Empy buffer!! :(");
}
else if (result.startsWith("T:"))
Debug.d(result.substring(0, result.length()-2));
else
Debug.c(result.substring(0, result.length()-2));
result = "";
}
}
} catch (IOException e) {
break;
}
}
}
public void openSerialConnection(String portName)
{
printToFile = false;
int baudRate = 19200;
//open our port.
Debug.d("Opening port " + portName);
try
{
CommPortIdentifier commId = CommPortIdentifier.getPortIdentifier(portName);
port = (SerialPort)commId.open(portName, 30000);
} catch (NoSuchPortException e) {
Debug.d("Error opening port: " + port);
}
catch (PortInUseException e){
Debug.d("Port '" + port + "' is already in use.");
}
//get our baudrate
try {
baudRate = Preferences.loadGlobalInt("BaudRate");
}
catch (IOException e){}
// Workround for javax.comm bug.
// See http://forum.java.sun.com/thread.jspa?threadID=673793
// FIXME: jvandewiel: is this workaround also needed when using the RXTX library?
try {
port.setSerialPortParams(baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
}
catch (UnsupportedCommOperationException e) {
Debug.d("An unsupported comms operation was encountered.");
}
/*
port.setSerialPortParams(baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
*/
// End of workround
try {
port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (Exception e) {
// Um, Linux USB ports don't do this. What can I do about it?
}
try {
port.enableReceiveTimeout(1);
} catch (UnsupportedCommOperationException e) {
Debug.d("Read timeouts unsupported on this platform");
}
//create our steams
try {
OutputStream writeStream = port.getOutputStream();
inStream = port.getInputStream();
outStream = new PrintStream(writeStream);
} catch (IOException e) {
Debug.d("Error opening serial port stream.");
}
//arduino bootloader skip.
Debug.d("Attempting to initialize Arduino");
try {Thread.sleep(1000);} catch (Exception e) {}
for(int i = 0; i < 10; i++)
outStream.write('0');
try {Thread.sleep(1000);} catch (Exception e) {}
}
public void openFile(String filename)
{
printToFile = true;
Debug.d("Opening file for GCode output: " + filename);
if (filename.equals("stdout"))
{
outStream = System.out;
}
else
{
try
{
Debug.d("opening: " + filename);
FileOutputStream fileStream = new FileOutputStream(filename);
outStream = new PrintStream(fileStream);
} catch (FileNotFoundException e) {
Debug.d("File '" + filename + "' not found, printing to stdout");
outStream = System.out;
}
}
}
public void openFile()
{
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
//chooser.setCurrentDirectory();
int result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION)
{
String name = chooser.getSelectedFile().getAbsolutePath();
openFile(name);
}
else
{
openFile("stdout");
}
}
}
|