Created
March 27, 2013 07:42
-
-
Save DarylWM/5252455 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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