PaSoRi RC-S330 / FelicaLib / C# 2008 Express Editionで。
白Felica、携帯なんかのIDmは問題ない。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Net;
using System.Net.Sockets;
using FelicaLib;
namespace FelicaApp
{
public partial class FelicaApp : Form
{
byte[] IDm = new Byte[8];
byte[] PMm = new Byte[8];
public FelicaApp()
{
// set timer for pasori polling
Timer timer = new Timer();
timer.Tick += new EventHandler(PollPaSoRi);
timer.Interval = 3000;
timer.Enabled = true;
InitializeComponent();
}
public void PollPaSoRi(object sender, EventArgs e)
{
try
{
using (Felica f = new Felica())
{
// try polling
f.Polling((int)SystemCode.Common);
// beep if read ok
this.lblMsg.Text = Properties.Resources.msgFelicaOK;
Console.Beep(500, 100);
IDm = f.IDm();
PMm = f.PMm();
// convert ID to string
string IDmString = "";
for (int i = 0; i < IDm.Length; i++)
{
IDmString = IDmString + IDm[i].ToString("X2");
}
this.lblIdm.Text = IDmString;
string PMmString = "";
for (int i = 0; i < PMm.Length; i++)
{
PMmString = PMmString + PMm[i].ToString("X2");
}
this.lblPmm.Text = PMmString;
// send server msg
string ret = this.sendID(IDmString, PMmString);
Int32 intRet = Int32.Parse(ret);
if (0 != intRet)
{
this.lblPt.Text = ret;
this.lblPt.Visible = true;
this.lblPtMsg1.Visible = true;
this.lblPtMsg2.Visible = true;
}
else
{
this.lblPt.Text = "";
this.lblPt.Visible = false;
this.lblPtMsg1.Visible = false;
this.lblPtMsg2.Visible = false;
}
// beep if read ok
Console.Beep(800, 100);
for (int i = 0; i < 700; i++)
{
System.Threading.Thread.Sleep(10);
Application.DoEvents();
}
// refresh for print screen
//this.lblMsg.Refresh();
//this.lblIdm.Refresh(); // TODO for DEBUG
//this.lblPmm.Refresh(); // TODO for DEBUG
}
}
catch (Exception ex)
{
if ((ex.Message == Properties.Resources.msgFelicaNG) |
(ex.Message == Properties.Resources.msgFelicaSySError))
{
this.lblMsg.Text = Properties.Resources.msgFelicaDefault;
this.lblPt.Text = "";
this.lblPt.Visible = false;
this.lblPtMsg1.Visible = false;
this.lblPtMsg2.Visible = false;
}
else
{
this.lblMsg.Text = ex.Message;
}
this.lblIdm.Text = "";
this.lblPmm.Text = "";
}
}
private string sendID(string IDm, string PMm)
{
// create msg
string server = Properties.Resources.confServer;
string message = "idm=" + IDm + "&" + "pmm=" + PMm;
// send to the server (http post)
return postRequest(server, message);
}
private string postRequest(string url, string postData)
{
// return string
string ret = "";
try
{
System.Text.Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData);
// create request
System.Net.WebRequest req = System.Net.WebRequest.Create(url);
// set method 'post'
req.Method = "POST";
// set contenttype
req.ContentType = "application/x-www-form-urlencoded";
// set length of post data
req.ContentLength = postDataBytes.Length;
// create stream for post
System.IO.Stream reqStream = req.GetRequestStream();
// post
reqStream.Write(postDataBytes, 0, postDataBytes.Length);
reqStream.Close();
// prepare retrieve response
System.Net.WebResponse res = req.GetResponse();
// create stream for response
System.IO.Stream resStream = res.GetResponseStream();
// retrieve response
System.IO.StreamReader sr = new System.IO.StreamReader(resStream, enc);
ret = sr.ReadToEnd();
// close
sr.Close();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
return ret;
}
}
}
コメント