Serial Communication with MSComm Control in windows application

Unknown | 12:07 PM |

Add Reference for Serial Communication:



Add MSComm Control For Serial Communication:




Serial Communication Form Design:





Source Code For Serial Communication:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace mifaretest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private static int num;
private static string st;

private void button1_Click(object sender, EventArgs e)
{
num = Convert.ToInt32(txtamt.Text);

st = num.ToString("X");
int bt = st.Length;
for (int i = 0; i < 8 - bt; i++)
{
st = "0" + st;
}
axMSComm1.Output = "+" + "04" + st;
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
string b = axMSComm1.Input.ToString();
string u = b.Substring(0, b.IndexOf("\r\n"));

int c = Convert.ToInt32(u, 16);

txtbal.Text = c.ToString();
}

}



private void Form2_Load(object sender, EventArgs e)
{
if (axMSComm1.PortOpen == false)
{
axMSComm1.PortOpen = true;
}


}

private void btnwithdrawal_Click(object sender, EventArgs e)
{
num = Convert.ToInt32(txtamt.Text);

st = num.ToString("X");
int bt = st.Length;
for ( int i = 0; i < 8 - bt; i++)
{
st = "0" + st;
}
axMSComm1.Output = "-" +"04" + st;
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
string b = axMSComm1.Input.ToString();
string u = b.Substring(0, b.IndexOf("\r\n"));

int c = Convert.ToInt32(u, 16);

txtbal.Text = c.ToString();
}

}


private static int i=0;
private static string x;
private static string c;
private static string p;
private static string s;
private static string l;
private static string r;
private static string t;
private static string v;
private static string u;
private static int k;
private void button2_Click(object sender, EventArgs e)
{
axMSComm1.Output = "x";
int o = axMSComm1.InBufferCount;

Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
x = axMSComm1.Input.ToString();
}

axMSComm1.Output = "c";
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
c = axMSComm1.Input.ToString();
}
axMSComm1.Output = " ";
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
p = axMSComm1.Input.ToString();
}
axMSComm1.Output = "s";
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
s = axMSComm1.Input.ToString();
}
axMSComm1.Output = "l" + "01" + "FF" + "\r\n";
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
l = axMSComm1.Input.ToString();
}
axMSComm1.Output = "r" + "04";
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
r = axMSComm1.Input.ToString();
}
axMSComm1.Output = "r" + "04";
Thread.Sleep(500);
if (axMSComm1.InBufferCount > 0)
{
t = axMSComm1.Input.ToString();
}
Thread.Sleep(500);

axMSComm1.Output = "rv" + "04";
Thread.Sleep(500);
// string m = x + c + p + s + l + r + t;
if (axMSComm1.InBufferCount > 0)
{
v = axMSComm1.Input.ToString();

u = v.Substring(0, v.IndexOf("\r\n"));
k = Convert.ToInt32(u, 16);
k = Int32.Parse(u, NumberStyles.HexNumber);

txtbal.Text = k.ToString();

}

}








}
}

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 ....