473,804 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using Serialports with Win Forms

I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output to a
textBox in my application for viewing. I however keep getting this error and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike
Nov 17 '05 #1
11 9122
Use the TextBox.Invoke or BeginInvoke to call a method that updates the text
box from the UI thread. What you're doing is updating the UI from a non-UI
thread.

--
Floyd

"Michael Tallhamer" <Mi************ **@discussions. microsoft.com> wrote in
message news:5C******** *************** ***********@mic rosoft.com...
I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output
to a
textBox in my application for viewing. I however keep getting this error
and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike

Nov 17 '05 #2
Thanks you were correct. After some looking I also found a good explanaton of
this here http://msmvps.com/coad/archive/2005/03/23/39466.aspx if anyone else
has this type of problem and needs some help as well

I however have a new question. I am attached to and accelerator that takes
internal measurements on a set of two ion chambers and automatically dumps
the data to the port after the data sets are read off the chambers. The
problem I have is the streams of data aren't always uniform in length so I
can't set a threshold for the DataRecieved event to be fired. I need some way
of determining when the stream of data is complete so I can then read in the
entire stream, parse it, and write out the values to a SQL database. I can't
seam to figure out how to determine the end of the stream since there is
nothing like a DataTransferCom plete event handler. I was thinking of trying
to use the SerialData.Eof object but need to know how to catch it and/or
idetify it in the bit stream and don't quite know how to set it up (possibly
my inexperience with the .Net Framework showing through)

Thanks again Floyd
--
Mike -- mi************@ usoncology.com
"Floyd Burger" wrote:
Use the TextBox.Invoke or BeginInvoke to call a method that updates the text
box from the UI thread. What you're doing is updating the UI from a non-UI
thread.

--
Floyd

"Michael Tallhamer" <Mi************ **@discussions. microsoft.com> wrote in
message news:5C******** *************** ***********@mic rosoft.com...
I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output
to a
textBox in my application for viewing. I however keep getting this error
and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike


Nov 17 '05 #3
Mike,
I'm not familiar with the SerialPort, I've used it but it's not as easy to
collect data as the ComPort in the TransPort library from ComponentScienc e.
You might want to take a look at that. You can define a regex to match your
block of data.
Other than that, read in each byte and construct the response block
manually. Chances are that the block will have a terminator char that you
can watch for.
--
Floyd
"Michael Tallhamer" wrote:
Thanks you were correct. After some looking I also found a good explanaton of
this here http://msmvps.com/coad/archive/2005/03/23/39466.aspx if anyone else
has this type of problem and needs some help as well

I however have a new question. I am attached to and accelerator that takes
internal measurements on a set of two ion chambers and automatically dumps
the data to the port after the data sets are read off the chambers. The
problem I have is the streams of data aren't always uniform in length so I
can't set a threshold for the DataRecieved event to be fired. I need some way
of determining when the stream of data is complete so I can then read in the
entire stream, parse it, and write out the values to a SQL database. I can't
seam to figure out how to determine the end of the stream since there is
nothing like a DataTransferCom plete event handler. I was thinking of trying
to use the SerialData.Eof object but need to know how to catch it and/or
idetify it in the bit stream and don't quite know how to set it up (possibly
my inexperience with the .Net Framework showing through)

Thanks again Floyd
--
Mike -- mi************@ usoncology.com
"Floyd Burger" wrote:
Use the TextBox.Invoke or BeginInvoke to call a method that updates the text
box from the UI thread. What you're doing is updating the UI from a non-UI
thread.

--
Floyd

"Michael Tallhamer" <Mi************ **@discussions. microsoft.com> wrote in
message news:5C******** *************** ***********@mic rosoft.com...
I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output
to a
textBox in my application for viewing. I however keep getting this error
and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng()); }
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike


Nov 17 '05 #4
Mike,
I'm not familiar with the SerialPort, I've used it but it's not as easy to
collect data as the ComPort in the TransPort library from ComponentScienc e.
You might want to take a look at that. You can define a regex to match your
block of data.
Other than that, read in each byte and construct the response block
manually. Chances are that the block will have a terminator char that you
can watch for.
--
Floyd
"Michael Tallhamer" wrote:
Thanks you were correct. After some looking I also found a good explanaton of
this here http://msmvps.com/coad/archive/2005/03/23/39466.aspx if anyone else
has this type of problem and needs some help as well

