none
Serial Communication in C#

    Question

  • Hy all,

    I am using C# to communicate with one of my embedded system.

    I am simply trying to send a data '+++'  to the system for which the external system replies OK.

    The process is verified using Brays terminal i.e on sending +++ i receive OK.

    But using my own program in C# its showing me some error.
    My code is :

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Linq; 
    using System.Text; 
    using System.Windows.Forms; 
    using System.IO.Ports; 
     
    namespace Serial_Port_Communication 
        public partial class Form1 : Form 
        { 
     
           string a = "+++"
             
            public Form1() 
            { 
                InitializeComponent(); 
                serialPort1.Open(); 
            } 
     
                   private void button1_Click(object sender, EventArgs e) 
            { 
                     
                    serialPort1.WriteLine(a); 
                    
           
            } 
     
                   
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) 
            { 
                textBox1.Text = "Data Recieved"
                textBox2.Text = serialPort1.ReadLine(); 
                 
            } 
     
            private void serialPort1_ErrorReceived(object sender, SerialErrorReceivedEventArgs e) 
            { 
                textBox1.Text = "Error Received"
                textBox3.Text = serialPort1.ReadLine(); 
            } 
        } 
     

    For the above code on clicking button 1 I get an invalid operation handle in datarecieved event pointing to textbox1 saying
    " Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on. "

    Now if i comment out textbox 1 from both the events and then click button 1 , it just i get no response at all ( atleast nuthing gets displayed on the texboxes ), but when i click close it shows another io handle exception saying "The I/O operation has been aborted because of either a thread exit or an application request."

    Can anyone please help me out please ?

    Regards

    AJ





    Wednesday, March 18, 2009 12:48 PM

Answers

  • Hi,

    As soon as a character is received you do serialPort1.ReadLine(). ReadLine() will wait for a string of characters terminated with a NewLine
    Are you sure the string you get back from your device has the specified NewLine ?
    Note: NewLine is a property of the SerialPort class which defines the string that makes up an end of line identifier which by default is '\n' or 13d 0Dh 

    Andy
    • Marked as answer by Ajay Nair Wednesday, March 18, 2009 5:40 PM
    Wednesday, March 18, 2009 4:43 PM

All replies

  • The serialport events are raised on a different thread not the main thread, you can use an anonymous delegate a shown below

            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                textBox1.Invoke(new EventHandler(delegate
                {
                    textBox1.Text = "Data Recieved";
                    textBox2.Text = serialPort1.ReadLine();
                }));
               

            }

            private void serialPort1_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
            {
                textBox1.Invoke(new EventHandler(delegate
                {
                    textBox1.Text = "Error Received";
                    textBox2.Text = serialPort1.ReadLine();
                }));
            }

    Wednesday, March 18, 2009 1:30 PM

  • Thank you !!
    Although it seems to have stopped the io exception errors , on clicking the button 1 it goes into an infinite loop without displaying anything in the textboxes so as to ascertain it has recieved any data !

    Kindly help !!

    Regards

    AJ
    Wednesday, March 18, 2009 1:59 PM
  • Hi,

    As soon as a character is received you do serialPort1.ReadLine(). ReadLine() will wait for a string of characters terminated with a NewLine
    Are you sure the string you get back from your device has the specified NewLine ?
    Note: NewLine is a property of the SerialPort class which defines the string that makes up an end of line identifier which by default is '\n' or 13d 0Dh 

    Andy
    • Marked as answer by Ajay Nair Wednesday, March 18, 2009 5:40 PM
    Wednesday, March 18, 2009 4:43 PM
  • Hey thanks for that break through !! Yes it isnt coming with a '\n'. I have to switch to read char ! and now its coming , thanks again 
    Wednesday, March 18, 2009 5:40 PM