Forms and asynchronous Serial Communication

V

Vadym Stetsyak

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
 
M

Mo

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;
}
 
M

Mo

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
 
G

Guest

Add the following line to Form's constructor:
Control.CheckForIllegalCrossThreadCalls = false;
It should work. I had same problem before.
 
W

Willy Denoyette [MVP]

| 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top