学生番号の読み取りって簡単なのか?2

学生番号の読み取りって簡単なのか?にて学籍番号の取得に成功して喜んでいたが「いちいちボタンを押すって面倒じゃね?」と言われた。たしかに。
 
もういちど書き直してみる。

事前に用意するもの

はじめてのFeliCa(3)を参照する。

試す

 
CSVって名前をつけたけど、CSVファイルに書き出す機能は入っていない。
 

Form1.cs

usingをいっぱい追加した。変数の処理とかnullとかイケてない。

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FelicaLib;
using System.Media;

namespace FelicaToCSV
{
    public partial class Form1 : Form
    {
        private bool loading = false;
        private string gakuseki = null;
        private string tmpGakuseki = null;
        private string rtnGakuseki = null;

        private string tmpTextBox = null;

        private string idm = null;
        private string tmpidm = null;

        public Form1() {
            InitializeComponent();
        }

        /**
          * バイナリ配列を文字列に変換する
          */
        private string BytesToHexString(byte[] bytes) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < bytes.Length; i++) {
                sb.Append(bytes[i].ToString("X2"));
            }
            return sb.ToString();
        }


        private void timer1_Tick(object sender, EventArgs e) {
      //ASCIIで格納されている学生番号を変換するための配列
            Hashtable ht = new Hashtable();
            ht["30"] = "0";
            ht["31"] = "1";
            ht["32"] = "2";
            ht["33"] = "3";
            ht["34"] = "4";
            ht["35"] = "5";
            ht["36"] = "6";
            ht["37"] = "7";
            ht["38"] = "8";
            ht["39"] = "9";
            ht["00"] = "N";

            //テキストボックスの値の取得
            if (textBox1.Text != "") {
                //テキストボックスの値と改行を入れる
                this.tmpTextBox = textBox1.Text + System.Environment.NewLine;
            }

            //felica読み込み
            try {
                using (Felica f = new Felica()) {
                    // FeliCaポーリング開始、Felicalib.csに加工が必要
                    f.Polling((int)SystemCode.UnivCode);
                    //f.Polling((int)SystemCode.Any);

                    //Felica読み込み
                    this.idm = BytesToHexString(f.IDm());
                    if (this.tmpidm != this.idm) {
                        tmpGakuseki = null;
                        tmpGakuseki = BytesToHexString(f.ReadWithoutEncryption(0x100B, 1));
                        for (int i = 0; i < 20; i++) {
                            string tmp = tmpGakuseki[i].ToString() + tmpGakuseki[i + 1].ToString();
                            rtnGakuseki += ht[tmp];
                            i++;
                        }

                        this.textBox1.Text = this.tmpTextBox + this.rtnGakuseki;
                        this.tmpidm = this.idm;
                        this.rtnGakuseki = null;
                        System.Media.SystemSounds.Asterisk.Play();
                    }

                }

                if (!this.loading) {
                    this.loading = true;
                }

            }
            catch (Exception) {
        //こんなメッセージだすとウザイ
                //MessageBox.Show("カードを検出できません", "問題あり、だめだこりゃ", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Form1_Load(object sender, EventArgs e) {
            //タイマの間隔を1000msに設定、ここは用途に合わせて改造
            timer1.Interval = 1000;
            //タイマを有効化
            timer1.Enabled = true;
        }
    }
}

FelicaLib.cs

UnivCodeを追加する。
FeliCaRawViewerを使って学籍番号を探しておく。

//FelicaLib.cs 一部分だけ
namespace FelicaLib
{
    // システムコード
    enum SystemCode : int
    {
        Any = 0xffff,           // ANY
        Common = 0xfe00,        // 共通領域
        Cyberne = 0x0003,       // サイバネ領域

        Edy = 0xfe00,           // Edy (=共通領域)
        Suica = 0x0003,         // Suica (=サイバネ領域)
        UnivCode = 0x3333, //学籍番号が入っているところのシステムコード(値はテキトー)
    }

連続読み取りも楽々!