Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Forms and asynchronous Serial Communication

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mo - 01 Oct 2006 04:00 GMT
Hi,
I have an application where I read a serial port data from a barcode
and set the labels on a form. I also have a textbox and button where
you can enter the data and here is the problem. if I use the textbox
and submit using thebutton everything works fine. If I use the
SerialDataReceivedEventHandler method  of the serial port, the
application crahes. It seens like that when the serial port triggers
and I call the same routines it is not aware of the form elements and
hence the application hangs up. Any body has ideas on what is the fix?
if I debug, I get the following message on when I go over the label in
the routine

AutoEllipsis = Function evaluation disabled because a previous function
evaluation timed out. You must continue execution to reenable function
evaluation.

Here is the code

namespace ShippingLabel
{
   public partial class xxxx : Form
   {
       SerialPort sp = new SerialPort();
       public NDES2()
       {
           InitializeComponent();

System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
           sp.BaudRate = 9600;
           sp.Parity = Parity.None;
           sp.DataBits = 8;
           sp.StopBits = StopBits.One;
           sp.ReadTimeout = 1500;
           sp.DataReceived += new
SerialDataReceivedEventHandler(sp_DataReceived);
           sp.Open();

       }
       void sp_DataReceived(object sender, SerialDataReceivedEventArgs
e)
       {
           try
           {
               string ret = "\r";
               string ID = sp.ReadLine().Replace(ret, "");
               sp.Close();
               TheNumber.Text = ID;
               Generate_Label(ID);
                sp.Open();

           }
           catch
           {
               ErrorLabel.Text += "Scanner Communication Failed";
               sp.Close();
           }
       }
public void Generate_Label(string theNumber)
       {

myLabel.text = theNumber;
}
Vadym Stetsyak - 30 Sep 2006 19:14 GMT
Hello, Mo!

You have to use Control.Invoke method to access your
UI.

From the docs on DataReceived
"The DataReceived event is raised on a secondary thread when data is
received from the SerialPort object. Because this event is raised on a
secondary thread, and not the main thread, attempting to modify some
elements in the main thread, such as UI elements, could raise a threading
exception. If it is necessary to modify elements in the main Form or
Control, post change requests back using Invoke, which will do the work on
the proper thread.
"

Have a look at ( http://www.codeproject.com/csharp/winformthreading.asp ) to
see how to handle mutlithreading in WinForms

You wrote  on 30 Sep 2006 20:00:57 -0700:

M> Hi,
M> I have an application where I read a serial port data from a barcode
M> and set the labels on a form. I also have a textbox and button where
M> you can enter the data and here is the problem. if I use the textbox
M> and submit using thebutton everything works fine. If I use the
M> SerialDataReceivedEventHandler method  of the serial port, the
M> application crahes. It seens like that when the serial port triggers
M> and I call the same routines it is not aware of the form elements and
M> hence the application hangs up. Any body has ideas on what is the
M> fix?
M> if I debug, I get the following message on when I go over the label
M> in
M> the routine

M> AutoEllipsis = Function evaluation disabled because a previous
M> function
M> evaluation timed out. You must continue execution to reenable
M> function
M> evaluation.

M> Here is the code

M> namespace ShippingLabel
M> {
M>     public partial class xxxx : Form
M>     {
M>         SerialPort sp = new SerialPort();
M>         public NDES2()
M>         {
M>             InitializeComponent();

M> System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
M>             sp.BaudRate = 9600;
M>             sp.Parity = Parity.None;
M>             sp.DataBits = 8;
M>             sp.StopBits = StopBits.One;
M>             sp.ReadTimeout = 1500;
M>             sp.DataReceived += new
M> SerialDataReceivedEventHandler(sp_DataReceived);
M>             sp.Open();

M>         }
M>         void sp_DataReceived(object sender,
M> SerialDataReceivedEventArgs
M> e)
M>         {
M>             try
M>             {
M>                 string ret = "\r";
M>                 string ID = sp.ReadLine().Replace(ret, "");
M>                 sp.Close();
M>                 TheNumber.Text = ID;
M>                 Generate_Label(ID);
M>                  sp.Open();

M>             }
M>             catch
M>             {
M>                 ErrorLabel.Text += "Scanner Communication Failed";
M>                 sp.Close();
M>             }
M>         }
M>  public void Generate_Label(string theNumber)
M>         {

M> myLabel.text = theNumber;
M> }

With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
Mo - 12 Nov 2006 19:09 GMT
Well, I am getting closer but still no Cigar!! I have been able to
delegate and invoke but now I am getting the Cross Thread error in the
method I am invoking. When I receive the serial data, I execute the
following code in the Serial_Port_Data_Received thread

SetTextValueHandler VH = new SetTextValueHandler(SetTextValue);
VH.Invoke(tempstring);

Which in turn calls the following delegate
 delegate void SetTextValueHandler(string value);

       void SetTextValue(string value)
       {
          TXNumber.Text = value;
       }

and in the the line where I set the text value (  TXNumber.Text =
value;) I am getting the cross thread error. Any ideas on how to
resolve this is greatly appreciated.

Mo
Nguyễn Đan Phương - 13 Nov 2006 14:19 GMT
Add the following line to Form's constructor:
Control.CheckForIllegalCrossThreadCalls = false;
It should work. I had same problem before.

> Well, I am getting closer but still no Cigar!! I have been able to
> delegate and invoke but now I am getting the Cross Thread error in the
[quoted text clipped - 17 lines]
>
> Mo
Willy Denoyette [MVP] - 13 Nov 2006 14:56 GMT
| Add the following line to Form's constructor:
| Control.CheckForIllegalCrossThreadCalls = false;
| It should work. I had same problem before.

Bad suggestion, you are just fooling yourself by doing this, Illegal
cross-thread calls shouldn't be ignored, the property is there to help you
diagnose the problem not to ignore it.

Willy.
google@davesvillage.com - 21 Nov 2006 02:09 GMT
http://csharp.simpleserial.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.