Friday, February 29, 2008

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

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();

No comments:

Using Authorization with Swagger in ASP.NET Core

 Create Solution like below LoginModel.cs using System.ComponentModel.DataAnnotations; namespace UsingAuthorizationWithSwagger.Models {     ...