pokusew / nfc-pcsc

Easy reading and writing NFC tags and cards in Node.js
MIT License
532 stars 132 forks source link

Error while transmitting: Card was reset #19

Open SamVerschueren opened 7 years ago

SamVerschueren commented 7 years ago

I often get a WriteError but can't really find a workaround for this. I also have no clue what the error message even means. Does someone have a clue how I can solve this?

{ WriteError: An error occurred while transmitting.
    at /Users/sam/Projects/playground/usb/node_modules/nfc-pcsc/dist/Reader.js:573:11
    at throw (native)
    at step (/Users/sam/Projects/playground/usb/node_modules/nfc-pcsc/dist/Reader.js:16:191)
    at /Users/sam/Projects/playground/usb/node_modules/nfc-pcsc/dist/Reader.js:16:402
  name: 'WriteError',
  code: null,
  previous:
   { TransmitError: An error occurred while transmitting.
       at reader.transmit (/Users/sam/Projects/playground/usb/node_modules/nfc-pcsc/dist/Reader.js:271:20)
     name: 'TransmitError',
     code: 'failure',
     previous:
      Error: SCardTransmit error: Card was reset.(0x80100068)
          at Error (native) } }
pokusew commented 7 years ago

Hi @SamVerschueren,

firstly, could you please provide more details?

According to my quick search, it looks like a strange error connected to Windows OS.

Also I have found an interesting thread related to this problem.

I believe, we can find a solution together.

So, please answer my questions.

Hope it helps.

SamVerschueren commented 7 years ago

Hi @pokusew, thanks for the quick reply. I know, my initial post was very brief but didn't have much time to provide more details, so here we go.

Here is the code that shows the behaviour

'use strict';
const NFC = require('nfc-pcsc').default;

const nfc = new NFC();

nfc.on('reader', reader => {
    reader.on('card', card => {
        console.log(`Card detected (${card.type})`);

        const data = Buffer.from([0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0, 0xa0]);

        reader.write(6, data)
            .then(result => {
                console.log('done!');
                console.log(result);
            })
            .catch(err => {
                console.log('error');
                console.error(err);
            });
    });
});

Save that file as index.js and run it. Put a tag on the card reader and then it should fail with the error message that I gave in my initial post. If you kill the program, put the tag on the card reader and then start the program while the tag is already detected by the reader, it works perfectly fine. It just doesn't work if the reader detects the tag while my code is already running.

When I reduce the size of the data buffer to 8 bytes, it seems to work perfectly fine. So I tried to chop my data array in chunks of 4 bytes each and wrote them sequentially to the tag. The first 2 or 3 chunks work, but then I get the same error or another one indicating that the card is unpowered.

So maybe I'm doing something wrong here, but I need to write 64 bytes (or more) to the tag and am not sure how I can achieve that.