public static void MainSelfLibNfc(string[] args) { Console.WriteLine("Hello"); Console.WriteLine("Using libnfc {0}", NfcContext.Version); //CancellationTokenSource cts = new CancellationTokenSource(); //Console.CancelKeyPress += (s, e) => //{ // if (cts.IsCancellationRequested) // { // //Hard break, let it go // } // else // { // //soft break, handle it // e.Cancel = true; // cts.Cancel(); // } //}; byte pollNr = 1; byte period = 1; var modulations = new[] { new NfcModulation(NfcModulationType.ISO14443A, NfcBaudRate.BR106), new NfcModulation(NfcModulationType.ISO14443B, NfcBaudRate.BR106), new NfcModulation(NfcModulationType.Felica, NfcBaudRate.BR212), new NfcModulation(NfcModulationType.Felica, NfcBaudRate.BR424), new NfcModulation(NfcModulationType.Jewel, NfcBaudRate.BR106), }; Console.WriteLine("Opening libnfc context. sizeof(NfcModulation)={0}", Marshal.SizeOf <NfcModulation>()); using (NfcContext context = new NfcContext()) { string[] devices = context.ListDevices(); Console.WriteLine("Found devices: [{0}]", string.Join(";", devices)); Console.WriteLine("Opening default device"); using (NfcDevice device = context.Open(null)) { Console.WriteLine("Device opened"); Console.WriteLine(" name={0} constring={1}", device.Name, device.ConnectionString); Console.WriteLine(" info={0}", device.GetInfo()); Console.WriteLine("Initializing as initiator"); INfcInitiator initiator = device.InitInitiator(false); Console.WriteLine("NFC reader opened"); Console.WriteLine("NFC reader name: {0}", device.Name); while (true) { PollTagsAndDisplayInfo(initiator); } } } }
protected override async Task ExecuteAsync(CancellationToken cancellationToken) { this.m_logger.LogInformation("Started nfcService"); //byte pollNr = 20; //byte period = 2; //var modulations = new[] //{ // new NfcModulation(NfcModulationType.ISO14443A, NfcBaudRate.BR106), // new NfcModulation(NfcModulationType.ISO14443B, NfcBaudRate.BR106), // new NfcModulation(NfcModulationType.Felica, NfcBaudRate.BR212), // new NfcModulation(NfcModulationType.Felica, NfcBaudRate.BR424), // new NfcModulation(NfcModulationType.Jewel, NfcBaudRate.BR106), //}; NfcModulation mod = new NfcModulation(NfcModulationType.ISO14443A, NfcBaudRate.BR106); using (NfcContext ctx = new NfcContext()) using (var device = ctx.Open(null)) { var initiator = device.InitInitiator(false); while (!cancellationToken.IsCancellationRequested) { var tag = initiator.SelectMifareTag(); if (tag == null) { await Task.Delay(100, cancellationToken); continue; } using (tag) { this.m_logger.LogInformation("Card detected"); this.m_hub.All.cardOn(); while (tag.IsPresent() && !cancellationToken.IsCancellationRequested) { await Task.Delay(100, cancellationToken); } this.m_logger.LogInformation("Card removed"); this.m_hub.All.cardOff(); } await Task.Delay(1000, cancellationToken); } } this.m_logger.LogInformation("Stopped nfcService"); }
static async Task Main(string[] args) { Console.WriteLine("Using libnfc {0}", NfcContext.Version); CancellationTokenSource cts = new CancellationTokenSource(); Console.CancelKeyPress += (s, e) => { if (cts.IsCancellationRequested) { //Hard break, let it go } else { //soft break, handle it e.Cancel = true; cts.Cancel(); } }; byte pollNr = 20; byte period = 2; var modulations = new[] { new NfcModulation(NfcModulationType.ISO14443A, NfcBaudRate.BR106), new NfcModulation(NfcModulationType.ISO14443B, NfcBaudRate.BR106), new NfcModulation(NfcModulationType.Felica, NfcBaudRate.BR212), new NfcModulation(NfcModulationType.Felica, NfcBaudRate.BR424), new NfcModulation(NfcModulationType.Jewel, NfcBaudRate.BR106), }; using (NfcContext context = new NfcContext()) using (NfcDevice device = context.Open(null)) { NfcInitiator initiator = device.InitInitiator(false); Console.WriteLine("NFC reader: {0} opened", device.Name); Console.WriteLine($"NFC device will poll during {pollNr * modulations.Length * period * 150}ms ({pollNr} pollings of {period * 150}ms for {modulations.Length} modulations)"); //NfcInitiatorTarget target = await initiator.PollAsync(modulations, pollNr, period); } }