Skip to content

Commit

Permalink
fix: correct command format
Browse files Browse the repository at this point in the history
  • Loading branch information
hans00 committed Dec 24, 2024
1 parent f95d6bc commit f5e166c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,12 @@ class Printer {
await (this._connection as UsbConnection).send(command);
break;
case ConnectionType.NET:
await (this._connection as net.Socket).write(command);
break;
return new Promise((resolve, reject) => {
(this._connection as net.Socket).write(command, 'binary', (err) => {
if (err) reject(err);
else resolve();
});
});
case ConnectionType.BLUETOOTH:
await (this._connection as BlueConnection).send(command);
break;
Expand Down
11 changes: 8 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { Buffer } from 'buffer';

const CRLF = Buffer.from('\r\n');

const COMMA = Buffer.from(',');

type CommandArg = string | number | Buffer;

export const buildCommand = (command: string, ...args: CommandArg[]) => {
const buffers = [Buffer.from(`${command} `)];
for (const arg of args) {
args.forEach((arg, index) => {
if (index > 0) {
buffers.push(COMMA);
}
if (Buffer.isBuffer(arg)) {
buffers.push(arg as Buffer);
} else {
buffers.push(Buffer.from(arg.toString()));
buffers.push(Buffer.from(String(arg)));
}
}
});
buffers.push(CRLF);
// @ts-ignore TS2345
return Buffer.concat(buffers);
Expand Down

0 comments on commit f5e166c

Please sign in to comment.