add --serial and --list-connected to pick one of several printers

When more than one printer is connected, ptouch-print used the first
one it found. --serial <serial> now uses only the printer with that USB
serial number, and --list-connected lists the connected printers with
their serials.

libptouch gets ptouch_open_serial() and ptouch_list_connected();
ptouch_open() keeps its signature and calls ptouch_open_serial() with
NULL. The serial is checked before the P-Lite and unsupported-raster
checks, so a different printer in the wrong mode no longer ends the
search, and the "found on USB bus" line now shows the serial. The early
error returns in the open loop now free the device list.
This commit is contained in:
James A authored and Dominic Radermacher committed 2026-09-25 09:51:25 +02:00
1 parent d05d31bb86
commit 7fa1e1ac46
4 files changed
+115 -9

No files matched your search

+90 -7
View File
@@ -112,6 +112,13 @@ struct _pt_dev_info ptdevs[] = {
};
int ptouch_open(ptouch_dev *ptdev)
{
return ptouch_open_serial(ptdev, NULL);
}
/* Like ptouch_open(), but when serial is not NULL only a printer whose
USB serial number matches it is used. */
int ptouch_open_serial(ptouch_dev *ptdev, const char *serial)
{
libusb_device **devs;
libusb_device *dev;
@@ -148,22 +155,43 @@ int ptouch_open(ptouch_dev *ptdev)
}
for (int k=0; ptdevs[k].vid > 0; ++k) {
if ((desc.idVendor == ptdevs[k].vid) && (desc.idProduct == ptdevs[k].pid) && (ptdevs[k].flags >= 0)) {
fprintf(stderr, _("%s found on USB bus %d, device %d\n"),
unsigned char buf[64] = {0};
if ((r=libusb_open(dev, &handle)) != 0) {
fprintf(stderr, _("libusb_open error :%s\n"), libusb_error_name(r));
if (serial) {
/* can't read its serial, so it can't be the one we want */
break;
}
libusb_free_device_list(devs, 1);
return -1;
}
if (desc.iSerialNumber) {
libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buf, sizeof(buf));
}
fprintf(stderr, _("%s found on USB bus %d, device %d, serial %s\n"),
ptdevs[k].name,
libusb_get_bus_number(dev),
libusb_get_device_address(dev));
libusb_get_device_address(dev),
buf[0] ? (char *)buf : "-");
/* check the serial before the P-Lite/unsupported checks, so
a different printer in the wrong mode doesn't end the search */
if (serial && (strcmp((char *)buf, serial) != 0)) {
libusb_close(handle);
handle = NULL;
break; /* next device */
}
if (ptdevs[k].flags & FLAG_PLITE) {
printf("Printer is in P-Lite Mode, which is unsupported\n\n");
printf("Turn off P-Lite mode by changing switch from position EL to position E\n");
printf("or by pressing the PLite button for ~ 2 seconds (or consult the manual)\n");
libusb_close(handle);
libusb_free_device_list(devs, 1);
return -1;
}
if (ptdevs[k].flags & FLAG_UNSUP_RASTER) {
printf("Unfortunately, that printer currently is unsupported (it has a different raster data transfer)\n");
return -1;
}
if ((r=libusb_open(dev, &handle)) != 0) {
fprintf(stderr, _("libusb_open error :%s\n"), libusb_error_name(r));
libusb_close(handle);
libusb_free_device_list(devs, 1);
return -1;
}
libusb_free_device_list(devs, 1);
@@ -186,7 +214,11 @@ int ptouch_open(ptouch_dev *ptdev)
}
}
}
fprintf(stderr, _("No P-Touch printer found on USB (remember to put switch to position E)\n"));
if (serial) {
fprintf(stderr, _("No P-Touch printer with serial %s found\n"), serial);
} else {
fprintf(stderr, _("No P-Touch printer found on USB (remember to put switch to position E)\n"));
}
libusb_free_device_list(devs, 1);
return -1;
}
@@ -555,6 +587,57 @@ void ptouch_list_supported()
return;
}
/* Print every connected P-Touch printer with its USB serial number, which
can then be passed to ptouch_open_serial(). Returns the number of
printers found, or -1 on error. */
int ptouch_list_connected()
{
libusb_device **devs;
libusb_device *dev;
libusb_device_handle *handle;
struct libusb_device_descriptor desc;
int r, i=0, found=0;
if ((libusb_init(NULL)) < 0) {
fprintf(stderr, _("libusb_init() failed\n"));
return -1;
}
if (libusb_get_device_list(NULL, &devs) < 0) {
libusb_exit(NULL);
return -1;
}
while ((dev=devs[i++]) != NULL) {
if (libusb_get_device_descriptor(dev, &desc) < 0) {
continue;
}
for (int k=0; ptdevs[k].vid > 0; ++k) {
if ((desc.idVendor == ptdevs[k].vid) && (desc.idProduct == ptdevs[k].pid) && (ptdevs[k].flags >= 0)) {
unsigned char buf[64] = {0};
const char *serial = "-";
if ((r=libusb_open(dev, &handle)) != 0) {
serial = libusb_error_name(r);
} else {
if (desc.iSerialNumber && (libusb_get_string_descriptor_ascii(handle, desc.iSerialNumber, buf, sizeof(buf)) > 0)) {
serial = (char *)buf;
}
libusb_close(handle);
}
printf(_("%s\tserial %s\t(USB bus %d, device %d)%s\n"),
ptdevs[k].name, serial,
libusb_get_bus_number(dev),
libusb_get_device_address(dev),
(ptdevs[k].flags & FLAG_PLITE) ? _(" - in P-Lite mode, unsupported") :
(ptdevs[k].flags & FLAG_UNSUP_RASTER) ? _(" - unsupported") : "");
++found;
break;
}
}
}
libusb_free_device_list(devs, 1);
libusb_exit(NULL);
return found;
}
const char* pt_mediatype(const uint8_t media_type)
{
switch (media_type) {