3d打印时,我们常用到g-code代码。那么如何查询g-code中用到了哪些命令呢?
下面C语言代码可以搜索并整理统计gcode文件中使用到的所有命令,也可以显示出某特定命令的所有代码,可作为一个编写gcode解释器的工具。
main.c
#include <stdio.h>
#include <stdlib.h>
#include "separate.h"
void main(){
get();
}
separate.c
#include <stdio.h>
#include <stdlib.h>
#include "separate.h"
//命令数量
int gNumber=0, mNumber=0;
//具体命令,如G28命令用数字28保存
int gCommands[100],mCommands[100];
void get(){
FILE *fp;
char str[100];
int a;
//获取代码
if( (fp=fopen("3d.txt","rt")) == NULL ){
puts("Fail to open file!");
exit(1);
}
//解析代码
while(fgets(str, 99, fp) != NULL){
//去除纯注释或空行
if(str[0]!='\n'&&str[0]!=';'&&str[0]!='\0'){
separate(str);
}
}
fclose(fp);
//输出结果
printf("\nNumber of G commands:%d",gNumber);
printf("\nNumber of M commands:%d \n",mNumber);
printf("\nG commands:\n");
for(a=0;a<gNumber;a++){
printf("%d ",gCommands[a]);
}
printf("\n\nM commands:\n");
for(a=0;a<mNumber;a++){
printf("%d ",mCommands[a]);
}
printf("\n\nend\n");
}
int FindG(int g){
int a;
for(a=0;a<gNumber;a++){
if(gCommands[a]==g) return 1;
}
return -1;
}
int FindM(int m){
int a;
for(a=0;a<mNumber;a++){
if(mCommands[a]==m) return 1;
}
return -1;
}
void separate(char str[]){
//str:命令后可能有注释
//整句命令,不带注释
char CommandSentence[100];
//如"G28"中的'2'和'8',将转换成int
char CharCommand[20]="\0";
//如"G28"中的"28"
int a,IntCommand;
//去除命令后注释
//gcode中';'后的是注释
for(a=0;(str[a]!='\n'&&str[a]!=';'&&str[a]!='\0');a++){
CommandSentence[a]=str[a];
}
//结束字符串
CommandSentence[a]='\0';
//去除命令内容
for(a=1;(CommandSentence[a]!='\0'&&CommandSentence[a]!=' ');a++){
CharCommand[a-1]=CommandSentence[a];
}
//转换成int便于操作
IntCommand=atoi(CharCommand);
if(str[0]=='G'){
//统计G命令个数
if(FindG(IntCommand)==-1){
gCommands[gNumber]=IntCommand;
gNumber++;
}
//显示特定命令的整句命令
//如:要找G1的命令,就在IntCommand后面写1
//if(IntCommand==1) printf("%s",str);
}else if(str[0]=='M'){
//统计M命令个数
if(FindM(IntCommand)==-1){
mCommands[mNumber]=IntCommand;
mNumber++;
}
//显示特定命令的整句命令
//如:要找M105,就在IntCommand==后写105
//if(IntCommand==105) printf("%s",str);
}else{
//未知命令
printf("unknown:\n");
printf("%s",str);
}
}
separate.h
#include <stdio.h>
#include <stdlib.h>
void get();
void separate(char str[]);
int FindG(int g);
int FindM(int m);
g-code示例:3d.txt
;MAXY:117.397
;MAXZ:12
;Generated with Cura_SteamEngine 4.4.1
M140 S60
M105
M190 S60
M104 S200
M105
M109 S200
M82 ;absolute extrusion mode
G21 ;metric values
G90 ;absolute positioning
M82 ;set extruder to absolute mode
M107 ;start with the fan off
G28 X0 Y0 ;move X/Y to min endstops
G28 Z0 ;move Z to min endstops
G1 Z15.0 F100 ;move the platform down 15mm
G92 E0 ;zero the extruded length
G1 F200 E3 ;extrude 3mm of feed stock
G92 E0 ;zero the extruded length again
G1 F100
G0 Y20 F100
M117 Printing...
G5
G92 E0
G92 E0
G1 F2400 E-6
;LAYER_COUNT:60
;LAYER:0
M106 S255
M204 S3000
M205 X10 Y10
G1 F600 Z0.275
G0 F3000 X94.61 Y94.977 Z0.275
M204 S1800
M205 X8 Y8
;TYPE:SKIRT
G1 F600 Z0.2
G1 F2400 E0
G1 X95.329 Y94.318 E0.04541
G1 X95.49 Y94.19 E0.05499
G1 X96.098 Y93.758 E0.08972
M204 S3000
M205 X10 Y10
G0 F3000 X95.098 Y95.277
M204 S1800
M205 X8 Y8
G1 F2400 X95.677 Y94.756 E4.05285
G1 X95.838 Y94.628 E4.06243
G1 X96.422 Y94.215 E4.09574
M204 S3000
M205 X10 Y10
G0 F3000 X95.584 Y95.584
M204 S1800
M205 X8 Y8
G1 F2400 X96.026 Y95.195 E7.89674
G1 X96.187 Y95.067 E7.90632
G1 X96.745 Y94.673 E7.93813
M204 S1800
M205 X8 Y8
G1 F1200 X101.947 Y101.133 E469.9681
M204 S3000
M205 X10 Y10
G0 F6000 X101.381 Y101.133
M204 S1800
M205 X8 Y8
G1 F1200 X101.134 Y101.38 E469.97972
;TIME_ELAPSED:458.818801
G1 F2400 E463.97972
M140 S0
M204 S4000
M205 X20 Y20
M107
M104 S0 ; turn off extruder
M140 S0 ; turn off bed
M84 ; disable motors
M107
G91 ;relative positioning
G1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressure
G1 Z+0.5 E-5 ;X-20 Y-20 F100 ;move Z up a bit and retract filament even more
G28 X0 ;Y0 ;move X/Y to min endstops, so the head is out of the way
G1 Y180 F2000
M84 ;steppers off
G90
M300 P300 S4000
M82 ;absolute extrusion mode
M104 S0
;End of Gcode
运行结果:
以乌班图20.04命令行为例
$gcc -o 3d separate.c main.c
$ ./3d
Number of G commands:8
Number of M commands:13
G commands:
21 90 28 1 92 0 5 91
M commands:
140 105 190 104 109 82 107 117 106 204 205 84 300
end
加上寻找特定命令:
$gcc -o 3d separate.c main.c
$ ./3d
M105
M105
G1 Z15.0 F100 ;move the platform down 15mm
G1 F200 E3 ;extrude 3mm of feed stock
G1 F100
G1 F2400 E-6
G1 F600 Z0.275
G1 F600 Z0.2
G1 F2400 E0
G1 X95.329 Y94.318 E0.04541
G1 X95.49 Y94.19 E0.05499
G1 X96.098 Y93.758 E0.08972
G1 F2400 X95.677 Y94.756 E4.05285
G1 X95.838 Y94.628 E4.06243
G1 X96.422 Y94.215 E4.09574
G1 F2400 X96.026 Y95.195 E7.89674
G1 X96.187 Y95.067 E7.90632
G1 X96.745 Y94.673 E7.93813
G1 F1200 X101.947 Y101.133 E469.9681
G1 F1200 X101.134 Y101.38 E469.97972
G1 F2400 E463.97972
G1 E-1 F300 ;retract the filament a bit before lifting the nozzle, to release some of the pressureG1 Z+0.5 E-5 ;X-20 Y-20 F100 ;move Z up a bit and retract filament even more
G1 Y180 F2000
Number of G commands:8
Number of M commands:13
G commands:
21 90 28 1 92 0 5 91
M commands:
140 105 190 104 109 82 107 117 106 204 205 84 300
end
github下载:git@github.com:yjr-jack/3dprinter