Skip to content

Instantly share code, notes, and snippets.

@cyrille
Last active January 1, 2016 04:48
Show Gist options
  • Save cyrille/8093811 to your computer and use it in GitHub Desktop.
Save cyrille/8093811 to your computer and use it in GitHub Desktop.
require "serialport"
#params for serial port
port_str = "/dev/ttyUSB0" #may be different for you
baud_rate = 9600
data_bits = 8
stop_bits = 1
parity = SerialPort::NONE
#@sp = SerialPort.new("/dev/ttyUSB0", 9600, 8, 1, SerialPort::NONE)
sp = SerialPort.new(port_str, baud_rate, data_bits, stop_bits, parity)
sp.sp.read_timeout=1500
SOF = [0x07, 0xF0]
EOF = [0x07, 0x0F]
SPEED = [0x00, 0x99]
SPEED_AUTO = SPEED + [0x00]
SPEED_0 = SPEED + [0x01]
SPEED_1 = SPEED + [0x01]
SPEED_2 = SPEED + [0x02]
SPEED_3 = SPEED + [0x03]
def checksum(ary)
[(ary<<173).inject(:+)].pack("C*").unpack("H*").first
end
def speed(num)
if num >= 0 && num <= 4
write_frame(eval("SPEED_#{num}"))
end
end
def auto_speed
write_frame(SPEED_AUTO)
end
def write_frame(ary)
f = SOF+ary+[checksum(ary)]+EOF
puts f.map{|s| s.to_s(16)}
@sp.write(f.pack("C*"))
end
def read
@sp.readline
end
sp.close
def some_examples_for_test_using_irb
# set speed to 0
sp.write([0x07, 0xF0, 0x00, 0x99, 0x01, 0x01, 0x48, 0x07, 0x0F].pack("C*"))
# set speed to 1
sp.write([0x07, 0xF0, 0x00, 0x99, 0x01, 0x02, 0x49, 0x07, 0x0F].pack("C*"))
# set speed to 2
sp.write([0x07, 0xF0, 0x00, 0x99, 0x01, 0x03, 0x4A, 0x07, 0x0F].pack("C*"))
# set speed to 3
sp.write([0x07, 0xF0, 0x00, 0x99, 0x01, 0x04, 0x4B, 0x07, 0x0F].pack("C*"))
# get temperatures
sp.write([0x07, 0xF0, 0x00, 0x0F, 0x00, 0xBC, 0x07, 0x0F].pack("C*"))
# get firmware and version
sp.write([0x07, 0xF0, 0x00, 0x69, 0x00, 0x16, 0x07, 0x0F].pack("C*"))
# read
sp.readline
end
def some_response_exemples
# "\a\xF3\a\xF0\u0000j\r\u0003< CA350 luxe}\a\u000F" (firmware and model)
# => [7, 243, 7, 240, 0, 106, 13, 3, 60, 32, 67, 65, 51, 53, 48, 32, 108, 117, 120, 101, 125, 7, 15] (using .bytes)
# "\a\xF3\a\xF0\u0000\u0010\u0004@PQD\xE6\a\u000F" (temperature)
# => [7, 243, 7, 240, 0, 16, 4, 64, 80, 81, 68, 230, 7, 15]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment