Fixing gcode-sender.rb / serialport.rb "invalid byte sequence in UTF-8 (ArgumentError)"
Somehow, most of the time my Arduino-based EggBot clone outputs one byte of undetermined garbage before saying "Ready". This caused gcode-sender.rb to fail with "invalid byte sequence in UTF-8 (ArgumentError)".
Eventually I found the solution in a post by Wayne Brissette here: https://www.ruby-forum.com/topic/4409620. Basically you need to treat the string as binary instead of utf-8. This small change in the code that waits for the robot to be ready fixed the issue for me:
Eventually I found the solution in a post by Wayne Brissette here: https://www.ruby-forum.com/topic/4409620. Basically you need to treat the string as binary instead of utf-8. This small change in the code that waits for the robot to be ready fixed the issue for me:
--- gcode-sender.rb.orig
+++ gcode-sender.rb
@@ -30,7 +30,7 @@
sp.read_timeout = 0 # Necessary for Windows.
while line = sp.gets
print line
- break if line.match(/^Ready/)
+ break if line.force_encoding("BINARY").match(/^.?Ready/)
end
File.foreach(filename) do |line|
line.chomp!
Comments
Display comments as Linear | Threaded