Advertisement
Guest User

GCode_Host

a guest
Oct 11th, 2010
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.25 KB | None | 0 0
  1. import processing.serial.*;
  2.  
  3.  
  4. // The serial port
  5. Serial myPort;
  6. String serialLine = "";
  7. String c = "";
  8. boolean started = false;
  9. boolean finished = false;
  10. boolean commandComplete = true;
  11. int commandCount = 0;
  12. int ypos = 20;
  13.  
  14.  
  15. String temperature = "?";
  16.  
  17. String gcode[] = {
  18. };
  19. int gcodeIndex = 0;
  20.  
  21. void setup()
  22. {
  23.   //init stuff
  24.   size(500, 600);
  25.   //smooth();
  26.  
  27.   // List all the available serial ports
  28.   println(Serial.list());
  29.   //open the first port...
  30.   myPort = new Serial(this, Serial.list()[1], 38400);
  31.  
  32.   //load our gcode lines
  33.   gcode = loadStrings("test.gcode");
  34.  // println(gcode);
  35.  background(20, 20, 20);
  36. }
  37.  
  38. void draw()
  39. {
  40.  
  41.   PFont font;
  42.   font = loadFont("ArialMT-48.vlw");
  43.   textFont(font, 16);
  44.   ypos =20 + gcodeIndex*20;
  45.   if(ypos>500)
  46.   {
  47.      background(20, 20, 20);
  48.      ypos = 20;
  49.   }
  50.  
  51.   fill(250, 250, 250);
  52.   String code = "[" +gcodeIndex +"] : "+ c;
  53.   text(code,10,ypos);
  54.   while (myPort.available() > 0)
  55.   {
  56.     int inByte = myPort.read();
  57.  
  58.     if (inByte == '\n')
  59.     {
  60.       println("Got: " + serialLine);
  61.    
  62.       if (match(serialLine, "^start") != null)
  63.         started = true;
  64.  
  65.       if (match(serialLine, "^ok") != null)
  66.       {
  67.         commandComplete = true;
  68.         fill(0, 240, 10);
  69.         text("ok",400,ypos);
  70.        
  71.         if (gcodeIndex == gcode.length)
  72.           println("Job's done!");
  73.  
  74.       }
  75.  
  76.       serialLine = "";
  77.     }
  78.     else if (inByte > 0)
  79.     {
  80.       serialLine += (char)inByte;
  81.     }
  82.   }
  83.  
  84.   if (started)
  85.   {
  86.     if (commandComplete)
  87.     {
  88.       String cmd = getNextCommand();
  89.       commandComplete = false;
  90.  
  91.       if (gcodeIndex == gcode.length)
  92.       {
  93.         finished = true;
  94.        fill(0, 240, 10);
  95.        ypos+=50;
  96.        text("DONE",10,ypos);  
  97.       }
  98.      
  99.       if (cmd != null)
  100.       {
  101.         println("Sent: " +cmd);
  102.         myPort.write(cmd);
  103.       }
  104.     }
  105.   }
  106.  
  107.   if (started && finished)
  108.   {
  109.     println("Job's done!");
  110.     started = false;
  111.     finished = false;
  112.            
  113.   }
  114. }
  115.  
  116. String getNextCommand()
  117. {
  118.   if (gcodeIndex < gcode.length)
  119.   {
  120.  
  121.     c = gcode[gcodeIndex];
  122.     gcodeIndex++;
  123.  
  124.     if (match(c, "^[A-Z]{1}") == null)
  125.       c = getNextCommand();
  126.  
  127.     return c;
  128.   }
  129.   else
  130.     return null;
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement