1. 程式人生 > >RFID讀寫---RFID讀卡

RFID讀寫---RFID讀卡

 程式碼為上課練習使用的RFID讀卡器使用!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;
using System.Collections;
using System.Runtime.InteropServices;
using ISO15693DLL;/*引用名稱空間!*/
namespace RFID讀寫
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
           ISO15693DLL.ISO15693Reader reader = new ISO15693DLL.ISO15693Reader();/*建立例項*/

        int a = 0;/*定義一個變數用於後方判斷讀取成功提示資訊框的數量。*/
        public MainWindow()
        {
            InitializeComponent();
            MessageBox.Show("歡迎使用本讀卡器 V0.0.1 Beta\n當前僅支援ISO15693卡片讀取\nMade by Cjw", "Cjw提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }
        


        private void RB_ck_Click(object sender, RoutedEventArgs e)
        {
            if(RB_ck.IsChecked==true)/*判斷串列埠單選框是否被選定,如果被選定則將按鈕內容修改*/
            {
                Button_Open.Content = ("開啟串列埠");
                Button_Close.Content = ("關閉串列埠");
            }
        }

        private void RB_USB_Checked(object sender, RoutedEventArgs e)/*判斷USB單選框是否被選擇,如果被選擇,則按鈕內容修改。*/
        {
            if (RB_USB.IsChecked == true)
            {
                  Button_Open.Content = ("開啟USB口");
                  Button_Close.Content = ("關閉USB口");
            }
        }
   
        private void Button_Open_Click(object sender, RoutedEventArgs e)
        {
            /*判斷,如果當前選定為串列埠,因為我沒有寫串列埠,所以,報錯!*/
            if(Button_Open.Content.ToString()=="開啟串列埠")
            {
                MessageBox.Show("當前暫時不支援串列埠功能!","Cjw提示",MessageBoxButton.OK,MessageBoxImage.Error);
            }
            else
                /*開啟的是USB口,如果返回0x00則為成功,否則失敗*/
                if(reader.OpenHIDDevice()==0x00)
                {
                    MessageBox.Show("開啟USB口成功","Cjw提示",MessageBoxButton.OK,MessageBoxImage.Information);
                Button_Open.Content = "USB口已開啟";
                Button_Open.IsEnabled = false;
                Button_Close.IsEnabled = true;
                Button_Read.IsEnabled = true;
                }
                else
                    MessageBox.Show("開啟USB口失敗\n請檢查資料線是否連線好", "Cjw提示", MessageBoxButton.OK, MessageBoxImage.Error);
        }

        private void Button_Read_Click(object sender, RoutedEventArgs e)
        {   /*定義一個變數用於存放返回卡片的數量*/
            Int32 TagCount=0;
                /*定義一個字串*/
            String[] TagNumber = new String[1];
            if (reader.Inventory(ModulateMethod.ASK, InventoryModel.Single, ref TagCount, ref TagNumber)==0x00)
            {
                Card_Numberbox.Text=TagNumber[0];
                if (a == 3)
                    Mess.Visibility = Visibility.Visible;
               
                
                 if (Mess.IsChecked==false)
                    MessageBox.Show("讀取成功!", "Cjw提示", MessageBoxButton.OK, MessageBoxImage.Information);
                CardId.Items.Add(DateTime.Now.ToLongTimeString().ToString()+"  "+TagNumber[0]);
                a++;
            }
            else
            {
                MessageBox.Show("讀取失敗!\n請檢查卡片是否放穩或卡片協議是否為ISO15693", "Cjw提示", MessageBoxButton.OK, MessageBoxImage.Error);
                Card_Numberbox.Text = ("讀取失敗!請檢查卡片是否放穩!");
            }
        }

        private void Button_Close_Click(object sender, RoutedEventArgs e)
        {
            if(Button_Close.Content.ToString()=="關閉USB口")
             if (reader.CloseHIDDevice()==0x00) /*同理,呼叫關閉USB口方法,*/
                {
                    MessageBox.Show("關閉USB口成功", "Cjw提示", MessageBoxButton.OK, MessageBoxImage.Information);
                    Button_Open.Content = "開啟USB口";
                    Button_Open.IsEnabled = true;
                    Button_Close.IsEnabled = false;
                    Button_Read.IsEnabled = false;
                }
                else
                    MessageBox.Show("關閉USB口失敗", "Cjw提示", MessageBoxButton.OK, MessageBoxImage.Error);

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetDataObject(Card_Numberbox.Text);
            MessageBox.Show("已成功複製到剪貼簿!", "Cjw提示", MessageBoxButton.OK, MessageBoxImage.Information);
        }

    }

}