NFC写功能不适用于Javascript

时间:2019-06-17 13:51:55

标签: javascript node.js

我正在使用Java脚本来进行NFC读/写功能 库nfc-pcsc。

当我阅读设备时,它的读起来很完美,现在我想将下拉值写到我的nfc卡中。

我写了post方法,但是它没有完全运行,在控制台上也没有。

这是我的代码

MY index.js文件

 var task = require('./../model/tasks');
router.post('/post',  function (req, res) {
	var post = req.body;
	debugger;

	var id = post.memberid --->my dropdown value

	task.writeData(id, function(err, count){
		if(err){
			res.json(err);
		}else{
			res.redirect('/');
		}
	});
});

我的task.js文件

var task = {
writeData: function (id, callback) {


try {
				nfc.on('reader', reader => {
					console.log(`${reader.reader.name}  device attached`);
reader.on('card', async card => {

							try {
 
								// reader.read(blockNumber, length, blockSize = 4, packetSize = 16)
								const data = await reader.read(4, 12); // starts reading in block 4, continues to 5 and 6 in order to read 12 bytes
								console.log(`data read`, data);
								const payload = data.toString(); // utf8 is default encoding
								console.log(`data converted`, payload);
						 
							} catch (err) {
								console.error(`error when reading data`, err);
							}

	try {

								const data = Buffer.allocUnsafe(12);
								data.fill(0);
								const memberid = id;//(new Date()).toTimeString();
								data.write(memberid); // if text is longer than 12 bytes, it will be cut off
								// reader.write(blockNumber, data, blockSize = 4)
								await reader.write(4, data); // starts writing in block 4, continues to 5 and 6 in order to write 12 bytes
								console.log(`data written`);

							} catch (err) {
								console.error(`error when writing data`, err);
							}

						});
					
				});
			}
			catch (err) {
				console.error(`error occured`, err);
enter code here
			}


	}
}

0 个答案:

没有答案