No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

send_gcode.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env python
  2. import sys
  3. import serial
  4. if len(sys.argv) < 3:
  5. print("Usage:")
  6. print(" " + sys.argv[0] + " /dev/serial/port input.gcode")
  7. sys.exit(1)
  8. port_file = sys.argv[1]
  9. in_file = sys.argv[2]
  10. port = serial.Serial()
  11. port.port = port_file
  12. port.baudrate = 115200
  13. #port.timeout = 0.0
  14. max_cmd_buffer = 5
  15. counter = 0
  16. def connect_serial():
  17. try:
  18. port.open()
  19. if port.is_open:
  20. print("connected to: " + port_file)
  21. else:
  22. print("error connecting to " + port_file)
  23. sys.exit(1)
  24. except serial.serialutil.SerialException:
  25. print("error connecting to " + port_file)
  26. sys.exit(1)
  27. def wait_for_text(s):
  28. while True:
  29. response = port.read_until().decode().strip()
  30. print("rx w: " + response)
  31. if response.startswith(s):
  32. break
  33. elif response.startswith("Error"):
  34. print(response)
  35. print("CNC returned error. aborting.")
  36. abort_print(False)
  37. port.close()
  38. sys.exit(1)
  39. def abort_print(wait_for_oks = True):
  40. global counter
  41. print("tx: M5")
  42. port.write("M5\n".encode())
  43. counter += 1
  44. print("tx: G0 X0 Y0 F1000")
  45. port.write("G0 X0 Y0 F1000\n".encode())
  46. counter += 1
  47. if wait_for_oks:
  48. while counter > 0:
  49. wait_for_text("ok")
  50. counter -= 1
  51. def send_file(f):
  52. global counter
  53. for line in f:
  54. if ";" in line:
  55. line = line.split(";")[0]
  56. line = line.strip()
  57. if len(line) <= 0:
  58. print("skipping empty line")
  59. continue
  60. print("tx: " + line)
  61. tx = line.encode() + b'\n'
  62. port.write(tx)
  63. counter += 1
  64. print("cnt=" + str(counter))
  65. while counter >= max_cmd_buffer:
  66. response = port.read_until().decode().strip()
  67. #print("rx: " + response)
  68. if response.startswith("ok"):
  69. counter -= 1
  70. print("cnt=" + str(counter))
  71. elif response.startswith("Error"):
  72. print(response)
  73. print("CNC returned error. aborting.")
  74. abort_print(False)
  75. port.close()
  76. sys.exit(1)
  77. def main():
  78. connect_serial()
  79. print("waiting for CNC to reset")
  80. wait_for_text("start")
  81. print("auto-homing after reset")
  82. print("tx: G28")
  83. port.write("G28\n".encode())
  84. wait_for_text("ok")
  85. try:
  86. with open(in_file, 'r') as f:
  87. send_file(f)
  88. while counter > 0:
  89. wait_for_text("ok")
  90. counter -= 1
  91. print("workaround. kill when done.")
  92. while True:
  93. pass
  94. except KeyboardInterrupt:
  95. print("user interrupt. aborting.")
  96. abort_print(True)
  97. port.close()
  98. if __name__ == '__main__':
  99. main()