I however have a new question. I am attached to and accelerator that takes
internal measurements on a set of two ion chambers and automatically dumps
the data to the port after the data sets are read off the chambers. The
problem I have is the streams of data aren't always uniform in length so I
can't set a threshold for the DataRecieved event to be fired. I need some way
of determining when the stream of data is complete so I can then read in the
entire stream, parse it, and write out the values to a SQL database. I can't
seam to figure out how to determine the end of the stream since there is
nothing like a DataTransferCom plete event handler. I was thinking of trying
to use the SerialData.Eof object but need to know how to catch it and/or
idetify it in the bit stream and don't quite know how to set it up (possibly
my inexperience with the .Net Framework showing through)

Thanks again Floyd
--
Mike -- mi************@ usoncology.com
"Floyd Burger" wrote:
Use the TextBox.Invoke or BeginInvoke to call a method that updates the text
box from the UI thread. What you're doing is updating the UI from a non-UI
thread.

--
Floyd

"Michael Tallhamer" <Mi************ **@discussions. microsoft.com> wrote in
message news:5C******** *************** ***********@mic rosoft.com...
I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output
to a
textBox in my application for viewing. I however keep getting this error
and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng()); }
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike


Nov 17 '05 #5
Thanks for the suggestion Floyd. I was able to figure out a solution and have
pasted the port_Datareciev ed event handler code I wrote below in the event
someone out there comes up against a similar problem. It may not be the most
elegant solution but it appears to work very efficiently and handled hundreds
of random length data transfers in its initial testing last night. I ended up
using the System.IO.Ports .SerialPort.Rea dTimeout property and the
System.TimeoutE xception in a try/catch/finally statement to identify the end
of the serial stream. Hope this is able to help someone else out there and if
it does let me know what you think or if you have any better suggestions. I'm
always learning

EVENT HANDLER CODE:

private void port_DataReceiv ed(object sender, SerialDataRecei vedEventArgs e)
{
//Setup a bool for use in the while loop that
//will continue to monitor the input stream
bool readingStatus = true;

//String object to recieve the stream as it is read in
string inputString = "";

//Set the ReadTimeout property to be used to triger
//the System.TimeoutE xception that will identify the
//end of the stream
port.ReadTimeou t = 1000;

//If the window state is minimized bring the terminal window
//up so you can view the output
if ((this.WindowSt ate != FormWindowState .Normal) ||
(this.WindowSta te != FormWindowState .Maximized))
{
ViewMainApp popupHandler = new ViewMainApp(Pop UpMainApp);
this.Invoke(pop upHandler);
}

//Used to catch the System.TimeoutE xception when it occurs
try
{
while (readingStatus)
{
inputString += port.ReadLine() + "\n";
}
}
catch (TimeoutExcepti on exception)
{
//reset the Readtimeout to InfiniteTimeout to prepare for
the next data set
port.ReadTimeou t = SerialPort.Infi niteTimeout;
}
finally
{
//Set textBox.Text to inputString via an Invoke from the
application thread
SetTextDelegate handler = new SetTextDelegate (SetText);
this.Invoke(han dler,inputStrin g.ToString());

//Clear the InBuffer to get ready for the next data set
port.DiscardInB uffer();
}
}
--
Mike - mi************@ usoncology.com
"Floyd" wrote:
Mike,
I'm not familiar with the SerialPort, I've used it but it's not as easy to
collect data as the ComPort in the TransPort library from ComponentScienc e.
You might want to take a look at that. You can define a regex to match your
block of data.
Other than that, read in each byte and construct the response block
manually. Chances are that the block will have a terminator char that you
can watch for.
--
Floyd
"Michael Tallhamer" wrote:
Thanks you were correct. After some looking I also found a good explanaton of
this here http://msmvps.com/coad/archive/2005/03/23/39466.aspx if anyone else
has this type of problem and needs some help as well

I however have a new question. I am attached to and accelerator that takes
internal measurements on a set of two ion chambers and automatically dumps
the data to the port after the data sets are read off the chambers. The
problem I have is the streams of data aren't always uniform in length so I
can't set a threshold for the DataRecieved event to be fired. I need some way
of determining when the stream of data is complete so I can then read in the
entire stream, parse it, and write out the values to a SQL database. I can't
seam to figure out how to determine the end of the stream since there is
nothing like a DataTransferCom plete event handler. I was thinking of trying
to use the SerialData.Eof object but need to know how to catch it and/or
idetify it in the bit stream and don't quite know how to set it up (possibly
my inexperience with the .Net Framework showing through)

