How to bind comport in c#.Net application.


1.    All process of the binding comport and recive data(both with richtext box and datarecieved event).


-         First i will talk about the what is comport and how it is used

-         Comport is one type of the commination port which is use to send and receive the data from the machine which is connected to computer with comport

-         When we want to use comport we have to bind this comport first then we use it.

-         Comport has some properties like board rate, parity bit, port numbered…

-         You can receive or send the data from the comport by two or more way here I explain two way

A.      receive the data by the comport data received event

 

A. using data received event

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.IO.Ports;

 

namespace ComportExample

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        StringBuilder strbd;

       

        string tmpstr = "";

 

        private void btnBind_Click(object sender, EventArgs e)

        {

            initcomport();

           

        }

 

        void objPort_DataReceived(object sender, SerialDataReceivedEventArgs e)

        {

            SerialPort sp=(SerialPort)sender;

            string str = "";

            str=sp.ReadExisting();

            tmpstr = str;

          

 

 

        }

 

        private void makestring()

        {

            strbd.Append(tmpstr);

            tmpstr = "";

            if (strbd.Length>100)

            {

                MessageBox.Show(strbd.ToString());

            }

        }

 

    

        private void Form1_Load(object sender, EventArgs e)

        {

            getComport();

        }

 

        private void getComport()

        {

            string[] ports = SerialPort.GetPortNames();

            if (ports.Length > 0)

            {

                for (int i = 0; i < ports.Length; i++)

                {

                    cbComport.Items.Add(ports[i].ToString());

                }

                cbComport.SelectedIndex = 0;

 

            }

        }

 

        private void initcomport()

        {

            try

            {

                SerialPort objPort = new SerialPort();

                objPort.PortName = cbComport.Text;

                objPort.BaudRate = 9600;

                objPort.DataBits = 8;

                objPort.StopBits = 0;

                objPort.Parity = 0;

                objPort.Open();

 

                objPort.DataReceived += objPort_DataReceived;

            }

            catch (Exception ex)

            {

 

                MessageBox.Show(ex.Message);

            }

        }

 

 

 

    }

}

 

 

Descriptions:

1.    First we have to add two name space

 using System.IO;

     using System.IO.Ports;

 

2.    now we bind the all the comport available on your PC

 

        private void getComport()

        {

            string[] ports = SerialPort.GetPortNames();

            if (ports.Length > 0)

            {

                for (int i = 0; i < ports.Length; i++)

                {

                    cbComport.Items.Add(ports[i].ToString());

                }

                cbComport.SelectedIndex = 0;

 

            }

        }

3.  now for the comport first we have to assign this and bind the comport this is given below, also we open the comport after open command u use the comport

 

  SerialPort objPort = new SerialPort();

                objPort.PortName = cbComport.Text;

                objPort.BaudRate = 9600;

                objPort.DataBits = 8;

                objPort.StopBits = 0;

                objPort.Parity = 0;

             objPort.Open();

 

 

4.      now we are write some data to the comport it is give as

objPort.Write("OPEN");

if u want to pass enter key on the comport the write command like

objPort.Write("OPEN"+”\r\n”);

 

5.      on give the command on the comport we receive the text (data) from the comport so we create comport receive event for the receiving data from the comport

objPort.DataReceived += objPort_DataReceived;

   void objPort_DataReceived(object sender, SerialDataReceivedEventArgs e)

   {

            SerialPort sp=(SerialPort)sender;

            string str = "";

            str=sp.ReadExisting();

            tmpstr = str;

     makestring();

   }

   private void makestring()

   {

            strbd.Append(tmpstr);

            tmpstr = "";

            if (strbd.Length>100)

     {

                richTextBox1.AppendText(tmpstr);

                MessageBox.Show(strbd.ToString());

 

            }

   }


See Also :   Follow Me On Facebook

--
/\/ir@\/  <(.'.)>

Comments

Popular posts from this blog

How to make and use web service in Microsoft Dynamics Navision.

How to use Format function in Microsoft Dynamics Navision.

How to create simple report in Microsoft Dynamics Navision.