UNPKG

@waiting/bankcard-reader-bp8903

Version:
13 lines (11 loc) 2.28 kB
/** * @waiting/bankcard-reader-bp8903 * 南天 BP8903 读银行卡 * * @version 1.1.1 * @author waiting * @license MIT * @link https://github.com/waitingsong/node-bankcard-reader-bp8903#readme */ import{DTypes as e}from"win32-def";import{parseDeviceOpts as t}from"@waiting/bankcard-reader-base";export{initialOpts}from"@waiting/bankcard-reader-base";import{info as r}from"@waiting/log";import{setPathDirectory as o,dirname as n,validateDllFile as i}from"@waiting/shared-core";import{Library as a}from"ffi";import{of as c}from"rxjs";const s={OpenComPort:[e.INT,[e.INT,e.POINT,e.INT,e.INT]],CloseComPort:[e.INT,[]],IsComOpen:[e.INT,[]],GetCardNumberFromDev:[e.INT,[e.INT,e.POINT,e.INT,e.POINT]]};function p(e,t){if(e&&e.inUse)return e.deviceOpts.debug&&r("Cautiton: connectDevice() device in use"),0;const o=e.apib.OpenComPort(t,Buffer.from(""),9600,1);return e.deviceOpts.debug&&r(`open com ret: ${o}`),0===o?t:0}function d(e){const t=e.apib.CloseComPort();return e.deviceOpts.debug&&r(`disconnectDevice at port: ${e.openPort}, ret: ${t} `),e.inUse=!1,!0}function u(e){const t=e.apib.IsComOpen();return e.deviceOpts.debug&&r(`isPortOpen: ${t}`),1===t}function f(e,t){const r=[];if(e.port>0){const o=m(e.port,e,t);o.openPort>0&&r.push(o)}else for(let o=1;o<=16;o++){const n=m(o,e,t);n.openPort>0&&r.push(n)}return r}function m(e,t,o){const n={apib:o,deviceOpts:t,inUse:!1,openPort:0},i=p(n,e);return i>0&&u(n)&&(n.inUse=!0,n.openPort=i,t.debug&&r(`Found device at serial/usb port: ${i}`),d(n)),n}function b(e){o(n(e.deviceOpts.dllTxt));const t=Buffer.alloc(64);e.deviceOpts.debug&&r("starting reading...");const i={cardno:""},a=e.apib.GetCardNumberFromDev(e.openPort,Buffer.from(""),3,t);return 0===a&&(i.cardno=t.toString().replace(/\0+.*$/,"")),e.deviceOpts.debug&&(r(`readDataBase code: ${a}`),r(`readDataBase bufLen: ${t.byteLength}`),r("readDataBase buf: "),r(t),r(`readDataBase ret: ${i}`)),i}async function g(e){const o=t(e),{debug:n}=o;n&&r(o),await i(o.dllTxt);const c=a(o.dllTxt,s),p=f(o,c);if(p&&p.length)return p;throw new Error("未找到读卡设备")}function l(e){if(e.openPort){try{d(e)}catch(e){throw e}const t=c(b(e));return t.toPromise()}throw new Error("设备端口未指定")}export{g as init,l as read}; //# sourceMappingURL=index.esm.min.js.map