Thanks again Floyd
--
Mike -- mi************@ usoncology.com
"Floyd Burger" wrote:
Use the TextBox.Invoke or BeginInvoke to call a method that updates the text
box from the UI thread. What you're doing is updating the UI from a non-UI
thread.

--
Floyd

"Michael Tallhamer" <Mi************ **@discussions. microsoft.com> wrote in
message news:5C******** *************** ***********@mic rosoft.com...
>I am a medical physicist new to the .Net Framework and am trying to write a
> simple app that will monitor the output from a serial port on one of my
> linear accelerators using the new Serialport object and write the output
> to a
> textBox in my application for viewing. I however keep getting this error
> and
> am unable to figure out how to get around this.
>
> System.InvalidO perationExcepti on was unhandled
> Message="Cross-thread operation not valid: Control 'textBox1' accessed
> from a thread other than the thread it was created on."
> Source="System. Windows.Forms"
>
> I have attached the code from my simple form for review in the event some
> one can help me
>
> Thank You
> Michael Tallhamer
>
> using System;
> using System.Collecti ons.Generic;
> using System.Componen tModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows. Forms;
> using System.IO;
> using System.IO.Ports ;
>
> namespace MorningCheckRea der
> {
> public partial class Form1 : Form
> {
> // Create the serial port with basic settings
> private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
> 8,
> StopBits.One);
>
> public Form1()
> {
> InitializeCompo nent();
> }
>
> private void button1_Click(o bject sender, EventArgs e)
> {
> // Attach a method to be called when there is data waiting in
> the port's
> buffer
> port.DataReceiv ed += new SerialDataRecei vedEventHandler
>
> (port_DataRecei ved);
>
> //Open the port
> port.Open();
> }
>
> private void button2_Click(o bject sender, EventArgs e)
> {
> port.Close();
> }
>
> private void port_DataReceiv ed(object sender,
> SerialDataRecei vedEventArgs e)
> {
>
> // Show all the incoming data in the port's buffer
> if (textBox1.Text. Length > 0)
> {
> textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
> 1,
>
> port.ReadExisti ng()); }
> else
> {
> textBox1.Text = port.ReadExisti ng();
> }
> }
> }
> }
>
>
> --
> Mike

Nov 17 '05 #6
Thanks for the suggestion Floyd. I was able to figure out a solution and have
pasted the port_Datareciev ed event handler code I wrote below in the event
someone out there comes up against a similar problem. It may not be the most
elegant solution but it appears to work very efficiently and handled hundreds
of random length data transfers in its initial testing last night. I ended up
using the System.IO.Ports .SerialPort.Rea dTimeout property and the
System.TimeoutE xception in a try/catch/finally statement to identify the end
of the serial stream. Hope this is able to help someone else out there and if
it does let me know what you think or if you have any better suggestions. I'm
always learning

EVENT HANDLER CODE:

private void port_DataReceiv ed(object sender, SerialDataRecei vedEventArgs e)
{
//Setup a bool for use in the while loop that
//will continue to monitor the input stream
bool readingStatus = true;

//String object to recieve the stream as it is read in
string inputString = "";

//Set the ReadTimeout property to be used to triger
//the System.TimeoutE xception that will identify the
//end of the stream
port.ReadTimeou t = 1000;

//If the window state is minimized bring the terminal window
//up so you can view the output
if ((this.WindowSt ate != FormWindowState .Normal) ||
(this.WindowSta te != FormWindowState .Maximized))
{
ViewMainApp popupHandler = new ViewMainApp(Pop UpMainApp);
this.Invoke(pop upHandler);
}

//Used to catch the System.TimeoutE xception when it occurs
try
{
while (readingStatus)
{
inputString += port.ReadLine() + "\n";
}
}
catch (TimeoutExcepti on exception)
{
//reset the Readtimeout to InfiniteTimeout to prepare for
the next data set
port.ReadTimeou t = SerialPort.Infi niteTimeout;
}
finally
{
//Set textBox.Text to inputString via an Invoke from the
application thread
SetTextDelegate handler = new SetTextDelegate (SetText);
this.Invoke(han dler,inputStrin g.ToString());

//Clear the InBuffer to get ready for the next data set
port.DiscardInB uffer();
}
}
--
Mike - mi************@ usoncology.com
"Floyd" wrote:
Mike,
I'm not familiar with the SerialPort, I've used it but it's not as easy to
collect data as the ComPort in the TransPort library from ComponentScienc e.
You might want to take a look at that. You can define a regex to match your
block of data.
Other than that, read in each byte and construct the response block
manually. Chances are that the block will have a terminator char that you
can watch for.
--
Floyd
"Michael Tallhamer" wrote:
Thanks you were correct. After some looking I also found a good explanaton of
this here http://msmvps.com/coad/archive/2005/03/23/39466.aspx if anyone else
has this type of problem and needs some help as well

