C#でICカード(FeliCa)に読み書きする方法(サンプルコード)

今回はSony製のICカードリーダーRC-S380とICカード(FeliCa)を使ってICカードにデータを読み書きするプログラムをご紹介します。

ソニー SONY 非接触ICカードリーダー/ライター PaSoRi RC-S380

言語:C#

開発環境:Visual Studio 2015

必要ライブラリ:FeliCaLib

以下参照

ja.osdn.net

 

FeliCaFeliCaLib.cs 

FeliCaLib.csの中を見てみるとこんな感じ

 

/*
felicalib - FeliCa access wrapper library

/*
felicalib - FeliCa access wrapper library

Copyright (c) 2007, Takuya Murakami, All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

3. Neither the name of the project nor the names of its contributors
may be used to endorse or promote products derived from this software
without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

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

Edy = 0xfe00, // Edy (=共通領域)
Suica = 0x0003, // Suica (=サイバネ領域)
}

public class Felica : IDisposable
{
[DllImport("felicalib.dll")]
private extern static IntPtr pasori_open(String dummy);
[DllImport("felicalib.dll")]
private extern static void pasori_close(IntPtr p);
[DllImport("felicalib.dll")]
private extern static int pasori_init(IntPtr p);
[DllImport("felicalib.dll")]
private extern static IntPtr felica_polling(IntPtr p, ushort systemcode, byte rfu, byte time_slot);
[DllImport("felicalib.dll")]
private extern static void felica_free(IntPtr f);
[DllImport("felicalib.dll")]
private extern static void felica_getidm(IntPtr f, byte data);
[DllImport("felicalib.dll")]
private extern static void felica_getpmm(IntPtr f, byte
data);
[DllImport("felicalib.dll")]
private extern static int felica_read_without_encryption02(IntPtr f, int servicecode, int mode, byte addr, byte data);
[DllImport("felicalib.dll")]
private extern static int felica_write_without_encryption(IntPtr f, int servicecode, byte addr, byte
data);

private IntPtr pasorip = IntPtr.Zero;
private IntPtr felicap = IntPtr.Zero;

public Felica()
{
pasorip = pasori_open(null);
if (pasorip == IntPtr.Zero)
{
throw new Exception("felicalib.dll を開けません");
}
if (pasori_init(pasorip) != 0)
{
throw new Exception("PaSoRi に接続できません");
}
}

public void Dispose()
{
if (pasorip != IntPtr.Zero)
{
pasori_close(pasorip);
pasorip = IntPtr.Zero;
}
}

~Felica()
{
Dispose();
}

public void Polling(int systemcode)
{
felica_free(felicap);

felicap = felica_polling(pasorip, (ushort)systemcode, 0, 0);
if (felicap == IntPtr.Zero)
{
throw new Exception("カード読み取り失敗");
}
}

public byte IDm()
{
if (felicap == IntPtr.Zero)
{
throw new Exception("no polling executed.");
}

byte buf = new byte[8];
felica_getidm(felicap, buf);
return buf;
}

public byte PMm()
{
if (felicap == IntPtr.Zero)
{
throw new Exception("no polling executed.");
}

byte buf = new byte[8];
felica_getpmm(felicap, buf);
return buf;
}

 

public byte ReadWithoutEncryption(int servicecode, int addr)
{
if (felicap == IntPtr.Zero)
{
throw new Exception("no polling executed.");
}
byte
data = new byte[16];
int ret = felica_read_without_encryption02(felicap, servicecode, 0, (byte)addr, data);
if (ret != 0)
{
return null;
}
return data;

}

 

しかし、これだけでは書き込みができません。

書き込みの命令が見つからないのです。

なので、以下を追加

※これでひとまず書き込めるようになります。

 

//書き込み追加
public byte WriteWithoutEncryption(int servicecode, int addr, string str)
{
if (felicap == IntPtr.Zero)
{
throw new Exception("no polling executed.");
}
byte
data = new byte[16];
data = System.Text.Encoding.UTF8.GetBytes(str);
int ret = felica_write_without_encryption(felicap, servicecode, (byte)addr, data);
if (ret != 0)
{
throw new Exception("カード書き込み失敗");
}

return data;
}

}
}

 

C#プログラム

FelicaLibを使います宣言。

using FelicaLib; 

 

FeliCa読み取り

string mozi = "";

using (Felica f = new Felica())
{
//ポートの指定
f.Polling(0xFFFF);
//block00を読み込む
byte[] Block00 = f.ReadWithoutEncryption(0x0009, 0x00);
//変換して変数に入れる
mozi = System.Text.Encoding.UTF8.GetString(Block00);

 FeliCaはByte型で記録されるみたいなので変換しないと読めないよ!

 

FeliCa書き込み

string mozi = "書き込むゼ";

using (Felica f = new Felica())
{
//ポートの指定
f.Polling(0xFFFF);
//block00に書き込む
f.WriteWithoutEncryption(0x0009, 0x00, mozi);
}

 

説明

読み込み命令のReadWithoutEncryption(0x0009, 0x00)

書き込み命令のWriteWithoutEncryption(0x0009, 0x00, mozi)

0x00はFeliCaの記憶領域(ブロック番号)を指示します。

基本的なICカードは16バイトの記憶領域が14個自由に使えます。

以下のサイトが参考になります。

FeliCa Lite-S - おなかすいたWiki!

また書き込み命令のmoziはString型の変数を指定してください。

 

以下FeliCaLib参考ページ

felicalib: felicalib.h

 

ソニー SONY 非接触ICカードリーダー/ライター PaSoRi RC-S380

ソニー SONY 非接触ICカードリーダー/ライター PaSoRi RC-S380