deepender singla

deepender singla

  • NA
  • 113
  • 76.8k

Serial data recieved help

Aug 10 2011 2:52 AM
in my first program i am recieving serial data from com 4 using this program:
#region Namespace Inclusions
using System;
using System.IO.Ports;
using System.Windows.Forms;
using System.Threading;
#endregion


namespace SerialPortExample
{
    class SerialPortProgram
    {
        // Create the serial port with basic settings
        private SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);


        [STAThread]
        static void Main(string[] args)
        {
            // Instatiate this class
            new SerialPortProgram();
        }


        private SerialPortProgram()
        {
            //Console.WriteLine("Incoming Data:");


            // Attach a method to be called when there
            // is data waiting in the port's buffer
            port.DataReceived += new
              SerialDataReceivedEventHandler(port_DataReceived);


            // Begin communications
            port.Open();


            // Enter an application loop to keep this thread alive
            Application.Run();
        }


        private void port_DataReceived(object sender,
          SerialDataReceivedEventArgs e)
        {
            // Show all the incoming data in the port's buffer
            string data = port.ReadExisting();
            //Console.WriteLine(data);
            Thread.Sleep(200);
            System.IO.File.WriteAllText("C://Users//intel//Documents//Visual Studio 2010//Projects//ConsoleApplication4//serialdata2.txt", data);
            Thread.Sleep(200);
            // System.Diagnostics.Process.Start("C:\\Users\\intel\\Documents\\Visual Studio 2010\\Projects\\ConsoleApplication5\\ConsoleApplication5\\bin\\Debug\\ConsoleApplication5.exe");
            port.Close();
            Application.Exit();


        }
    }
}



then in my second program i am sending this data to com1 using this program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;


using System.Threading;


namespace cConsoleAppMonitorServoCompletion
{
    class Program
    {
        static SerialPort _serialPort;


        static void Main(string[] args)
        {
         //   try
          //  {
                _serialPort = new SerialPort("COM1",115200,Parity.None,8,StopBits.One);
                //_serialPort.PortName = "COM1";
                //_serialPort.BaudRate =
   
                _serialPort.Open();
                string data = System.IO.File.ReadAllText("C://Users//intel//Documents//Visual Studio 2010//Projects//ConsoleApplication4//serialdata2.txt");
               // Console.Write(data == "45");
             //oreach (char c in "#3 P1546 \r") Console.Write("{0:X2} ", (int)c);
               //hread.Sleep(200000);
              //  foreach (char c in data) Console.Write("{0:X2} ", (int)c);
    // Thread.Sleep(200000);


    data= data.Substring(0, data.Length - 0) + "\r";  




                //string data2 = "#3 P1500 \r";
                //Console.WriteLine(data2 == data);
                //data.ToString();
                //Thread.Sleep(200);
               // StringComparison(data,"#3 P1500 \r");
           // string data=System.IO.File.ReadAllText
                _serialPort.Write(data);


             //   Console.WriteLine("#0 P500 \r");  
                //string output;
              //  output = "";
                //Example: "Q <cr>" 
               // This will return a "." if the previous move is complete, or a "+" if it is still in progress. 
            /*   while (!(output == ".")) //loop until you get back a period 
               {    
                    _serialPort.Write("Q \r");
                    output = _serialPort.ReadExisting();
                    Console.WriteLine(output);
                    Thread.Sleep(10);
                }*/
               Thread.Sleep(100); 
            _serialPort.Close();
          // }
          // catch (TimeoutException) { }
        }
    }
}

now when i am running program one by one myself they are working fine but i want to run them in continuous loop that is for(i=0;i<10,i++)
( both program exe)

but when i don this first program in which i am recieving data create problem in middle of loop it stop working and throws an exception , so how i can do this any help will be  greatly appreciatated.











Answers (4)