Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A simple class for reading values from the DG600F coin acceptor sold by SparkFun (https://www.sparkfun.com/products/11636)

View coin_acceptor.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
#!/usr/bin/env ruby
 
# A simple class for reading values from the DG600F coin acceptor sold by
# SparkFun (https://www.sparkfun.com/products/11636)
#
# The coin acceptor must be configured to send three bytes per coin; see section
# 8.6 of the manual.
#
# You can configure the serial device and baud rate, e.g.:
#
# c = CoinAcceptor.new device: '/dev/ttyS0', baud: 2400
#
# By default it will use /dev/ttyUSB0 at 9600 baud.
#
# You must pass a block which will receive the credit value as configured
# on the coin acceptor:
#
# c.accept_coins do |credit|
# puts "You put in #{credit}s"
# end
#
# For testing at the command line, you can just run this file directly.
 
require 'serialport'
 
class CoinAcceptor
HEADER = 0xAA
IGNORE = 0xFF
 
attr_reader :serial_port
 
def initialize device: "/dev/ttyUSB0", baud: 9600
@serial_port = SerialPort.new device, baud, 8, 1, SerialPort::NONE
@serial_port.flush_input
end
 
def get_byte
byte = IGNORE
byte = @serial_port.read(1).unpack('C').first until byte != IGNORE
byte
end
 
def accept_coins
loop do
if get_byte == HEADER
credit = get_byte
check = get_byte
yield credit if check == HEADER ^ credit
end
end
end
end
 
if __FILE__ == $0
c = CoinAcceptor.new
 
c.accept_coins do |credit|
puts credit
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.