C++ Linux: OpenPrinter equivalent

C++ Linux: OpenPrinter equivalent

本文关键字:equivalent OpenPrinter Linux C++      更新时间:2023-10-16

我使用Qt.我需要直接写一些文本到我的打印机。在windows上,我可以使用winapi OpenPrinter和WritePrinter来做到这一点,但我需要这个代码在linux上运行,但我在linux上找不到这个函数OpenPrinter。我知道我可以像这样直接写入linux/dev/usb/lpx文件:

char *dev = "/dev/usb/lp0";
int printerfile = open(dev, O_RDWR | O_NOCTTY | O_NDELAY) ;
if (printerfile < 0)
{
        cout << "Failed to open "<<dev;
        exit(-1);
}
string str = "test 12345";
write(printerfile,str.c_str(),str.length());

但这需要设备路径,如/dev/usb/lp0,我只能从Qt中获得打印机名称,如EPSON LX-300+。

您可以尝试通过命令

了解更多关于Linux操作系统附加打印机的信息:
[udevadm info -q all -n /dev/usb/lp0]

输出如下:

-------------
P: /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-
1.2:1.0/usbmisc/lp0
N: usb/lp0
E: DEVNAME=/dev/usb/lp0
E: DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-
1.2:1.0/usbmisc/lp0
E: MAJOR=180
E: MINOR=0
E: SUBSYSTEM=usbmisc
E: UDEV_LOG=3
-------------