ravikiran32 0 Newbie Poster

I am just a beginner in .net and i have written the following code to execute command "AT" on the mobile and receive response "ok" from the mobile. I have completed the task learning from some example code. but what i would like to know is, how "AutoResetEvent" is working here. how the threads are divided in this code and i have not defined any thread here. while calling "reset() on AutoResetEvent objectref" which thread is blocking and which thread is running and after calling waitone on AutoResetEvent objectref which thread is waiting and get invoked after timeout(which is given as argument to waitone() function. please help me, i have read about these classes and functions in the internet source but in vain.

public partial class Form1 : Form
    {
        SerialPort sp = new SerialPort("com1", 9600, Parity.None, 8, StopBits.One);
        public AutoResetEvent receivenow;
        public Form1()
        {
            InitializeComponent();
        }

     
        private void button1_Click(object sender, EventArgs e)
        {
                       
            receivenow = new AutoResetEvent(false);
            sp.DiscardInBuffer();
            sp.DiscardOutBuffer();
        
            sp.WriteLine("At"+"\r");
  receivenow.Reset();
          
        }

    private void port_datareceived(object sender, SerialDataReceivedEventArgs args)
    {
            if (args.EventType== SerialData.Chars)
        {
                 receivenow.Set();
          if(receivenow.WaitOne(300,false))
           {
            string responsetocmd = sp.ReadExisting();
          MessageBox.Show(responsetocmd);
           }
                   
        }
       else { MessageBox.Show("not character type"); }

    }


        public SerialPort OpenPort(string p_strPortName, int p_uBaudRate, int p_uDataBits, int p_uReadTimeout, int p_uWriteTimeout)
        {
            receivenow = new AutoResetEvent(false);
            SerialPort port = new SerialPort();

            try
            {
                port.PortName = p_strPortName;                 //COM1
                port.BaudRate = p_uBaudRate;                   //9600
                port.DataBits = p_uDataBits;                   //8
                port.StopBits = StopBits.One;                  //1
                port.Parity = Parity.None;                     //None
                port.ReadTimeout = p_uReadTimeout;             //300
                port.WriteTimeout = p_uWriteTimeout;           //300
                port.Encoding = Encoding.GetEncoding("iso-8859-1");
               port.DataReceived += new SerialDataReceivedEventHandler(port_datareceived);
                port.Open();
                port.DtrEnable = true;
                port.RtsEnable = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return port;
        }

        //Close Port
        public SerialPort  ClosePort(SerialPort port)
        {
            try
            {
                port.Close();
                port.DataReceived -= new SerialDataReceivedEventHandler(port_datareceived);
                port = null;
                return (port);
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
           sp= this.OpenPort("COM6", 2400, 8, 300, 300);
           if (this.sp != null)
           {
               MessageBox.Show("port"+sp.PortName.ToString()+" created succefuully");
           }
           else
               MessageBox.Show("port not created ");


        }

        private void button3_Click(object sender, EventArgs e)
        {
           sp= this.ClosePort(sp);
            if (this.sp == null)
                MessageBox.Show("port closed");
            else
                MessageBox.Show("port not clsoed");
        }

    }

thanks in advance......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.