Go Down

Topic: ideas for parsing hpgl code? (Read 1 time) previous topic - next topic


I use atoi like below.

Code: [Select]

servo1 = readString.substring(7, 11);
     servo2 = readString.substring(12, 16);
     int n1;
     int n2;
     n1 = atoi(servo1); //convert string to number
     n2 = atoi(servo2);
Consider the daffodil. And while you're doing that, I'll be over here, looking through your stuff.   8)


Thanks for atoi. I had to convert the String datatype to a char array in order to get atoi to work. Probably not the most efficient way to accomplish the task of parsing this sort of incoming data ("PD3450.0,-2202.0;") but it's code I can understand and build on.

Code: [Select]
void setup()

void loop()
 int sTime = millis();
 String scratchpad = "PD3450.0,-2202.0";
 String cmd = scratchpad.substring(0, 2);

 int len = scratchpad.length();
 int i = scratchpad.indexOf(",");

 String x = scratchpad.substring(2, i);
 String y = scratchpad.substring(i + 1, len);

 char X_array[20];
 char Y_array[20];

 x.toCharArray(X_array, 20);
 y.toCharArray(Y_array, 20);

 float X1 = atoi(X_array);
 float Y1 = atoi(Y_array);

 int eTime = sTime - millis();
 Serial.print("x: ");
 Serial.print("y: ");



pretty curious what you will come up with,
I´m struggling with the same problem at the moment,

The thing i probably will do is interpret the hpgl on my computer (Probably c# or so) and send custom stripped clean commands to the arduino over serial.

Quit some time ago I  build a crappy robot arm controller which didnt work too well, but this principle of sending data worked fine..
doing all the difficult calculating on my laptop and have the arduino only reaspond to simple commands,

This is the scripot i used back than
Code: [Select]

#include <Servo.h>
#include <stdlib.h>     // needed for atoi

char buffer[4];
int received;

Servo joop;  
Servo pietje;  

int joopangle;
int pietjeangle;

void setup()



void loop()
 if (Serial.available())
       buffer[received++] = Serial.read();
       buffer[received] = '\0';
       if (received == (sizeof(buffer)))
           int firstletter = buffer[0];
           char finalval[3] = {buffer[1], buffer[2], buffer[3]};
           int servoangle = atol(finalval);
           if(firstletter == 'J')
               joopangle = servoangle;
            if(firstletter == 'P')
              pietjeangle = servoangle;
            received = 0;

Once again I´m very curious for your result; keep posting..


Hi -

I've wrestled a lot with motori: http://sensi.org/~svo/motori/

He has a fairly complete, basic parser but I found the code very Arduino "unfriendly" - lots of compiler issues - and my knowledge of C/C++/the Arduino build process was not sufficient to tech it out. He is also using timers and interrupts and it was hard to uncouple that (again, my insufficient understanding of his code).

I've finally got it working - using his method of scanning the input but basically rewriting the whole thing to work with Arduino (along with coding in a way I can understand and debug). Learned a lot!

Meanwhile I've also been thinking through my whole toolchain and wondering if .hpgl is the best end result, compared to gcode. I think I have settled on using pstoedit as my main conversion utility and its .hpgl export isn't as good as its gcode export. So my whole excursion into hpgl may have been for the learning/scars only ...  :)

The RepRap gcode parser is Arduino freindly and easier to get up and running.



Would you be willing to share your aduino to hpgl code? It seems just what I need to build on for a project I have in mind.


Go Up