Created
August 20, 2018 02:26
-
-
Save tenderlove/071cc616744715c7ec90ea5b738350ef 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 'uart' | |
require 'io/wait' | |
class Sample < Struct.new(:time, | |
:pm1_0_standard, :pm2_5_standard, :pm10_standard, | |
:pm1_0_env, :pm2_5_env, | |
:concentration_unit, | |
:particle_03um, :particle_05um, :particle_10um, | |
:particle_25um, :particle_50um, :particle_100um) | |
end | |
uart = UART.open ARGV[0], 9600, '8N1' | |
p Sample.members # header | |
loop do | |
uart.wait_readable | |
start1, start2 = uart.read(2).bytes | |
# According to the data sheet, packets always start with 0x42 and 0x4d | |
unless start1 == 0x42 && start2 == 0x4d | |
# skip a sample | |
uart.read | |
next | |
end | |
length = uart.read(2).unpack('n').first | |
data = uart.read(length) | |
crc = 0x42 + 0x4d + 28 + data.bytes.first(26).inject(:+) | |
data = data.unpack('n14') | |
next unless crc == data.last # crc failure | |
p Sample.new(Time.now.utc, *data.first(12)).to_a | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment