SerialPort (RS-232 Serial COM Port) in C# .NET for MIFARE
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
SerialPort port = new SerialPort("COM2", 9600, Parity.None, 8, StopBits.One);
try
{
port.Open();
port.Write("x");
Thread.Sleep(500);
string x = port.ReadExisting().ToString();
port.Write("c");
Thread.Sleep(500);
string c = port.ReadExisting().ToString();
port.Write(" ");
Thread.Sleep(500);
string p = port.ReadExisting().ToString();
port.Write("s");
Thread.Sleep(500);
string s = port.ReadExisting().ToString();
port.Write("l" + "01" + "FF" + "\r\n");
Thread.Sleep(500);
string l = port.ReadExisting().ToString();
port.Write("r" + "04");
Thread.Sleep(500);
string r = port.ReadExisting().ToString();
port.Write("r" + "04");
Thread.Sleep(500);
string t = port.ReadExisting().ToString();
Thread.Sleep(500);
port.Write("rv" + "04");
Thread.Sleep(500);
string v = port.ReadExisting().ToString();
//string u = v.Substring(0, v.IndexOf("\r\n"));
//int k = Convert.ToInt32(u, 16);
//// k = Int32.Parse(u, NumberStyles.HexNumber);
//TextBox1.Text = k.ToString();
int num = Convert.ToInt32(txtamt.Text);
string st = num.ToString("X");
int bt = st.Length;
for (int i = 0; i < 8 - bt; i++)
{
st = "0" + st;
}
port.Write("+" + "04" + st);
Thread.Sleep(500);
string b = port.ReadExisting().ToString();
string u = b.Substring(0, b.IndexOf("\r\n"));
int d = Convert.ToInt32(u, 16);
string j = d.ToString();
MessageBox.Show("Your Balance Amount is " + j);
}
catch (Exception ex)
{
throw ex;
}
port.Close();
Category: