Warning

 

Close

Confirm Action

Are you sure you wish to do this?

Confirm Cancel
Member Login
Site Notices
9/22/2017 12:11:25 AM
Posted: 5/19/2002 3:37:32 AM EDT
[left]I writing a pieace of software to read CNC files all text. I cannot seam to be able to extract G codes from the file! I am writting the program with visual C++ 6.0 using file streams ifstream ostream etc! all in dos. I know there are programming gurus here! any help be appreciated. example I want to set a rapid and set an output to a controller! I am wanting to extract G code's tool change set X/Y position.[/left] N1 G0 G70 G90 X-5.0 Y-5.0 T1 M6
Link Posted: 5/19/2002 5:18:25 AM EDT
I know nothing about G codes...... Are you reading the file as text, are you sure it is a text file? Can you write the data to stdout or another file? This isn't homework?
Link Posted: 5/19/2002 5:29:58 AM EDT
Well, I know C and C++ quite well. But I have no idea what a CNC file is. If you could describe what it is and how it's formatted maybe we can help you.
Link Posted: 5/19/2002 5:50:41 AM EDT
Well what I am trying to do is to extract G and m Codes fram a plain text file what I am trying to do is extract G codes example: N1 G0 G70 G90 X-5.0 Y-5.0 T1 M6 Read the line above extract 'G0' set the controller! extract 'G70' set the controller! extract 'G90' set the controller! here extract X and Y axis get the floating variables to set the table position. I have not figured out how I wanted to handle tool change yet. the whole idea is build around a custom interpretur software, and all i need to do is read G and M codes, tool changes to set a controller! any help would be Gladly appreciated.
Link Posted: 5/19/2002 6:01:05 AM EDT
Look at strtok in ANSI C. Use the space character as the delimiter to parse for the G* fields. Be aware that strtok doesn't work the same in all libraries. I forget what the differences are but you should be able to figure it out easily enough if you step through with your debugger.
Link Posted: 5/19/2002 7:21:03 AM EDT
That looks like something Perl would be real handy for.
Link Posted: 5/19/2002 7:39:51 AM EDT
That looks like a basic parsing program for a school homework assignment in beginning C++... Open File File Contents: N1 G0 G70 G90 X-5.0 Y-5.0 T1 M6 Read Line Parse Do something with values... If there is more than one line to parse, add a loop. What is a CNC file anyway?
Link Posted: 5/19/2002 8:14:36 AM EDT
Until you answer my questions I am not going to help.
Link Posted: 5/19/2002 8:45:25 AM EDT
Just use fgets() to read each line into a string and then take stipilot's advice and use strtok() to parse it.
Link Posted: 5/19/2002 8:57:32 AM EDT
[Last Edit: 5/19/2002 9:00:40 AM EDT by DaveTX]
Just use getline() within the ifstream class and then parse the line. Parsing in this case would mean using strtok() to get each token ("N1", "G0", etc) then check the first character. Do double number; sscanf(myToken+1, "%lf", &number); switch (myToken[0]) { case 'G': [store it as a G value or whatever] case 'X': [store it as x] case 'Y': [you get the idea] } I certainly do not know everything about C++ but I am a microsoft certified professional in developing desktop applications with vc6.0 and I have no idea what "setting a rapid" or "setting output to a controller." Edited to add: Sorry, my indentions in the code do not seem to be showing up after I post! Sorry if this response is too basic; this is what you need to know right?
Link Posted: 5/19/2002 9:04:52 AM EDT
Sorry, I don't know a way to make the text nonproportional. Example: #include main(int argc, char *argv[]) { FILE *fp; int n, cont1, cont2, cont3, x, y, t, m; fp = fopen("a.txt", "r"); fscanf(fp, "N%d G%d G%d G%d X-%d Y-%d T%d M%d", &n, &cont1, &cont2, &cont3, &x, &y, &t, &m); printf("Controller 1: %d\n", cont1); printf("Controller 2: %d\n", cont2); printf("Controller 3: %d\n", cont3); fclose(fp); return(0); } The output: tcsh% cc a.c tcsh% ./a.out Controller 1: 0 Controller 2: 70 Controller 3: 90 I've been programming for over 30 years. I used to eat-sleep-breathe formatted data. Too bad know-how won't get you a job.z
Link Posted: 5/19/2002 10:50:01 AM EDT
zoom - Are you sure he can count on it being formatted perfectly every time? I just assumed that the string he gave was an example and that it could come in any order. I don't really feel like I have enough info... If so, then go with his approach using fscanf/sscanf I think he also wanted to use fstream though.
Link Posted: 5/19/2002 12:00:14 PM EDT
Look at strtok in ANSI C. Use the space character as the delimiter to parse for the G* fields.
View Quote
This is the right approach, except also look for the ANSI STR for END OF LINE, ie, Carriage Return. If you know pearl this would be a breeze as this is exactly why pearl was written. Ben
Link Posted: 5/19/2002 12:55:02 PM EDT
[left]Here is the complete cycle of the code! what I am trying to do is make an interpreter software run the controller off of the parellel port running stepper motor's off of a small mill I was going to purchase a Camtronics 5amp controller and retrofit a grizzly milling machine to CNC. well I thought a neat software project to do is to read a CNC file interpret what the code says! Here is a sniplet CNC program we used on a Bridgeport mill I downloaded a turboCNC dos software. the code's run fine with it i thought about desigining my own. G codes tell the controller what to do what my software is intended to do is to talk to the controller! N1 G0 G70 G90 X-2.25 Y-.75 T1 M6 N5 X-.75 Y1. Z.050 N10 G1 Z-.050 F25. N15 Y.75 N20 X-1.25 N25 Y1.75 N30 X-.75 N35 Y1.5 N45 G0 X-.5 Y.75 Z.050 N50 G1Z-.050 F25. N55 G0X-.25Y.75Z.050 N60 G1 Z-.050 F25. N65 Y1.75 N70 X.25 Y.75 N75 Y1.75 N80 G0X.5Y.75Z.050 N85 G1Z-.050 F25. N90 G0X1.25Y1.5Z.050 N95 G1Z-.050 F25. N100 Y1.75 N105X.75 N110Y.75 N115X1.25
Link Posted: 5/19/2002 3:04:32 PM EDT
The model you should be following is DaveTX's example. It would look some like this. #include #include static void G( double val ); static void X( double val ); static void Y( double val ); static void Z( double val ); int main( int argc, char* argv[] ) { double val; FILE *fp; char buf[256], s[256], *p; if ((fp = fopen("path.cnc", "r")) != NULL) { while (fgets(buf, sizeof(buf), f) != NULL) { p = strtok(line, " "); while (p != NULL) { sscanf(p+1, "%lf", &val); switch (toupper(*p)) { case 'G': G(val); break; case 'X': X(val); break; case 'Y': Y(val); break; case 'Z': Z(val); break; } p = strtok(NULL, " "); } } fclose(fp); } return 0; } static void G( double val ) { } static void X( double val ) { } static void Y( double val ) { } static void Z( double val ) { } This assumes you always a blank space between commands.
Top Top