Search code examples
pythonvalidationserial-portpyserial

MEI cash validator programming with Python


I am trying to communicate with MEI Advance Cash Validator using Python, but have no success so far. I am using pyserial library, the machine is connected to COM Port. Also, I am programming under Ubuntu 18.

Whatever I send to the machine is ignored and I have no reply. I am trying to follow the official documentation, and it says the format should be as follows: STX (0x02) - start of the message Length - the number of bytes in the message Message Type Data Fields EXT (0x03) - end of the message Checksum - XORed checksum

Here is what I try:

ser = serial.Serial(
    port='/dev/ttyS0',
    baudrate = 9600,
    #parity=serial.PARITY_ODD,
    #stopbits=serial.STOPBITS_TWO,
    bytesize=serial.SEVENBITS
)

def send(msg,ser):
    r = chr(0x02)
    r += "".join([chr(i) for i in msg])
    check = 0
    for a in msg:
        check = check^a
    r += chr(0x03)
    r += chr(check)
    print("Sending", r.encode())
    ser.write(r.encode())

m1 = [0x08,0x10,0x1F,0x14,0x00] #1F
m2 = [0x08,0x11,0x1F,0x15,0x00]

for i in range(5):
    send(m1, ser)
    send(m2, ser)
    time.sleep(1)
    out = 0
    while ser.inWaiting() > 0:
        print(ser.read(1),end="")
        out += 1
    if out > 0:
        print()
ser.close()

And the result is:

/home/timur/local/kmf/venv/bin/python /home/timur/local/kmf/example.py
Sending b'\x02\x08\x10\xc2\x9f\x14\x00\x03\xc2\x93'
Sending b'\x02\x08\x11\xc2\x9f\x15\x00\x03\xc2\x93'
Sending b'\x02\x08\x10\xc2\x9f\x14\x00\x03\xc2\x93'
Sending b'\x02\x08\x11\xc2\x9f\x15\x00\x03\xc2\x93'
Sending b'\x02\x08\x10\xc2\x9f\x14\x00\x03\xc2\x93'
Sending b'\x02\x08\x11\xc2\x9f\x15\x00\x03\xc2\x93'
Sending b'\x02\x08\x10\xc2\x9f\x14\x00\x03\xc2\x93'
Sending b'\x02\x08\x11\xc2\x9f\x15\x00\x03\xc2\x93'
Sending b'\x02\x08\x10\xc2\x9f\x14\x00\x03\xc2\x93'
Sending b'\x02\x08\x11\xc2\x9f\x15\x00\x03\xc2\x93'

Process finished with exit code 0

And so the machine is not answering anything at all :(

Any help would be highly appreciated!!!


Solution

  • You're close. The protocol requires that you listen for the message and then toggle the third byte every time the target responds correctly. So instead of sending those two message in succession, send one, read and parse the validator's response, then toggle your ACK byte. Also, your checksum is wrong. The last byte should be the 8-bit XOR of the payload portion of the packet, that is the 6 bytes AFTER the start command byte. The final packet should be 8 bytes in length.

    The MEI protocol is compatible with the Apex RS-232 bill validator API. I have a Python host written here that should work for you.

    Disclaimer: I work for Pyramid Technologies