Friday, June 28, 2013

How to Read Data From Comport and save it in txt file in console application

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

            }



        }
    }
}