Operate a Coin Acceptor
The initialization sequence as defined per the MDB Specification is as follows:
M,1R,08R,09R,0F,00R,0F,0100000000R,0AR,0C,000F0000(Poll, in loop)Now, below, there is a python program that does this sequence programatically.
Some remarks:
- This program follows the advised sequence described in Section 5 of the MDB Specification to Initialize a Coin Acceptor.
- The Tube status is always empty
- The program is doing nothing with Coin Inserted events
- The VMC is not enabling any of the Expansion Features that are allowed for level 3 Coin Acceptors.
- If the coin acceptor sends an incorrect message, the sequence is not reinitialized (this is something that must be done if the program is to be used in a real VMC)
- Code is not optimized (e.g. Readline must be called after write2Serial, so this could be put in a function)
- The goal of this example is to demonstrate and keep it simple, this is not suitable for "Real" VMCs. However, it can serve as a starting point.
x
from os import read, writeimport serial,sys,time#Commands that need to be sent to the interface# M,1 -> Activate Master# R,08 -> Reset Coin Changer# R,0B -> Obtain Just Reset from Changer~# R,09# R,0F,00 -> Request additional Features (LVL 3+ Only)# R,0F,01ZZZZZZZZ -> To enable desired features (depends on the changer's reply to previous command) (LVL 3+ Only)# R,0A -> Tube Status# R,0C,000F0000 # No manual Dispense (Last 2 Bytes), Coin Types 1,2,3 and 4 are accepted (000F , on first 2 bytes)# R,0B -> Poll (In Loop)# COMMAND DEFINITIONENABLE_MASTER="M,1"RESET_COIN_ACCEPTOR="R,08"REQUEST_SETUP_INFO="R,09"EXPANSION_REQUEST="R,0F,00"EXPANSION_FEATURE_ENABLE="R,0F,0100000000"TUBE_STATUS_REQUEST="R,0A"COIN_TYPE="R,0C,000F0000"POLL="R,0B"############################################CHANGER RESPONSE TABLEdef initSerialPort(portName): global ser ser = serial.Serial() #Create Serial Object ser.baudrate = 115200 #Set the appropriate BaudRate ser.timeout = 1 #The maximum timeout that the program waits for a reply. If 0 is used, the pot is blocked until readline returns ser.port = portName # Specify the device file descriptor ser.open() #Open the serial connection def readSerial(): global ser s = ser.readline().decode('ascii') # Read the response # DEBUG : print("Read from Serial port: " + s) return sdef write2Serial(message): global ser buff = message + "\n" ser.write(bytes(buff,'ascii')) #Write the version command "V\n" encoded in Binary def initCoinAcceptor(): write2Serial(ENABLE_MASTER) readSerial() write2Serial(RESET_COIN_ACCEPTOR) readSerial() write2Serial(REQUEST_SETUP_INFO) setupInfo = readSerial() print("Changer Setup Info:" + setupInfo) write2Serial(EXPANSION_REQUEST) expansion = readSerial() print("Changer Expansion Response:" + expansion) write2Serial(TUBE_STATUS_REQUEST) readSerial() write2Serial(COIN_TYPE) readSerial() def startPollLoop(): while True: write2Serial(POLL) # Poll Coin Changer print("Changer Response:" + readSerial()) time.sleep(0.5)if __name__ == "__main__": print("Qiba Simple python example to operate a coin acceptor\n\n") if len(sys.argv) != 2: print("Usage: python VMCCoinAcceptor.py <port_name>") exit(1) portName = sys.argv[1] initSerialPort(portName) write2Serial("V") print("Device Firmware version:" + readSerial() + "\n") initCoinAcceptor() startPollLoop()