This is my 1st blog ,It is regarding, how to read Read Data From Comport and save it in .txt format in console application ,It is very simple ...........The code are as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace PortDataReceived
{
class Program
{
static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM1");
if (mySerialPort.IsOpen == true) mySerialPort.Close();
mySerialPort.BaudRate = 2400;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.Open();
// Console.WriteLine("Press any key to continue...");
//Console.ReadLine();
Thread.sleep("500");
mySerialPort.ReadTimeout = 500;
string indata = mySerialPort.ReadExisting();
Console.WriteLine(indata);
if (string.IsNullOrEmpty(indata))
{
StringBuilder builder = new StringBuilder();
StreamWriter sw = new StreamWriter(@"c:\arjun.txt", true);
sw.WriteLine(indata);
sw.Close();
}
else
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < indata.Length-8; i += 8)
{
string section = indata.Substring(i, 8);
//int ascii = 0;
string ascii = "";
try
{
//ascii = Convert.ToInt32(section, 2);
ascii = section;
StreamWriter sw = new StreamWriter(@"c:\arjun.txt", true);
foreach (char c in section)
{
if (Char.IsLetterOrDigit(c))
{
builder.Append(c);
}
}
sw.WriteLine(builder.ToString());
sw.Close();
break;
}
catch(Exception e)
{
throw e;
}
// builder.Append((char)ascii);
}
// Console.WriteLine(builder.ToString());
// Console.WriteLine(indata);
//Console.WriteLine("Data Received:");
//Console.Write(indata);
// Console.ReadKey();
mySerialPort.Close();
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
namespace PortDataReceived
{
class Program
{
static void Main(string[] args)
{
SerialPort mySerialPort = new SerialPort("COM1");
if (mySerialPort.IsOpen == true) mySerialPort.Close();
mySerialPort.BaudRate = 2400;
mySerialPort.Parity = Parity.None;
mySerialPort.StopBits = StopBits.One;
mySerialPort.DataBits = 8;
mySerialPort.Handshake = Handshake.None;
mySerialPort.Open();
// Console.WriteLine("Press any key to continue...");
//Console.ReadLine();
Thread.sleep("500");
mySerialPort.ReadTimeout = 500;
string indata = mySerialPort.ReadExisting();
Console.WriteLine(indata);
if (string.IsNullOrEmpty(indata))
{
StringBuilder builder = new StringBuilder();
StreamWriter sw = new StreamWriter(@"c:\arjun.txt", true);
sw.WriteLine(indata);
sw.Close();
}
else
{
StringBuilder builder = new StringBuilder();
for (int i = 0; i < indata.Length-8; i += 8)
{
string section = indata.Substring(i, 8);
//int ascii = 0;
string ascii = "";
try
{
//ascii = Convert.ToInt32(section, 2);
ascii = section;
StreamWriter sw = new StreamWriter(@"c:\arjun.txt", true);
foreach (char c in section)
{
if (Char.IsLetterOrDigit(c))
{
builder.Append(c);
}
}
sw.WriteLine(builder.ToString());
sw.Close();
break;
}
catch(Exception e)
{
throw e;
}
// builder.Append((char)ascii);
}
// Console.WriteLine(builder.ToString());
// Console.WriteLine(indata);
//Console.WriteLine("Data Received:");
//Console.Write(indata);
// Console.ReadKey();
mySerialPort.Close();
}
}
}
}
/////////////////////////////////////////////////
ReplyDelete//Read data From txt file and save into database //table
//Databbase: test (sql server)
//table: tbl_temp
// unctionality if record exist then update same record otherwise insert record
//////////////////////////////////////////////
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
namespace WIND_RND
{
static class Program
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
/*
----Object: Table [dbo].[tbl_temp] Script Date: 06/30/2013 00:44:40 ----
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_temp](
[empname] [varchar](50) NULL,
[age] [varchar](50) NULL
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
*/
string line;
string instquer = "insert into tbl_temp(empname,age) values(@empname,@age)";
string updquer = "update tbl_temp set empname=@empname,age=@age where empname=@empname";
using (StreamReader sr = new StreamReader("D:\\test.txt"))
{
while ((line = sr.ReadLine()) != null)
{
using (SqlConnection con = new SqlConnection("data source=surendra-pc; database=test; uid=sa;pwd="))
{
using (SqlCommand cmd = new SqlCommand("select count(1) from tbl_temp where empname=@empname", con))
{
cmd.Parameters.Add("@empname", SqlDbType.VarChar, 50).Value = line.Split("\t".ToCharArray())[0];
bool flag = true;
try
{
con.Open();
if (Convert.ToInt32(cmd.ExecuteScalar()) > 0)
cmd.CommandText = updquer;
else
cmd.CommandText = instquer;
}
catch { flag = false; }
finally { con.Close(); }
if (flag)
{
cmd.Parameters.Add("@age", SqlDbType.VarChar, 50).Value = line.Split("\t".ToCharArray())[1];
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch { }
finally { con.Close(); }
}
}
}
Console.WriteLine(line); // Write to console.
}
}
}
}
}
/*
----test.txt
surendra 321
hemant 24
sandeep 22
pawan mistry 271
*/
Very good sir keep it up
ReplyDelete