简体   繁体   English

使用javascript获取完整的信用卡数据

[英]Getting full credit card data using javascript

I have been trying to get the credit card data from Desko Keyboard which I eventually succeeded but the problem is I'm getting card data in a different pattern every time I swipe我一直在尝试从 Desko 键盘获取信用卡数据,我最终成功了,但问题是我每次刷卡时都以不同的模式获取信用卡数据

Below is my JS code下面是我的JS代码

var fs = require('Serialport');
function listenDevice() {
  this.port = fs('COM8');
  let dataComplete = '';
  let count = 0;
  this.port.on('open', () => {
    console.log('sp: ' + this.port.path + ' port open event');
    this.port.set({ dtr: true, rts: true }, function (err) {
      if (err) {
        console.log('Set Error: ', err.message);
        this.isServiceError = true;
        this.serviceErrorText = err;
      }
    });
  });

  this.port.on('data', (data) => {
    console.log('sp: Data: ', data.toString('utf-8'));
  });
}

This is the Pattern of the card data I'm getting:这是我得到的卡片数据的模式:

sp: Data: ?CBZZZZZZZZZZZZZZZZ^XXXXXXXX sp: Data: XXXX X ^18082261485500005000000 !ZZZZZZZZZZZZZZZZ sp: Data: =1808226000005? sp:数据:?CBZZZZZZZZZZZZZZ^XXXXXXXX sp:数据:XXXX X ^18082261485500005000000 !ZZZZZZZZZZZZZZZZ sp:数据:=1808226000005?

sp: Data: ?CBZZZZZZZZZZZZZZZZ^XXXXXXXX sp: Data: XXXX X ^18082261485 sp: Data: 500005000000 !ZZZZZZZZZZZZZZZZ=1808226000005? sp:数据:?CBZZZZZZZZZZZZZZ^XXXXXXXX sp:数据:XXXX X ^18082261485 sp:数据:500005000000 !ZZZZZZZZZZZZZZZZ=1808226000005?

sp: Data: ?CBZZZZZZZZZZZZZZZZ^XXXXXXXX sp: Data: XXXX X ^18082261485500005000000 !ZZZZZZZZZZZZZZZZ=1808226000005? sp:数据:?CBZZZZZZZZZZZZZZ^XXXXXXXX sp:数据:XXXX X ^18082261485500005000000 !ZZZZZZZZZZZZZZZ=1808226000005?

Here X denotes the Card Holder Name Z denotes the Card Number这里 X 表示持卡人姓名 Z 表示卡号

As you can sp: Data: log has been called twice or thrice but the card data is similar.正如你可以sp: Data: log 被调用了两次或三次,但卡数据是相似的。 I want to concat this card data no matter how the data is coming.无论数据如何来,我都想连接这张卡数据。 Any idea.任何的想法。

And I'm using serial port to read the data我正在使用串口读取数据

You can't assume you'll have all the data in a single data event.您不能假设您将在单个data事件中拥有所有data You need to buffer data, and using the information you know about the protocol, determine when you have enough.您需要缓冲数据,并使用您了解的协议信息来确定何时有足够的数据。

Since you're only expecting a small amount of data, it's acceptable to have a running buffer of data concatenated together.由于您只需要少量数据,因此将运行的数据缓冲区连接在一起是可以接受的。

You'll want to do something like this:你会想要做这样的事情:

let buffer;
this.port.on('data', (data) => {
  buffer = Buffer.concat([buffer, data]);
  if (/* your code to determine if you have enough data */) {
    const packet = buffer.slice(0, /* expected length */);
    buffer = buffer.slice(/* expected length*/);
    console.log(packet.toString());
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM