Skip to content

Instantly share code, notes, and snippets.

@DarylWM
Created March 27, 2013 07:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DarylWM/5252455 to your computer and use it in GitHub Desktop.
Save DarylWM/5252455 to your computer and use it in GitHub Desktop.
require 'serialport'
require 'json'
require 'base64'
sp = SerialPort.new "/dev/cu.usbmodem1d11", 115200, 8, 1, SerialPort::NONE
imageBytes = ""
loop {
jsonMessage = sp.gets.tr("\n\r", "")
begin
parsedMessage = JSON(jsonMessage)
if parsedMessage["type"] == "inf"
puts parsedMessage["msg"]
elsif parsedMessage["type"] == "img"
imageBytes += Base64.decode64(parsedMessage["msg"]["seg"]["enc"])
if parsedMessage["msg"]["seg"]["rem"] <= 0
# Write the image bytes to a file
File.open("photo.jpg", "wb") do |f|
f.write(imageBytes)
end
# Clear the image bytes for the next one
imageBytes = ""
puts "Wrote an image to the file. Waiting for the next one."
end
end
rescue Exception => e
puts e.message
imageBytes = ""
end
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment