Link to home
Start Free TrialLog in
Avatar of Rocco Galati
Rocco Galati

asked on

How to translate Python code into C/C++ code for a printer

I'm having serious problems to make my kiosk printer (Nippon Primex NP-VK30) work correctly under Ubuntu 19.04.
The manufacturer provides no support for Ubuntu, but I've found on Github a very simple library made in Python.
I attached the Python files to this topic.

I tried to launch "test.py" and my printer worked fine!

Since I need to print some text strings from my C/C++ application, I would like to translate the python code into C/C++ code so I can easily import it in my original application.

Unfortunately, I'm just a beginner and I never coded in Python.
Can you help me, please?

Thank you!

This is the code for test.py:

#!/usr/bin/python
from w2k203dpi import Printer

p = Printer()

p.println('test')

p.bold(True)
p.println('test')
p.bold(False)

p.underline(True)
p.println('test')
p.underline(False)

p.qrcode('test')

p.fullcut()

Open in new window


This is the code for the class file:

class Printer:

    def __init__(self):
        DEVPATH = '/dev/usb/lp0'
        self.f = open(DEVPATH, 'w')
        self.mode = 0x00

    def raw(self, data):
        for i in data:
           self.f.write(i)
        self.f.flush()

    def esc(self, data):
        self.raw('\x1b' + data)

    def font(self, value):
        if value:
            self.mode |= (1 << 0)
        else:
            self.mode &= ~(1 << 0)
        self.esc('!' + chr(self.mode))

    def bold(self, value):
        if value:
            self.mode |= (1 << 3)
        else:
            self.mode &= ~(1 << 3)
        self.esc('!' + chr(self.mode))

    def dblheight(self, value):
        if value:
            self.mode |= (1 << 4)
        else:
            self.mode &= ~(1 << 4)
        self.esc('!' + chr(self.mode))

    def dblwidth(self, value):
        if value:
            self.mode |= (1 << 5)
        else:
            self.mode &= ~(1 << 5)
        self.esc('!' + chr(self.mode))

    def underline(self, value):
        if value:
            self.mode |= (1 << 7)
        else:
            self.mode &= ~(1 << 7)
        self.esc('!' + chr(self.mode))

    def partialcut(self):
        for i in range(3): self.println()
        self.esc('m')

    def fullcut(self):
        for i in range(3): self.println()
        self.esc('i')

    def println(self, s = ''):
        self.f.write(s + '\n')
        self.f.flush()

    def qrcode(self, data, pixelsize = 8):
        correction = 0   # 0 = L, 1 = M, 2 = Q, 3 = H
        version = 0      # model/version
        mask = 0         # mask pattern
        datalen = len(data)
        self.esc('\x71' + chr(pixelsize) + chr(correction) + chr(version) + chr(mask) + chr(datalen % 256) + chr(datalen / 256) + data)
        self.println()

Open in new window

test.py
w2k203dpi.py
Avatar of David Favor
David Favor
Flag of United States of America image

Tip: If you have all this working in Python, keeping this Python code will be far easier to maintain than converting to C/C++.

https://www.primex.co.jp/NPIServlet?viewpage=4098&nextpage=4099&BLGCode=10&ModelCode=56 specifically states Primex provides Linux + Linux ARM + Android drivers, so Linux looks to be fully supported.

If you have problems installing the Linux driver or working with the driver from various languages (like C/C++), best open a support ticket with Primex.

For anyone to answer your question here, they'd require having a Primex NP-VK30 to work with.

You might find someone who has this physical device + experience working with this device from Linux.

If not, a Primex support ticket will likely be best.

Note: Working with Linux printers can be a serious time drain. If you have Python code that's working, I suggest you keep using the Python code. You'll save yourself a massive amount of time.

Hint: If you think you must do this from C/C++ then looks like you'll first have to port w2k203dpi.py to C/C++ as this seems to implement the escape sequences required to run your printer.

Which suggests... you already have the Linux Driver installed + working, then the w2k203dpi.py simply interacts with the installed driver.
Avatar of Rocco Galati
Rocco Galati

ASKER

The primex staff provided no support to me.

the python code really works fine, but I can't use it from my C/C++ application as it is.

so, should I write a C++ class like the one present in the w2k203dpi.py?
it won't be easy for me because I do not know what that python code does.
the python code really works fine, but I can't use it from my C/C++ application as it is.

Fairly easy to do.

1) Create a simple print queue.

2) Write the output file from your C/C++ code.

3) Use a Python script to print the queued file.

4) Delete the file after it prints.

So... exact same way normal system level print queues work.

Or you can port w2k203dpi.py which will just take a good bit of time to do.

Nothing special about the Python code, you'll just read it + port it, line by line.

Python, PERL, Ruby, C. All languages are pretty much the same. Only difference with C/C++ is these languages have no forgiveness for memory bounds management, so it's easy to corrupt memory.

Python, PERL, Ruby, etc... are much easy to get code working, as it's nearly impossible to corrupt memory.
I'm using C/C++ with Wxwidgets to implement a Kiosk application, at the end of the procedure, the printer must print a ticket for the user.

I would like to avoid to call a python script from my C code every time, I would like to do it directly in C.

I'm trying to convert that python class but it is not easy for my skills.
I was able to send a text string to my printer by using C!

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
        FILE *printer = fopen("/dev/usb/lp0", "w");

        if (printer == NULL)
                return 1;
        
        printf("OK\n");
        fprintf(printer, "alsdjakljdlaskjd\n\n\n\n");
        pclose(printer);
        return 0;
}

Open in new window


The problem is that the printer doesn't cut the paper.
It is a thermal 80x80 mm printer and it should cut the paper after the printing process.
The python script is able to but the paper, but I do not know how to do it in C.

Can you help me with this, please?
I solved the problem, there was a specific control character to send to the printer to enable the cut.

everything is ok now, thank you for your help.
ASKER CERTIFIED SOLUTION
Avatar of Rocco Galati
Rocco Galati

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial