Home > Enterprise >  Python print to POS printer
Python print to POS printer

Time:09-07

I am trying to print order lines with a POS printer.

I connected to the printer via it's IP address.

The printer is printing, but it takes a long time (up to 2 minutes).

This is what I have:

def send_to_printer(data):
    # connect
    SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    SOCKET.connect((HOST, PORT)) 
    print('connected')
    xon = "DC1".encode('ascii', 'replace')
    SOCKET.send(xon) 
    # symbols
    symbol_for_value = "H"
    symbol_for_qty = "*"
    symbol_for_department = "1R"
    symbol_for_subtotal = "="
    symbol_for_payment = "1R4T"

    # lines
    for line in data['order_lines']:
        product_name_to_print = str(line['product_name'])[:15]
        product_qty_to_print = str(int(line['qty']))
        product_price_to_print = str(int(line['price'] * 100))
        block = "{}{}{}{}{}{}".format(
            product_qty_to_print, 
            symbol_for_qty, 
            product_price_to_print, 
            symbol_for_value,
            '"'  product_name_to_print  '"', 
            symbol_for_department
        ).encode('ascii', 'replace')
        SOCKET.send(block) 

        product_name_to_display = str(line['product_name'])[:20]
        product_price_to_display = str(line['price'])
        terminator = '"{}: {}"1%"'.format(product_name_to_display, product_price_to_display).encode('ascii', 'replace')
        SOCKET.send(terminator) 

    # ----------------- subtotal
    terminator = symbol_for_subtotal.encode('ascii', 'replace')
    SOCKET.send(terminator) 
    # ----------------- payments    
    block = symbol_for_payment.encode('ascii', 'replace')
    SOCKET.send(block) 

    terminator = '"{}"1%"'.format("THANK YOU").encode('ascii', 'replace')
    SOCKET.send(terminator) 
    #
    xoff = "DC3".encode('ascii', 'replace')
    SOCKET.send(xoff) 
    #
    SOCKET.close()
    exit()

I am not sure how to handle this XON/XOFF commands and the documentation of the printer is not very clear about it.

Thank you for any suggestions

CodePudding user response:

There was a special setting "no echo" in the printer settings that has to be turned off. Now it's printing fast

  • Related