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

+16 -2
View File
@@ -65,6 +65,7 @@ struct arguments {
char *save_png;
int verbose;
int timeout;
char *serial;
};
typedef enum { JOB_CUTMARK, JOB_IMAGE, JOB_PAD, JOB_TEXT, JOB_UNDEFINED } job_type_t;
@@ -109,6 +110,7 @@ static struct argp_option options[] = {
{ "force-tape-width", 6, "<px>", 0, "Set tape width in pixels, use together with --writepng without a printer connected", 1},
{ "copies", 7, "<number>", 0, "Sets the number of identical prints", 1},
{ "timeout", 8, "<seconds>", 0, "Set timeout waiting for finishing previous job. Default:1, or a per-printer minimum where one is known. 0 means infinity", 1},
{ "serial", 12, "<serial>", 0, "Use the printer with this USB serial number", 1 },
{ 0, 0, 0, 0, "print commands:", 2},
{ "image", 'i', "<file>", 0, "Print the given image which must be a 2 color (black/white) png", 2},
@@ -123,6 +125,7 @@ static struct argp_option options[] = {
{ 0, 0, 0, 0, "other commands:", 3},
{ "info", 20, 0, 0, "Show info about detected tape", 3},
{ "list-supported", 21, 0, 0, "Show printers supported by this version", 3},
{ "list-connected", 22, 0, 0, "Show connected printers and their USB serial numbers (for --serial)", 3},
{ 0 }
};
@@ -141,7 +144,8 @@ struct arguments arguments = {
.forced_tape_width = 0,
.save_png = NULL,
.verbose = 0,
.timeout = -1 /* -1 = not given; 0 is already "infinity" */
.timeout = -1, /* -1 = not given; 0 is already "infinity" */
.serial = NULL
};
job_t *jobs = NULL;
@@ -688,6 +692,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case 11: // precut
arguments->precut = true;
break;
case 12: // serial
arguments->serial = arg;
break;
case 'a': // align
if ((strcmp(arg, "c") == 0) || (strcmp(arg, "center") == 0)) {
arguments->align = ALIGN_CENTER;
@@ -709,6 +716,13 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case 21: // list-supported
ptouch_list_supported();
exit(0);
case 22: { // list-connected
int n = ptouch_list_connected();
if (n == 0) {
fprintf(stderr, _("No P-Touch printer found on USB (remember to put switch to position E)\n"));
}
exit(n > 0 ? 0 : 5);
}
case ARGP_KEY_ARG:
argp_failure(state, 1, E2BIG, _("No arguments supported"));
break;
@@ -745,7 +759,7 @@ int main(int argc, char *argv[])
argp_parse(&argp, argc, argv, 0, 0, &arguments);
if (!arguments.forced_tape_width) {
if ((ptouch_open(&ptdev)) < 0) {
if ((ptouch_open_serial(&ptdev, arguments.serial)) < 0) {
return 5;
}
if (ptouch_init(ptdev) != 0) {