I however have a new question. I am attached to and accelerator that takes
internal measurements on a set of two ion chambers and automatically dumps
the data to the port after the data sets are read off the chambers. The
problem I have is the streams of data aren't always uniform in length so I
can't set a threshold for the DataRecieved event to be fired. I need some way
of determining when the stream of data is complete so I can then read in the
entire stream, parse it, and write out the values to a SQL database. I can't
seam to figure out how to determine the end of the stream since there is
nothing like a DataTransferCom plete event handler. I was thinking of trying
to use the SerialData.Eof object but need to know how to catch it and/or
idetify it in the bit stream and don't quite know how to set it up (possibly
my inexperience with the .Net Framework showing through)

Thanks again Floyd
--
Mike -- mi************@ usoncology.com
"Floyd Burger" wrote:
Use the TextBox.Invoke or BeginInvoke to call a method that updates the text
box from the UI thread. What you're doing is updating the UI from a non-UI
thread.

--
Floyd

"Michael Tallhamer" <Mi************ **@discussions. microsoft.com> wrote in
message news:5C******** *************** ***********@mic rosoft.com...
>I am a medical physicist new to the .Net Framework and am trying to write a
> simple app that will monitor the output from a serial port on one of my
> linear accelerators using the new Serialport object and write the output
> to a
> textBox in my application for viewing. I however keep getting this error
> and
> am unable to figure out how to get around this.
>
> System.InvalidO perationExcepti on was unhandled
> Message="Cross-thread operation not valid: Control 'textBox1' accessed
> from a thread other than the thread it was created on."
> Source="System. Windows.Forms"
>
> I have attached the code from my simple form for review in the event some
> one can help me
>
> Thank You
> Michael Tallhamer
>
> using System;
> using System.Collecti ons.Generic;
> using System.Componen tModel;
> using System.Data;
> using System.Drawing;
> using System.Text;
> using System.Windows. Forms;
> using System.IO;
> using System.IO.Ports ;
>
> namespace MorningCheckRea der
> {
> public partial class Form1 : Form
> {
> // Create the serial port with basic settings
> private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
> 8,
> StopBits.One);
>
> public Form1()
> {
> InitializeCompo nent();
> }
>
> private void button1_Click(o bject sender, EventArgs e)
> {
> // Attach a method to be called when there is data waiting in
> the port's
> buffer
> port.DataReceiv ed += new SerialDataRecei vedEventHandler
>
> (port_DataRecei ved);
>
> //Open the port
> port.Open();
> }
>
> private void button2_Click(o bject sender, EventArgs e)
> {
> port.Close();
> }
>
> private void port_DataReceiv ed(object sender,
> SerialDataRecei vedEventArgs e)
> {
>
> // Show all the incoming data in the port's buffer
> if (textBox1.Text. Length > 0)
> {
> textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
> 1,
>
> port.ReadExisti ng()); }
> else
> {
> textBox1.Text = port.ReadExisti ng();
> }
> }
> }
> }
>
>
> --
> Mike

Nov 17 '05 #7
ZS

Hi ,
What is your .net framework version.

I'm trying to develop an application similar to yours and am having trouble.
Is there a namespace System.IO.Ports

-ZS
"Michael Tallhamer" wrote:
I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output to a
textBox in my application for viewing. I however keep getting this error and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike

Feb 23 '06 #8
At the time of this post I was using the beta 2 release but now I am using
the .Net 2.0 Framework. The System.IO.Ports .SerialPort object will handle all
of the serial port communications based on the properties you set up for the
connection.

--
Mike
"ZS" wrote:

Hi ,
What is your .net framework version.

I'm trying to develop an application similar to yours and am having trouble.
Is there a namespace System.IO.Ports

-ZS
"Michael Tallhamer" wrote:
I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output to a
textBox in my application for viewing. I however keep getting this error and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike

Feb 23 '06 #9
ZS
Is there a way to achieve the same using the 1.1 framework structure.
Thanks for your response.
-Zelma

"Michael Tallhamer" wrote:
I am a medical physicist new to the .Net Framework and am trying to write a
simple app that will monitor the output from a serial port on one of my
linear accelerators using the new Serialport object and write the output to a
textBox in my application for viewing. I however keep getting this error and
am unable to figure out how to get around this.

