Skip to content

Instantly share code, notes, and snippets.

@bithive
Created December 20, 2013 23:08
Show Gist options
  • Save bithive/8063097 to your computer and use it in GitHub Desktop.
Save bithive/8063097 to your computer and use it in GitHub Desktop.
A simple class for reading values from the DG600F coin acceptor sold by SparkFun (https://www.sparkfun.com/products/11636)
#!/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
@hjanuschka
Copy link

hey - nice gist, do you have a wiring plan on how i can connect the acceptor via usb?
i don't get it, searched for hours on google - i am bit of an electronic noob.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment