• 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# WinForm下,隐藏主窗体,只在进程管理器中显示进程,在任务栏,状态栏都不显示窗体的方法
    C#全能数据库操作类及调用示例
    多个汇总列转换为行记录 mssql
    Oracle 10g创建数据库 用户等基本操作
    Jquery基本选择器 层次选择器 过滤选择器 表单选择器使用示例 带注释
    SQL与ORACLE的外键约束级联更新和删除
    C# 屏幕监控 自动截屏程序 主窗体隐藏,仅在进程中显示
    图文讲解VS2010程序打包操作 安装卸载
    查表法按日期生成流水号 mssql
    给DataTable添加主键 几何级提升Select筛选数据的速度
  • 原文地址:https://www.cnblogs.com/CodeSnippet/p/11375915.html
Copyright © 2020-2023  润新知