473,406 Members | 2,312 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Forms and asynchronous Serial Communication

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.CheckForIllegalCrossT hreadCalls = 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;
}

Oct 1 '06 #1
5 7873
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:

MHi,
MI have an application where I read a serial port data from a barcode
Mand set the labels on a form. I also have a textbox and button where
Myou can enter the data and here is the problem. if I use the textbox
Mand submit using thebutton everything works fine. If I use the
MSerialDataReceivedEventHandler method of the serial port, the
Mapplication crahes. It seens like that when the serial port triggers
Mand I call the same routines it is not aware of the form elements and
Mhence the application hangs up. Any body has ideas on what is the
Mfix?
Mif I debug, I get the following message on when I go over the label
Min
Mthe routine

MAutoEllipsis = Function evaluation disabled because a previous
Mfunction
Mevaluation timed out. You must continue execution to reenable
Mfunction
Mevaluation.

MHere is the code

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

MSystem.Windows.Forms.Control.CheckForIllegalCross ThreadCalls = 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
MSerialDataReceivedEventHandler(sp_DataReceived);
M sp.Open();

M }
M void sp_DataReceived(object sender,
MSerialDataReceivedEventArgs
Me)
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 {

MmyLabel.text = theNumber;
M}
With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
Oct 1 '06 #2
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

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

"Mo" <le******@yahoo.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
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
Nov 13 '06 #4

"Nguy?n Dan Phuong" <ng*************@aim.comwrote in message
news:F3**********************************@microsof t.com...
| 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.


Nov 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Noel | last post by:
Hi, I am a tad confused about if there are any benefits from using asynchronous vs synchronous network communication. As my example, I have been writing a dns lookup stack with a network...
1
by: Andreas Horneff | last post by:
Hi @ all, I've got a problem with serial communication in Borland C++ Builder. I've already found a lot of stuff about serial communication in the internet, but it dosen't work. What I want...
3
by: carmen | last post by:
I'm working in an aplication for a Smart Device that need to "talk" with a printer continuosly through the serial port. I'm trying to use the John Hint's sample code "Use P/Invoke to develop a .NET...
4
by: Vidya Bhagwath | last post by:
Hello Experts, I am porting the C++ code into the Visual C#.NET. My C++ code is mainly based on the serial communication. So I am using the windows structure such as DCB.. etc and the windows...
6
by: Leandro Berti via DotNetMonster.com | last post by:
Hi All, I wrote a code to do serial communication with an equipament. When i use the code outside of threaded class it seens work properly, but when i put inside a class and execute a thread in...
4
by: joe bloggs | last post by:
I am writing a mobile application to interface with a legacy system and I am planning to use web services to communicate with this system. The legacy system receives data through a serial port. ...
8
by: panko | last post by:
Hello, I can't manage with asynchronous socket communication. :( I wrote a class CSocket.cs. This class is taking care of sending strings to LED display. This display is actually communicating...
2
by: Adrian Chen | last post by:
please help me! I come across a problem. Now I develop a finger print management system which is based on B/S.When users click a button in the web pages, a device connected to the COM1 serial port...
0
by: Dhananjay | last post by:
Hi, I am working on an VB.Net application which I want to communicate to external device using comm port (Serial Port) . So for that first I am trying to simulate the communication on serial...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.