G Code Parser || interpreter

hi all, I am German so I am sorry for my bad english. But now to the problem.

I want to program a parer for G-Code. Ive take a look on projekts like RepRap or the Mandle. I have open the Firmware and my face look like :astonished:. Here is an example of what the Arduino will get from the PC.

;TYPE:CUSTOM T0 M92 E91.600000 M109 S240.000000 ;Sliced Multiple files at: Fr 12 Apr 2013 18:37:06 ;Basic settings: Layer height: 0.2 Walls: 1 Fill: 25 ;Print time: 0:11 ;Filament used: 0.29m 2.66g ;Filament cost: 0.06 G21 ;metric values G90 ;absolute positioning M107 ;start with the fan off G28 X0 Y0 ;move X/Y to min endstops G28 Z0 ;move Z to min endstops G92 X0 Y0 Z0 E0 ;reset software position to front/left/z=0.0 G1 Z15.0 F180 ;move the platform down 15mm G92 E0 ;zero the extruded length G1 F40 E5 ;extrude 5mm of feed stock G92 E0 ;zero the extruded length again G1 F3600;PRINTNR:0 ;LAYER:0 ;TYPE:SKIRT G1 X71.75 Y146.75 Z0.2 F3600.0 G1 F2400.0 G1 E2.0 G1 F3600.0 G1 X101.75 Y146.75 Z0.2 F1200.0 E2.5659 G1 X131.75 Y146.75 E3.1318 G1 X144.675 Y146.75 E3.3756 G1 X148.25 Y143.175 E3.4709 G1 X148.25 Y113.175 E4.0368 G1 X148.25 Y83.175 E4.6027 G1 X148.25 Y53.25 E5.1672 G1 X118.25 Y53.25 E5.7331 G1 X88.25 Y53.25 E6.2989 G1 X71.75 Y53.25 E6.6102

I want something like this:

  1. Read one line
  2. Detect the Symbols (G, X, Y, Z, E etc) 3.Read the value of the commands

I have tried the CmdMessenger but this one need a Number on the beginning of each line and commend. How can I realise my idea?

  1. Read one line

From? There are thousands of examples around for reading from the serial port and storing in a NULL terminated char array.

  1. Detect the Symbols (G, X, Y, Z, E etc)

Those records are not made up of symbols. They are made up of tokens. The strtok() function bears investigating. Once you have a token, like "G21", copying the first character, changing it to a space (" 21") and calling atoi() on the rest of the token will give you the command and the value.

Thank you for the quick answer. I have found an example for what you say.
http://www.cplusplus.com/reference/cstring/strtok/

so what i have to do is something like this:

#include <stdio.h>
#include <string.h>

int led1 = 2;

void setup(){
 pinMode (led1, OUTPUT);
 Serial.begin (115200);
}

void loop(){
 if (Serial.available()){
 char str[] = Serial.read();
 char * pch;

  Serial.println (str);
  pch = strtok (str," ,.-;:G");

  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-;:G");
  }
  return 0;
}


}
}

ok is it right that the “G” is not a Symbole but a Charactar?

Now the 1.Read one line
I want that the programm read all Serial input until it has reached the next /n or what ever is arrived when the serial Monitor send a next line.

I don't have anything to add to this discussion about parsing g-code beyond what has already been mentioned, but I do have to ask something:

Is your reason behind doing this to simply learn how it is done?

I only ask because what you are doing is simply "re-inventing the wheel" - there are more than a few Arduino g-code parsers out there. As to their quality or ability, I can't comment on, but I can say that there is probably a reason why their code is complex and seemingly unwieldy - at least from an initial glance.

You may just find yourself in a mess of your own similar code at some later point - ultimately fighting with similar or same problems that the coder(s) of those other interpreters fought.

Something else to keep in mind (not saying this is true, just something that might be a possibility) - depending on the needs of the machine being controlled by the Arduino, perhaps in an effort to get the most accuracy and speed the code ended up being more convoluted due to simply being hacked to gain every little bit from the device. The code may have started out very straightforward and easy to read, but by the end (and due to a ton of programmer's having this strange abhorrence to writing quality comments explaining what their algorithms are doing) the code looks incomprehensible, due to various "hacks" or optimizations applied.

Another possibility is that the actual g-code interpretation is being done by some intermediate software on the PC, and what is being sent to the Arduino is some -other- instruction/control code that is simpler for the micro to work with, given the restrictions of memory (a possibility - not saying this is true, just something to realize).

Again - if you are re-inventing the wheel as a learning exercise, I can't fault someone for that - provided they understand that is what they are doing. If your goal though, is simply to do so because you thought what currently exists is too complex - just realize that in the end, you might just make something just as complex (and inevitably - you might go back to review the other code out there, and find out just how much sense it makes after all - so in the end, you learn something in a very roundabout method)...

cr0sh - I could not agree more.

A note to Hagen: I have tried. A few years ago. Been there, Seen it, Done it - it worked, but it was slightly "dumbed down", sufficient for my purposes. I can send you the code.

Since then I've learned to read the full Marlin etc. They use every trick, like #define macros with arguments. Because the code is "Fully customizable" (every bit of source code is that, you just start editing ;) - because you only need to edit a few defines and that propagates through. Like there is a #define if the limit switches are HIGH when closed or LOW, and the code branches properly. The result is the code is needlessly(?!) complicated to cope with this or hidden through macros.

thanks for the replys,

I have a new Idea for a 3D Printer. I want to create a "in German - Lasersinter" that costs not more than 1500€. The other parts of the machine are no problem. and i know about the simple things in Arduino like pinMode and so on. But i want to understand the Functions of a Parser and how it works, because i must write it down for school and of cause for me. But every commend and tutorial is in english so it is very difficult for me.

I know about the marlin project because i use nearly the same firmware. But i have no Plan what is happens. I know that there is very much to learn and I should read more but I need the Infomations as fast as possible.

I try to understand the marlin project and if I find any more questions I will post it. I know a total new firmware is not the best way but i must explain it.

 char str[] = Serial.read();

The Serial.read() method returns one character. You are defining an array of length one, which does not leave room for the trailing NULL needed to make str a string (as opposed to an array of characters). Therefore, you can not pass str to any function that expects a string.

  pch = strtok (str," ,.-;:G");

Calling strtok() on a 1 element array doesn't make sense.

I think you need to study how the Serial functions work BEFORE you try to build a G code parser.

Ok there is no Problem any more thanks for all Information. I have bought a book about Arduino. For all German users here the link:

http://www.amazon.de/Arduino-Kochbuch-Michael-Margolis/dp/3868993533/ref=sr_1_1?s=books&ie=UTF8&qid=1370942675&sr=1-1&keywords=Arduino+Kochbuch

My Sketch works fine and I can connect to Printrun or Pronterface.