소스 검색

Merge pull request #1570 from C-o-r-E/escape_fix

Fixes gCode handling of comments and escaped characters
Scott Lahteine 9 년 전
부모
커밋
254970c92a
2개의 변경된 파일106개의 추가작업 그리고 74개의 파일을 삭제
  1. 22
    0
      Documentation/GCodes.md
  2. 84
    74
      Marlin/Marlin_main.cpp

+ 22
- 0
Documentation/GCodes.md 파일 보기

@@ -101,3 +101,25 @@
101 101
 *  M908 - Control digital trimpot directly.
102 102
 *  M928 - Start SD logging (M928 filename.g) - ended by M29
103 103
 *  M999 - Restart after being stopped by error
104
+
105
+# Comments
106
+
107
+Comments start at a `;` (semicolon) and end with the end of the line:
108
+
109
+    N3 T0*57 ; This is a comment
110
+    N4 G92 E0*67
111
+    ; So is this
112
+    N5 G28*22
113
+
114
+(example taken from the [RepRap wiki](http://reprap.org/wiki/Gcode#Comments))
115
+
116
+If you need to use a literal `;` somewhere (for example within `M117`), you can escape semicolons with a `\`
117
+(backslash):
118
+
119
+     M117 Hello \;)
120
+
121
+`\` can also be used to escape `\` itself, if you need a literal `\` in front of a `;`:
122
+
123
+    M117 backslash: \\;and a comment
124
+
125
+Please note that hosts should strip any comments before sending GCODE to the printer in order to save bandwidth.

+ 84
- 74
Marlin/Marlin_main.cpp 파일 보기

@@ -730,103 +730,113 @@ void get_command()
730 730
     serial_char = MYSERIAL.read();
731 731
     if(serial_char == '\n' ||
732 732
        serial_char == '\r' ||
733
-       (serial_char == ':' && comment_mode == false) ||
734 733
        serial_count >= (MAX_CMD_SIZE - 1) )
735 734
     {
736
-      if(!serial_count) { //if empty line
737
-        comment_mode = false; //for new command
735
+      // end of line == end of comment
736
+      comment_mode = false;
737
+
738
+      if(!serial_count) {
739
+        // short cut for empty lines
738 740
         return;
739 741
       }
740 742
       cmdbuffer[bufindw][serial_count] = 0; //terminate string
741
-      if(!comment_mode){
742
-        comment_mode = false; //for new command
743
-        fromsd[bufindw] = false;
744
-        if(strchr(cmdbuffer[bufindw], 'N') != NULL)
743
+
744
+      fromsd[bufindw] = false;
745
+      if(strchr(cmdbuffer[bufindw], 'N') != NULL)
746
+      {
747
+        strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
748
+        gcode_N = (strtol(strchr_pointer + 1, NULL, 10));
749
+        if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
750
+          SERIAL_ERROR_START;
751
+          SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
752
+          SERIAL_ERRORLN(gcode_LastN);
753
+          //Serial.println(gcode_N);
754
+          FlushSerialRequestResend();
755
+          serial_count = 0;
756
+          return;
757
+        }
758
+
759
+        if(strchr(cmdbuffer[bufindw], '*') != NULL)
745 760
         {
746
-          strchr_pointer = strchr(cmdbuffer[bufindw], 'N');
747
-          gcode_N = (strtol(strchr_pointer + 1, NULL, 10));
748
-          if(gcode_N != gcode_LastN+1 && (strstr_P(cmdbuffer[bufindw], PSTR("M110")) == NULL) ) {
749
-            SERIAL_ERROR_START;
750
-            SERIAL_ERRORPGM(MSG_ERR_LINE_NO);
751
-            SERIAL_ERRORLN(gcode_LastN);
752
-            //Serial.println(gcode_N);
753
-            FlushSerialRequestResend();
754
-            serial_count = 0;
755
-            return;
756
-          }
761
+          byte checksum = 0;
762
+          byte count = 0;
763
+          while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
764
+          strchr_pointer = strchr(cmdbuffer[bufindw], '*');
757 765
 
758
-          if(strchr(cmdbuffer[bufindw], '*') != NULL)
759
-          {
760
-            byte checksum = 0;
761
-            byte count = 0;
762
-            while(cmdbuffer[bufindw][count] != '*') checksum = checksum^cmdbuffer[bufindw][count++];
763
-            strchr_pointer = strchr(cmdbuffer[bufindw], '*');
764
-
765
-            if( (int)(strtod(strchr_pointer + 1, NULL)) != checksum) {
766
-              SERIAL_ERROR_START;
767
-              SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
768
-              SERIAL_ERRORLN(gcode_LastN);
769
-              FlushSerialRequestResend();
770
-              serial_count = 0;
771
-              return;
772
-            }
773
-            //if no errors, continue parsing
774
-          }
775
-          else
776
-          {
766
+          if( (int)(strtod(strchr_pointer + 1, NULL)) != checksum) {
777 767
             SERIAL_ERROR_START;
778
-            SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
768
+            SERIAL_ERRORPGM(MSG_ERR_CHECKSUM_MISMATCH);
779 769
             SERIAL_ERRORLN(gcode_LastN);
780 770
             FlushSerialRequestResend();
781 771
             serial_count = 0;
782 772
             return;
783 773
           }
784
-
785
-          gcode_LastN = gcode_N;
786 774
           //if no errors, continue parsing
787 775
         }
788
-        else  // if we don't receive 'N' but still see '*'
776
+        else
789 777
         {
790
-          if((strchr(cmdbuffer[bufindw], '*') != NULL))
791
-          {
792
-            SERIAL_ERROR_START;
793
-            SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
794
-            SERIAL_ERRORLN(gcode_LastN);
795
-            serial_count = 0;
796
-            return;
797
-          }
778
+          SERIAL_ERROR_START;
779
+          SERIAL_ERRORPGM(MSG_ERR_NO_CHECKSUM);
780
+          SERIAL_ERRORLN(gcode_LastN);
781
+          FlushSerialRequestResend();
782
+          serial_count = 0;
783
+          return;
798 784
         }
799
-        if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
800
-          strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
801
-          switch((int)((strtod(strchr_pointer + 1, NULL)))){
802
-          case 0:
803
-          case 1:
804
-          case 2:
805
-          case 3:
806
-            if (Stopped == true) {
807
-              SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
808
-              LCD_MESSAGEPGM(MSG_STOPPED);
809
-            }
810
-            break;
811
-          default:
812
-            break;
813
-          }
814 785
 
786
+        gcode_LastN = gcode_N;
787
+        //if no errors, continue parsing
788
+      }
789
+      else  // if we don't receive 'N' but still see '*'
790
+      {
791
+        if((strchr(cmdbuffer[bufindw], '*') != NULL))
792
+        {
793
+          SERIAL_ERROR_START;
794
+          SERIAL_ERRORPGM(MSG_ERR_NO_LINENUMBER_WITH_CHECKSUM);
795
+          SERIAL_ERRORLN(gcode_LastN);
796
+          serial_count = 0;
797
+          return;
798
+        }
799
+      }
800
+      if((strchr(cmdbuffer[bufindw], 'G') != NULL)){
801
+        strchr_pointer = strchr(cmdbuffer[bufindw], 'G');
802
+        switch((int)((strtod(strchr_pointer + 1, NULL)))){
803
+        case 0:
804
+        case 1:
805
+        case 2:
806
+        case 3:
807
+          if (Stopped == true) {
808
+            SERIAL_ERRORLNPGM(MSG_ERR_STOPPED);
809
+            LCD_MESSAGEPGM(MSG_STOPPED);
810
+          }
811
+          break;
812
+        default:
813
+          break;
815 814
         }
816 815
 
817
-        //If command was e-stop process now
818
-        if(strcmp(cmdbuffer[bufindw], "M112") == 0)
819
-          kill();
820
-        
821
-        bufindw = (bufindw + 1)%BUFSIZE;
822
-        buflen += 1;
823 816
       }
817
+
818
+      //If command was e-stop process now
819
+      if(strcmp(cmdbuffer[bufindw], "M112") == 0)
820
+        kill();
821
+
822
+      bufindw = (bufindw + 1)%BUFSIZE;
823
+      buflen += 1;
824
+
824 825
       serial_count = 0; //clear buffer
825 826
     }
826
-    else
827
-    {
828
-      if(serial_char == ';') comment_mode = true;
829
-      if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
827
+    else if(serial_char == '\\') {  //Handle escapes
828
+       
829
+        if(MYSERIAL.available() > 0  && buflen < BUFSIZE) {
830
+            // if we have one more character, copy it over
831
+            serial_char = MYSERIAL.read();
832
+            cmdbuffer[bufindw][serial_count++] = serial_char;
833
+        }
834
+
835
+        //otherwise do nothing        
836
+    }
837
+    else { // its not a newline, carriage return or escape char
838
+        if(serial_char == ';') comment_mode = true;
839
+        if(!comment_mode) cmdbuffer[bufindw][serial_count++] = serial_char;
830 840
     }
831 841
   }
832 842
   #ifdef SDSUPPORT

Loading…
취소
저장