Here is version 0.11 of my Ruby code to send GCODE to a 3D printer using Marlin firmware.
Edit: Current version is 0.17 after tweaking when posting a 3D Benchy sliced with Cura and adding SIGINT actions graceful shutdown actions
Seems to print OK; but of course needs to be tested in many different failure modes.
Note: Have not yet tested with filament in the printer. So far, have only tested watching the movement of the hot-end with no filament.
##############################################
# marlin_printer.rb
# Author: Neo
# URL: https://community.unix.com
# Summary: Send GCODE to a Marlin 3D printer
# Version: 0.11
# Date: 7 November 2021
##############################################
require 'serialport'
def print_gcode
if Rails.application.config.marlin_device.present?
device = Rails.application.config.marlin_device
else
get_device
if Rails.application.config.marlin_device.present?
device = Rails.application.config.marlin_device
else
puts "[#{Time.now}] Connection to device failed!"
return false
end
end
sp = SerialPort.new device
baud_rate = 115200
gcode_file = "./Ender3_Level_Test_v2.gcode"
##########################################################
# these are the supported modem baud rates according to Ruby
# but the ender 3v2 only seems to support rates up to 115200
# 2400 (24)
# 9600 (96)
# 19200 (19, 192)
# 38400 (38, 384)
# 57600 (57, 576)
# 115200 (115, 1152)
# 250000 (250) <- Not supported on Ender 3v2
# 500000 (500) <- Not supported on Ender 3v2
# 1000000 <- Not supported on Ender 3v2
##########################################################
sp.baud=baud_rate
count = 0
puts "[#{Time.now}] Connected to serial port #{device}, baud rate set to: #{sp.baud}"
puts "[#{Time.now}] Read GCODE from file #{gcode_file}."
gcode = []
data = File.readlines(gcode_file)
if data.count < 1
puts "[#{Time.now}] FAILURE: Read GCODE from file #{gcode_file} failed!"
return false
end
data.each do |line|
gcode << line if line[0] != ';'
end
if gcode.count < 1
puts "[#{Time.now}] FAILURE: GCODE file empty after stripping comments!"
return false
end
gcode_lines = gcode.count
word = ''
puts "[#{Time.now}] GCODE File Size to Print: #{gcode_lines} Lines"
sp.write gcode[0]
puts "[#{Time.now}] GCODE Sent #{gcode[count].chomp} OK: #{device}"
count += 1
# insure we reach the end of the code file
# fix this later to me more accurate
# marlin firmware seems not to return "ok" for some commands
# or my "ok" detection needs tweaking.
remaining_lines = (gcode_lines *1.05).to_i
remaining_lines.times do |n|
begin
test_line = sp.readline("\n")
puts "[#{Time.now}] PRINTER Line #{count} Sent #{test_line.chomp} from GCODE #{gcode[count].chomp}"
if test_line.downcase.chomp == 'ok'
percent_file_remaining = (100.0 * count.to_f/remaining_lines.to_f ).round(2)
puts "[#{Time.now}] GCODE Sent Line #{count} #{gcode[count].chomp}"
count += 1
sp.write gcode[count]
puts "[#{Time.now}] GCODE Sent Line #{count} #{gcode[count].chomp} - File Progress: #{percent_file_remaining}%"
end
rescue
puts "[#{Time.now}] GCODE EXCEPTION #{count} Lines of #{gcode_lines} - File Progress: #{percent_file_remaining}%"
if count >= gcode_lines
remaining_lines = count
break
end
#exception occurs then nothing to read and read times out
next
end
end
percent_file_remaining = (100.0 * count.to_f/remaining_lines.to_f ).round(2)
puts "[#{Time.now}] GCODE Completed #{count} Lines of #{gcode_lines} - File Progress: #{percent_file_remaining}%"
end
Sample Beginning Output:
2.7.1 :002 > print_gcode
[2021-11-07 12:18:50 +0700] Connected to serial port /dev/cu.wchusbserial140, baud rate set to: 115200
[2021-11-07 12:18:50 +0700] Read GCODE from file ./Ender3_Level_Test_v2.gcode.
[2021-11-07 12:18:50 +0700] GCODE File Size to Print: 6013 Lines
[2021-11-07 12:18:50 +0700] GCODE Sent M190 S70 OK: /dev/cu.wchusbserial140
[2021-11-07 12:18:50 +0700] PRINTER Line 1 Sent ok from GCODE M104 S220
[2021-11-07 12:18:50 +0700] GCODE Sent Line 1 M104 S220
[2021-11-07 12:18:50 +0700] GCODE Sent Line 2 M109 S200 - File Progress: 0.02%
[2021-11-07 12:18:50 +0700] PRINTER Line 2 Sent //action:notification Bed Heating... from GCODE M109 S200
[2021-11-07 12:18:50 +0700] PRINTER Line 2 Sent T:200.05 /200.00 B:69.99 /70.00 @:75 B@:35 W:? from GCODE M109 S200
[2021-11-07 12:18:51 +0700] PRINTER Line 2 Sent T:200.03 /200.00 B:69.97 /70.00 @:76 B@:46 W:12 from GCODE M109 S200
[2021-11-07 12:18:52 +0700] PRINTER Line 2 Sent echo:busy: processing from GCODE M109 S200
[2021-11-07 12:18:52 +0700] PRINTER Line 2 Sent T:200.07 /200.00 B:69.96 /70.00 @:75 B@:48 W:11 from GCODE M109 S200
[2021-11-07 12:18:53 +0700] PRINTER Line 2 Sent T:200.03 /200.00 B:69.95 /70.00 @:77 B@:52 W:10 from GCODE M109 S200
[2021-11-07 12:18:54 +0700] PRINTER Line 2 Sent T:199.99 /200.00 B:69.97 /70.00 @:78 B@:43 W:9 from GCODE M109 S200
[2021-11-07 12:18:54 +0700] PRINTER Line 2 Sent echo:busy: processing from GCODE M109 S200
[2021-11-07 12:18:55 +0700] PRINTER Line 2 Sent T:200.03 /200.00 B:69.96 /70.00 @:76 B@:52 W:8 from GCODE M109 S200
[2021-11-07 12:18:56 +0700] PRINTER Line 2 Sent T:200.05 /200.00 B:69.99 /70.00 @:76 B@:38 W:7 from GCODE M109 S200
[2021-11-07 12:18:56 +0700] PRINTER Line 2 Sent echo:busy: processing from GCODE M109 S200
[2021-11-07 12:18:57 +0700] PRINTER Line 2 Sent T:200.03 /200.00 B:69.97 /70.00 @:76 B@:47 W:6 from GCODE M109 S200
[2021-11-07 12:18:58 +0700] PRINTER Line 2 Sent T:200.05 /200.00 B:70.04 /70.00 @:76 B@:20 W:5 from GCODE M109 S200
[2021-11-07 12:18:58 +0700] PRINTER Line 2 Sent echo:busy: processing from GCODE M109 S200
[2021-11-07 12:18:59 +0700] PRINTER Line 2 Sent T:200.05 /200.00 B:70.01 /70.00 @:76 B@:36 W:4 from GCODE M109 S200
[2021-11-07 12:19:00 +0700] PRINTER Line 2 Sent T:200.03 /200.00 B:70.01 /70.00 @:76 B@:38 W:3 from GCODE M109 S200
[2021-11-07 12:19:00 +0700] PRINTER Line 2 Sent echo:busy: processing from GCODE M109 S200
[2021-11-07 12:19:01 +0700] PRINTER Line 2 Sent T:200.05 /200.00 B:70.01 /70.00 @:76 B@:36 W:2 from GCODE M109 S200
[2021-11-07 12:19:02 +0700] PRINTER Line 2 Sent T:200.03 /200.00 B:70.01 /70.00 @:76 B@:39 W:1 from GCODE M109 S200
[2021-11-07 12:19:02 +0700] PRINTER Line 2 Sent echo:busy: processing from GCODE M109 S200
[2021-11-07 12:19:03 +0700] PRINTER Line 2 Sent T:200.03 /200.00 B:70.01 /70.00 @:76 B@:37 W:0 from GCODE M109 S200
...
Sample Ending Output:
2021-11-07 13:04:21 +0700] PRINTER Line 6007 Sent ok from GCODE M106 S0 ; turn off cooling fan
[2021-11-07 13:04:21 +0700] GCODE Sent Line 6007 M106 S0 ; turn off cooling fan
[2021-11-07 13:04:21 +0700] GCODE Sent Line 6008 M104 S0 ; turn off extruder - File Progress: 95.15%
[2021-11-07 13:04:21 +0700] PRINTER Line 6008 Sent ok from GCODE M104 S0 ; turn off extruder
[2021-11-07 13:04:21 +0700] GCODE Sent Line 6008 M104 S0 ; turn off extruder
[2021-11-07 13:04:21 +0700] GCODE Sent Line 6009 M140 S0 ; turn off bed - File Progress: 95.17%
[2021-11-07 13:04:21 +0700] PRINTER Line 6009 Sent //action:notification Bed Cooling... from GCODE M140 S0 ; turn off bed
[2021-11-07 13:04:21 +0700] PRINTER Line 6009 Sent //action:notification Ender-3 V2 Ready. from GCODE M140 S0 ; turn off bed
[2021-11-07 13:04:21 +0700] PRINTER Line 6009 Sent ok from GCODE M140 S0 ; turn off bed
[2021-11-07 13:04:21 +0700] GCODE Sent Line 6009 M140 S0 ; turn off bed
[2021-11-07 13:04:21 +0700] GCODE Sent Line 6010 M84 ; disable motors - File Progress: 95.18%
[2021-11-07 13:04:23 +0700] PRINTER Line 6010 Sent echo:busy: processing from GCODE M84 ; disable motors
[2021-11-07 13:04:25 +0700] PRINTER Line 6010 Sent echo:busy: processing from GCODE M84 ; disable motors
[2021-11-07 13:04:27 +0700] PRINTER Line 6010 Sent ok from GCODE M84 ; disable motors
[2021-11-07 13:04:27 +0700] GCODE Sent Line 6010 M84 ; disable motors
[2021-11-07 13:04:27 +0700] GCODE Sent Line 6011 M82 ;absolute extrusion mode - File Progress: 95.2%
[2021-11-07 13:04:27 +0700] PRINTER Line 6011 Sent ok from GCODE M82 ;absolute extrusion mode
[2021-11-07 13:04:27 +0700] GCODE Sent Line 6011 M82 ;absolute extrusion mode
[2021-11-07 13:04:27 +0700] GCODE Sent Line 6012 M104 S0 - File Progress: 95.22%
[2021-11-07 13:04:27 +0700] PRINTER Line 6012 Sent //action:notification Ender-3 V2 Ready. from GCODE M104 S0
[2021-11-07 13:04:27 +0700] PRINTER Line 6012 Sent ok from GCODE M104 S0
[2021-11-07 13:04:27 +0700] GCODE Sent Line 6012 M104 S0
[2021-11-07 13:04:27 +0700] GCODE EXCEPTION 6013 Lines of 6013 - File Progress: 95.23%
[2021-11-07 13:04:27 +0700] GCODE Completed 6013 Lines of 6013 - File Progress: 100.0%
=> nil
.....
See Also: