2626#include < stdlib.h>
2727
2828#include < QProcess>
29+ #include < QTimer>
30+ #include < QDebug>
2931#include < QStringList>
3032#include < QString>
3133#include < QTime>
3436#include < QFileDialog>
3537#include < QStatusBar>
3638#include < QFile>
39+ #include < QDir>
3740#include < QTextStream>
3841#include < QTemporaryFile>
42+ #include < QThread>
3943
4044#include < QDebug>
4145
4650namespace g2m {
4751
4852void g2m::interpret_file () {
49- lineVector.clear ();
53+ if (file.isEmpty () || (!file.endsWith (" .ngc" ) && !file.endsWith (" .canon" ))) {
54+ infoMsg (" No valid g-code file to interpret" );
55+ return ;
56+ }
57+
58+ interpret_file_async ();
59+ }
60+
61+ void g2m::interpret_file_async () {
5062 nanotimer timer;
5163 timer.start ();
5264 gcode_lines = 0 ;
@@ -117,7 +129,17 @@ void g2m::interpret_file() {
117129
118130// /ask for a tool table, even if one is configured - user may wish to change it
119131bool g2m::chooseToolTable () {
120- if (!QFileInfo (tooltable).exists ()){
132+ if (tooltable.isEmpty () || !QFileInfo (tooltable).exists ()){
133+ QString defaultTooltable = QDir::tempPath () + " /qgcoder.tooltable" ;
134+ QFile file (defaultTooltable);
135+ if (file.open (QIODevice::WriteOnly | QIODevice::Text)) {
136+ QTextStream out (&file);
137+ out << " T1 P1 Z0.0 D0.125000 ; 1/8 inch end mill\n " ;
138+ out << " T2 P2 Z0.0 D0.062500 ; 1/4 inch end mill\n " ;
139+ file.close ();
140+ tooltable = defaultTooltable;
141+ return true ;
142+ }
121143 infoMsg (" cannot find tooltable! " );
122144 emit debugMessage (" cannot find tooltable! " );
123145 return false ;
@@ -129,16 +151,35 @@ bool g2m::chooseToolTable() {
129151bool g2m::startInterp (QProcess &tc) {
130152 if (!chooseToolTable ())
131153 return false ;
132- // run: rs274 file.ngc
154+ tc. setProcessChannelMode (QProcess::SeparateChannels);
133155 tc.start ( interp , QStringList (file) );
134156 if (!tc.waitForStarted (5000 )) {
135157 infoMsg (" Interpreter failed to start" );
136158 return false ;
137159 }
138- tc.write (" 3\n " ); // "read tool file" command to rs274
139- tc.write (tooltable.toLatin1 ());
140- tc.write (" \n " ); // "enter"
141- tc.write (" 1\n " ); // "start interpreting" command to rs274
160+ tc.write (" 3\n " );
161+ if (!tc.waitForReadyRead (2000 )) {
162+ if (tc.state () == QProcess::NotRunning) {
163+ std::string err = tc.readAllStandardError ().constData ();
164+ infoMsg (" Interpreter died: " + err);
165+ return false ;
166+ }
167+ }
168+ QString resp = tc.readAllStandardOutput ();
169+ if (!resp.contains (" name of tool file" )) {
170+ tc.waitForReadyRead (1000 );
171+ resp = tc.readAll ();
172+ }
173+ QByteArray toolPath = tooltable.toLocal8Bit ();
174+ tc.write (toolPath);
175+ tc.write (" \n " );
176+ tc.waitForReadyRead (2000 );
177+ resp = tc.readAllStandardOutput ();
178+ if (resp.contains (" Cannot open" )) {
179+ infoMsg (" Error: Cannot open tooltable file. Check file permissions and path." );
180+ return false ;
181+ }
182+ tc.write (" 1\n " );
142183 tc.closeWriteChannel ();
143184 return true ;
144185}
@@ -209,8 +250,12 @@ void g2m::interpret() {
209250 return ;
210251 }
211252
212- if (!foundEOF) {
213- infoMsg (" Warning: file data not terminated correctly. If the file is terminated correctly, this indicates a problem interpreting the file." );
253+ if (!foundEOF && toCanon.state () == QProcess::NotRunning) {
254+ QVector<canonLine*> allLines;
255+ for (canonLine* line : lineVector) {
256+ allLines.append (line);
257+ }
258+ emit signalCanonLines (allLines);
214259 }
215260
216261 emit debugMessage ( tr (" g2m: read %1 lines of g-code which produced %2 canon-lines." ).arg (gcode_lines).arg (lineVector.size ()) );
@@ -252,28 +297,40 @@ void g2m::infoMsg(std::string s) {
252297 std::cout << s << std::endl;
253298}
254299
300+ // / set the tooltable and start interpreting input from stdin. called from interpret()
255301// / set the tooltable and start interpreting input from stdin. called from interpret()
256302bool g2m::startInterp2 (QProcess &tc, QString tempFile) {
257303 if (!chooseToolTable ())
258304 return false ;
259305 // run: rs274 file.ngc
260-
261- // qDebug() << QStringList(tempFile);
262-
306+ tc.setProcessChannelMode (QProcess::SeparateChannels);
263307 tc.start ( interp , QStringList (tempFile) );
264308
265309 if (!tc.waitForStarted (5000 )) {
266310 infoMsg (" Interpreter failed to start" );
267311 return false ;
268312 }
269313
270- tc.write (" 3\n " ); // "read tool file" command to rs274
314+ tc.write (" 3\n " );
271315
272- // qDebug() << tooltable.toLatin1() ;
316+ QString resp ;
273317
274- tc.write (tooltable.toLatin1 ());
275- tc.write (" \n " ); // "enter"
276- tc.write (" 1\n " ); // "start interpreting" command to rs274
318+ tc.waitForReadyRead (100 );
319+ resp = tc.readAllStandardOutput ();
320+
321+ QByteArray toolPath = tooltable.toLocal8Bit ();
322+ tc.write (toolPath);
323+ tc.write (" \n " );
324+ tc.waitForReadyRead (3000 );
325+ resp = tc.readAllStandardOutput ();
326+
327+ if (resp.contains (" Cannot open" )) {
328+ infoMsg (" Error: Cannot open tooltable file. Check file permissions and path." );
329+ emit debugMessage (" Cannot open tooltable: " + tooltable);
330+ return false ;
331+ }
332+
333+ tc.write (" 1\n " );
277334 tc.closeWriteChannel ();
278335 return true ;
279336}
@@ -328,13 +385,22 @@ void g2m::interpret2(QString tempFile) {
328385 // std::cout << " ERROR: toCanon.canReadLine() fails="<< fails << "\n";
329386 fails++;
330387 }
331- toCanon.waitForReadyRead ();
388+ if (!toCanon.waitForReadyRead (500 ))
389+ break ;
332390 } while ( (fails < 1000 ) &&
333391 ( (toCanon.canReadLine ()) ||
334392 ( toCanon.state () != QProcess::NotRunning ) ) );
335393
336394 emit canonLineMessage ( l.left (l.size ()-1 ) );
337395
396+ if (!foundEOF && toCanon.state () == QProcess::NotRunning) {
397+ QVector<canonLine*> allLines;
398+ for (canonLine* line : lineVector) {
399+ allLines.append (line);
400+ }
401+ emit signalCanonLines (allLines);
402+ }
403+
338404 if (fails > 1 ) {
339405 if (fails < 1000 ) {
340406 infoMsg (" Waited for interpreter over 1000 times." );
@@ -361,6 +427,9 @@ void g2m::interpret2(QString tempFile) {
361427 }
362428
363429 emit debugMessage ( tr (" g2m: read %1 lines of g-code which produced %2 canon-lines." ).arg (gcode_lines).arg (lineVector.size ()) );
430+
431+ toCanon.kill ();
432+ toCanon.waitForFinished ();
364433 return ;
365434}
366435
0 commit comments