SerialPort (RS-232 Serial COM Port) in C# .NET for MIFARE

Unknown | 12:48 PM |



This article is about communicating through the PC's Serial COM RS-232 port using Microsoft .NET 2.0 or later by using the System.IO.Ports.SerialPort class.

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:

About http://dotnetvisual.blogspot.in/:
DOT NET TO ASP.NET is a web application framework marketed by Microsoft that programmers can use to build dynamic web sites, web applications and web services. It is part of Microsoft's .NET platform and is the successor to Microsoft's Active Server Pages (ASP) technology. ASP.NET is built on the Common Language Runtime, allowing programmers to write ASP.NET code using any Microsoft .NET language. create an application very easily ....