System.InvalidO perationExcepti on was unhandled
Message="Cross-thread operation not valid: Control 'textBox1' accessed
from a thread other than the thread it was created on."
Source="System. Windows.Forms"

I have attached the code from my simple form for review in the event some
one can help me

Thank You
Michael Tallhamer

using System;
using System.Collecti ons.Generic;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows. Forms;
using System.IO;
using System.IO.Ports ;

namespace MorningCheckRea der
{
public partial class Form1 : Form
{
// Create the serial port with basic settings
private SerialPort port = new SerialPort("COM 1", 9600, Parity.None,
8,
StopBits.One);

public Form1()
{
InitializeCompo nent();
}

private void button1_Click(o bject sender, EventArgs e)
{
// Attach a method to be called when there is data waiting in
the port's
buffer
port.DataReceiv ed += new SerialDataRecei vedEventHandler

(port_DataRecei ved);

//Open the port
port.Open();
}

private void button2_Click(o bject sender, EventArgs e)
{
port.Close();
}

private void port_DataReceiv ed(object sender,
SerialDataRecei vedEventArgs e)
{

// Show all the incoming data in the port's buffer
if (textBox1.Text. Length > 0)
{
textBox1.Text = textBox1.Text.I nsert(textBox1. Text.Length -
1,

port.ReadExisti ng());
}
else
{
textBox1.Text = port.ReadExisti ng();
}
}
}
}
--
Mike

Feb 23 '06 #10

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

Similar topics

6
4842
by: Billy Jacobs | last post by:
I have a website which has both secure and non-secure pages. I want to uses forms authentication. How do I accomplish this? Originally I had my web.config file in the root with Forms Authentication set up and it worked just fine. Then I realized that I needed to have some pages unsecure. I then created 2 directories. One named Secure and the other named Public. I placed my web.config file in my
0
8376
by: grutta | last post by:
I am writing a windows service that will recieve notification when a USB Device is insterted into the machine. I have used the RegisterDeviceNotification and the RegisterServiceCtrlHandlerEx with a handler. The handler portion seems to fail indicating that my parameters are invalid. I am getting an error code 126 when i try to register the handler and 28 when i register for event notification. Any ideas as to what the deal is? I am...
5
2964
by: Dave | last post by:
I tried posting this in the WinForm forum and got no hits so I am trying it here. After inserting a new data row to a DataTable that is bound to a datagrid, I am unable to change data in a row that is after the newly added row without getting bizarre results. I have added the full code for the test below. Create a project drop in the code and run. There is nothing crazy about the code. I used the designer to add the dataset and to...
19
4115
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can easily understand a newbie using bound controls or someone with a tight deadline. I guess I need...
0
2190
by: Marco Segurini | last post by:
Hi, I am trying to dynamically install/deinstall a message handler to a System.Windows.Forms.Form using NativeWindow. I do not use IMessageFilter derived class because it intercept only the PostMessage messages. My problem is that when I install the message handler the second time a "Fatal stack overflow error" occurs.
3
5344
by: Rob | last post by:
Hi all, I am having trouble converting the code below (found on http://vbnet.mvps.org/index.html?code/core/sendmessage.htm) into a format that will work using vb .NET. Can anyone have a look at it and let me know what I need to change. I have tried changing the "hwnd" type into intptr's but there seem to be other problems too, like it won't allow "lParam As Any" to be declared.
3
2862
by: Siv | last post by:
Hi, A little while ago I wrote a small program that allowed the user to view products from a database. The database holds the details of the products which can be viewed via a form and additionally pictures of the product are stored in an images subfolder and the database holds the file name of the relevant picture. The user can then click a button to display the picture in a pop-up window and also another button to email the potential...
0
2779
by: Sister Ray | last post by:
I'm trying to create a simple form that sends an email using my company's exchange server. I'm using the System.Net.Mail Namespace of the .net framework 2.0. I've googled everywhere and i think my code is ok, however i keep getting a "A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond" error every time. Any...
19
248289
Atli
by: Atli | last post by:
Introduction At some point, all web developers will need to collect data from their users. In a dynamic web page, everything revolves around the users input, so knowing how to ask for and collect this data is essential to any developer. This article is a basic tutorial on how to user HTML Forms, the most common method of data collection. Assumptions - Basic HTML knowledge. - Basic PHP knowledge. HTML Forms A common and simple way of...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10315
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.