C# LJYZN-105发卡机 NFC读取

Posted codesnippet

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# LJYZN-105发卡机 NFC读取相关的知识,希望对你有一定的参考价值。

仅用于“LJYZN-105发卡机”(产品相关资源:http://www.ljyzn.com/product_info.asp?id=202

1) UI

技术图片

 

2) Code

 

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ReaderB;
namespace NFCReader

    public partial class Form1 : Form
    
        //写入配置文件
        [DllImport("Kernel32.dll")]
        public static extern bool WritePrivateProfileString(string strAppName, string strKeyName, string strString, string strFileName);
        //读取配置文件
        [DllImport("Kernel32.dll")]
        public static extern int GetPrivateProfileString(string strAppName, string strKeyName, string strDefault, StringBuilder sbReturnString, int nSize, string strFileName);

        private byte fComAddr, fBaud = 5;
        private int frmcomportindex;
        private bool fIsInventoryScan;
        private string tcp_ip;
        private int tcp_port;
        private int com_port;
        public Form1()
        
            InitializeComponent();
        

        private void Form1_Load(object sender, EventArgs e)
                    
            for (int i = 1; i < 13; i++)
            
                comboBox1.Items.Add("Com" + i.ToString());
            
            comboBox1.SelectedIndex = 0;
            button2.Enabled = false;
            timer1.Enabled = false;

            string strFileName = Application.StartupPath + "\\\\Config.ini";
            StringBuilder stringBuilder = new StringBuilder(256);
            Form1.GetPrivateProfileString("config", "com", "", stringBuilder, 256, strFileName);
            if (!string.IsNullOrEmpty(stringBuilder.ToString().Trim()))
            
                com_port = Convert.ToInt32(stringBuilder.ToString());
                comboBox1.SelectedIndex = com_port - 1;
                int result = StaticClassReaderB.OpenComPort(com_port, ref fComAddr, fBaud, ref frmcomportindex);
                if (result == 0)
                
                    comboBox1.Enabled = false;
                    button1.Enabled = false;
                    button2.Enabled = true;
                    timer1.Enabled = true;
                
                else
                
                    MessageBox.Show("串口通讯错误", "信息提示");
                

            
            Form1.GetPrivateProfileString("config", "ip", "", stringBuilder, 256, strFileName);
            if (!string.IsNullOrEmpty(stringBuilder.ToString().Trim()))
            
                textBox1.Text = tcp_ip = stringBuilder.ToString();
            
            Form1.GetPrivateProfileString("config", "port", "", stringBuilder, 256, strFileName);
            if (!string.IsNullOrEmpty(stringBuilder.ToString().Trim()))
            
                textBox2.Text = stringBuilder.ToString();
                tcp_port = Convert.ToInt32(stringBuilder.ToString());
            
        

        private void Button2_Click(object sender, EventArgs e)
        
            if (frmcomportindex > 0)
            
                StaticClassReaderB.CloseSpecComPort(this.frmcomportindex);
                button1.Enabled = true;
                button2.Enabled = false;
                comboBox1.Enabled = true;
                timer1.Enabled = false;
                //读取NFC卡状态的间隔时间
                timer1.Interval = 1000;
            
        

        private void Button1_Click(object sender, EventArgs e)
        
            int port = comboBox1.SelectedIndex + 1;
            int result = StaticClassReaderB.OpenComPort(port, ref fComAddr, fBaud, ref frmcomportindex);
            if (result == 0)
            
                comboBox1.Enabled = false;
                button1.Enabled = false;
                button2.Enabled = true;
                timer1.Enabled = true;
            
            else
            
                MessageBox.Show("串口通讯错误", "信息提示");
            
        

        private void Timer1_Tick(object sender, EventArgs e)
        
            if (fIsInventoryScan)
                return;
            this.fIsInventoryScan = true;
            this.Inventory();
            this.fIsInventoryScan = false;
        

        private void Inventory()
        
            int num = 0;
            int num2 = 0;
            byte[] array = new byte[5000];
            byte adrTID = 0;
            byte lenTID = 0;
            byte tidflag = 0;
            var result = StaticClassReaderB.Inventory_G2(ref this.fComAddr, adrTID, lenTID, tidflag, array, ref num2, ref num, this.frmcomportindex);
            if (result == 1 | result == 2 | result == 3 | result == 4 | result == 251)
            
                byte[] array2 = new byte[num2];
                Array.Copy(array, array2, num2);
                string text = this.ByteArrayToHexString(array2);
                int num3 = 0;
                if (num == 0)
                
                    if (listBox1.Items.Count > 0)
                    
                        if (!listBox1.Items[listBox1.Items.Count - 1].ToString().Contains("[OFF]"))
                        
                            string newItem = listBox1.Items[listBox1.Items.Count - 1].ToString().Replace("[ON]", "[OFF]");
                            listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
                            listBox1.Items.Add(newItem);
                            listBox1.SelectedIndex = listBox1.Items.Count - 1;
                            //发送NFC卡离开信息
                            SendMessage(tcp_ip, tcp_port, newItem);
                        
                    
                    return;
                
                int num4 = (int)array2[num3];
                string text2 = text.Substring(num3 * 2 + 2, num4 * 2);
                if (listBox1.Items.Count <= 0)
                
                    listBox1.Items.Add(text2 + "[ON]");
                    //发送NFC靠近信息
                    SendMessage(tcp_ip, tcp_port, text2 + "[ON]");
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
                
                else
                
                    var lastItem = listBox1.Items[listBox1.Items.Count - 1].ToString();
                    if (lastItem != text2 + "[ON]")
                    
                        listBox1.Items.Add(text2 + "[ON]");
                        //发送NFC靠近信息
                        SendMessage(tcp_ip, tcp_port, text2 + "[ON]");
                        listBox1.SelectedIndex = listBox1.Items.Count - 1;
                    
                
            
        

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        
            if (frmcomportindex > 0)
            
                StaticClassReaderB.CloseSpecComPort(this.frmcomportindex);
            
        

        private string ByteArrayToHexString(byte[] data)
        
            StringBuilder stringBuilder = new StringBuilder(data.Length * 3);
            foreach (byte value in data)
            
                stringBuilder.Append(Convert.ToString(value, 16).PadLeft(2, 0));
            
            return stringBuilder.ToString().ToUpper();
        

        private void TextBox1_TextChanged(object sender, EventArgs e)
        
            tcp_ip = textBox1.Text;
        

        private void TextBox2_TextChanged(object sender, EventArgs e)
        
            tcp_port = Convert.ToInt32(textBox2.Text);
        

        private void SendMessage(string nodeAddress, int port,string arg)
        
            //...
        
    

 

以上是关于C# LJYZN-105发卡机 NFC读取的主要内容,如果未能解决你的问题,请参考以下文章

为啥有些门禁卡不能被华为NFC复制?

NFC阅读器没有在android中连续读取NFC标签

wpf读取低频卡

在我的应用程序中读取 iOS 13 NFC 前台标签,同时读取 iOS 后台

Android系统级应用连续读取NFC标签实现

NFC 或 BLE 读取设备的 Mac 地址