zoukankan      html  css  js  c++  java
  • 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)
            {
                //...
            }
        }
    }
  • 相关阅读:
    《Microsoft Sql server 2008 Internals》读书笔记第十一章DBCC Internals(2)
    《Microsoft Sql server 2008 Internals》读书笔记第十一章DBCC Internals(9)
    《Microsoft Sql server 2008 Internals》读书笔记第九章Plan Caching and Recompilation(10)
    CKEditor在asp.net环境下的使用一例
    《Microsoft Sql server 2008 Internals》读书笔记第五章Table(7)
    《Microsoft Sql server 2008 Internals》读书笔记第九章Plan Caching and Recompilation(11)
    千万数据的连续ID表,快速读取其中指定的某1000条数据?
    javascript中的float运算精度
    .Net与Java的互操作(.NET StockTrader微软官方示例应用程序)
    《Microsoft Sql server 2008 Internals》读书笔记第十一章DBCC Internals(6)
  • 原文地址:https://www.cnblogs.com/CodeSnippet/p/11375915.html
Copyright © 2011-2022 走看看