Skip to content

Commit 4ae9578

Browse files
committed
增加com口支持
1 parent c088293 commit 4ae9578

File tree

6 files changed

+29
-14
lines changed

6 files changed

+29
-14
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
./node_modules/
2-
./build/
1+
node_modules/
2+
build/

index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ export type PrintResult = {
99
/*err is c/c++ GetLastError()'s result;*/
1010
err: number
1111
};
12-
export type DeviceType = 'USB' |'LPT'
12+
export type DeviceType = 'USB' |'LPT' | 'COM'
1313
export function GetUsbDeviceList (): EscposPrinter[];
1414
export function GetLptDeviceList(): EscposPrinter[];
15+
export function GetComDeviceList(): EscposPrinter[];
1516
export function GetDeviceList(type?:DeviceType): EscposPrinter[];
1617
/**
1718
*

index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ const win32Escpos = function () {
55
GetUsbDeviceList () {
66
return GetDeviceList('USB');
77
},
8+
GetComDeviceList() {
9+
return GetDeviceList('COM');
10+
},
811
GetLptDeviceList () {
912
return GetDeviceList('LPT').map((item, index) => {
1013
const matchs = item.path.match(/lpt\d/);

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "node-escpos-addon",
3-
"version": "1.0.128",
3+
"version": "1.0.129",
44
"description": "a cpp escpos plugin for nodejs and electron",
55
"main": "./index.js",
66
"scripts": {
@@ -11,12 +11,12 @@
1111
"config": "node-gyp configure --nodedir=\"F:\\node\""
1212
},
1313
"keywords": [
14-
"a",
14+
"windows",
1515
"cpp",
1616
"escpos",
17-
"plugin",
18-
"for",
19-
"nodjes"
17+
"addon",
18+
"electron",
19+
"nodejs"
2020
],
2121
"author": "vidy",
2222
"license": "ISC",

src/index.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,22 @@ void GetDeviceList(const FunctionCallbackInfo<Value>& args)
9696
GUID *guidP;
9797
DWORD i = sizeof(GUID);
9898
guidP = &guid;
99-
// GUID_DEVINTERFACE_KEYBOARD
10099
if (SetupDiClassGuidsFromName(deviceTypeBuffer, guidP, i, &i)) {
101100
GetDeviceList(deviceList, GUID_DEVINTERFACE_PARCLASS);
102101
}
103102
free(deviceTypeBuffer);
104103
}
104+
else if (!strcmp(deviceTypeBuffer, "COM")) {
105+
GUID guid;
106+
GUID *guidP;
107+
DWORD i = sizeof(GUID);
108+
guidP = &guid;
109+
// GUID_DEVINTERFACE_KEYBOARD
110+
if (SetupDiClassGuidsFromName(deviceTypeBuffer, guidP, i, &i)) {
111+
GetDeviceList(deviceList, GUID_DEVINTERFACE_COMPORT);
112+
}
113+
free(deviceTypeBuffer);
114+
}
105115
else {
106116
isolate->ThrowException(Exception::Error(String::NewFromUtf8(isolate, "Wrong type, must be Usb or Ports")));
107117
free(deviceTypeBuffer);

test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const addon = require('./build/Debug/addon.node');
22
const iconv = require('iconv-lite');
3-
4-
// const list = addon.GetUsbDeviceList('USB');
5-
// console.log(list);
6-
7-
console.log('lpt',addon.GetUsbDeviceList('LPT') )
3+
console.log(addon);
4+
const list = addon.GetDeviceList('COM');
5+
console.log(list);
6+
const content = iconv.encode("你好啊\n halo cpp!\\n\n\n\n\nn\n\n\n\n", 'GB18030')
7+
console.log(addon.PrintRaw(list[0].path,content )) ;
8+
// console.log('lpt',addon.GetUsbDeviceList('LPT') )
89
// // const printers = list.filter(item => item.service ==='usbprint' || item.deviceName==='USB 打印支持'|| item.service ==='WinUSB');
910
// const content = iconv.encode("你好啊\n halo cpp!\\n\n\n\n\nn\n\n\n\n", 'GB18030')
1011

0 commit comments

Comments
 (0)