Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SerialPort sp;
C#
 Public string[] portnames=SerialPort.GetPortNames();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {           
           foreach (string s in portnames)
            {
                comport.ItemsSource = OrderedPortNames();
            }
        }

        private void Read_Click(object sender, RoutedEventArgs e)
        {

sp = new System.IO.Ports.SerialPort(comport.Text, Convert.ToInt32(baud.Text), Parity.None, Convert.ToInt32(bitrat.Text), StopBits.One);
 sp.DataReceived += new SerialDataReceivedEventHandler(receive_data);           
   
     private void receive_data (object sender, SerialDataReceivedEventArgs e)
        {
           string data = sp.ReadExisting();
            MyLog(data);   
        }
      
       private void MyLog(string msg)
        {
            this.Dispatcher.Invoke(new DisplayTextDelegate(DisplayText), data);          
        }

        private void DisplayText(string text)
        {            
            textBox1.AppendText(text);
        }

        private void write_Click(object sender, RoutedEventArgs e)
        {
            sp.Write(textBox2.Text);
            textBox2.Text = "";
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            if (sp.IsOpen)
                sp.Close();   
        }



This is a small Chat program i wrote for Serialport Data Receive and Send,my question is if we send data from one COM Port to other ,other COMport should read continuously ,for example if i send 'HELLO' ,in other COM Port read 'HELLO' continuous un limited,giving one checkbox to stop loop, using Threading can any one help please, i used TextBoxes for sending and receiving data .
Posted
Updated 14-Mar-12 2:18am
v3
Comments
getanswer 14-Mar-12 11:36am    
Hi lukeer, thx for your answer, i tried with threading and while loop giving error,i dont want to display once receiveddata but i want to Receiving Data continuously unless we stop,
thx

1 solution

You've been given the MSDN link for the SerialPort class as an answer to your previous question[^].

For data reception, have a look at the DataReceived[^] event. It fires when data is received. You then can check how much data is available and read it. An example for that is given in the link.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900