Serial port communication with Multiple Application

Can two Application Communicate with a single Port?

When two applications communicates with a serial port i.e. When One application send data to another application using a serial port then you will get an error . So this means you can not use a serial port in two application.

What is the Solution of it?

Lets discuss and find the solution of it.

Solution
  • Create an window application whose design as below and add the Baud Rate,Data Bit,Stop Bit,Parity Bit value.

Serial Port Form Design



  • Then browse virtual serial port driver(VSPD) and install it.
  • Open the driver which looks like below.
  • Go to "Manage Ports"  tab, select two ports and click "Add Pair" as below Image.













  • After Pairing you can see the highlighted part in the image.






  • Added the code for the respective button in the Form control code view.
 public partial class SerialPortDataReceived_New : Form
    {
        string[] port = SerialPort.GetPortNames();


        SerialPort serialport;
        public SerialPortDataReceived_New()
        }
        void Serialport_DataReceived(object sender, SerialDataReceivedEventArgs e)
        private void btnConnect_Click(object sender, EventArgs e)



        {

            InitializeComponent();
            methodserial();

        public void methodserial()

        {
            foreach (var item in port)
                cbPort.Items.Add(item);
            btnConnect.Enabled = true;
            btnDisconnect.Enabled = false;
        }

        {                    

            string indata = serialport.ReadExisting();
            txtSent.Text = "Received Data " + indata;
         }

        {

            try
            {
                string PortName = cbPort.Text;
                int BaudRate = Convert.ToInt32(cbBaudRate.Text);
                Parity parity = (Parity)Enum.Parse(typeof(Parity), cbParityBit.Text);
                StopBits stopbit = (StopBits)Enum.Parse(typeof(StopBits), cbStopBit.Text);
                int databit = Convert.ToInt32(cbDataBit.Text);
                ConnectSerialPort(PortName, BaudRate, parity, stopbit, databit);
                serialport.DataReceived += new SerialDataReceivedEventHandler(Serialport_DataReceived);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
         }
        public void ConnectSerialPort(string portname, int BaudRate, Parity parity, StopBits stopbit, int databit)
        {
            serialport = new SerialPort(portname, BaudRate, parity, databit, stopbit);
            if(!serialport.IsOpen)
            serialport.Open();
            btnConnect.Enabled = false;
            btnDisconnect.Enabled = true;
        }
        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            btnDisconnect.Enabled = false;
            btnConnect.Enabled = true;
            if(serialport.IsOpen)
            serialport.Close();
        }
        string str;
        private void btnSend_Click(object sender, EventArgs e)
        {
            
            str += TextBox1.Text;            
            serialport.Write(str);
            txtSent.Text = str;
            
        }
    }
Setup Completed...............................

Lets us go to the application Bin/Debug folder and run two instance of the application.





























You can see whatever i am sending from second application is received by 1st application.


Conclusion

  • Two application can not use single port.

  • In serial port one application lock the port , so that other application can not use the same port.

  • To enable two application to communicate with each other we need to create two virtual port and do the Pairing to it.

  • One application can send and the other application can receive data by using Pairing.

Share:

No comments:

Post a Comment

Contact for Azure Training

Name

Email *

Message *

Subscribe YouTube

Total Pageviews

Popular Posts

Labels

Recent Posts