472,119 Members | 1,656 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Serial port visibility in user control

I'm a newbie that is still struggling with OOP concepts & how to make things
work they way I want. Using Visual C# Express, I have a form in which I added
a user control to display a graph, based upon data received via the serial
port. If I run the serial port in the main form code, I can get data and,
using public properties of the user control, transfer the data to be shown on
the graph. However, I am trying to add a feature that will poll the external
device via the serial port and update the graph within the user control.
However, when I try to add a serial port command (such as
serialPort1.WriteLine()) within the user control code I get an error:
"The name 'serialPort1' does not exist in the current context"
How do I make the serial port control "visible" to my user control?
Thanks,
Dave T.
Nov 7 '06 #1
7 5172
You are thinking about it backwards. A UserControl should be told what to
do, not how to do it. You should create methods on the usercontrol like
AddData() that others will call when they want to put data on the graph. The
UserControl will be resposible for getting them to the specific control.
Users of the UserControl do not need to know that it has a graph control on
it. That could change in the future, and if you don't change the AddData()
interface, nothing will care. This is the beauty of OO. I hope that makes
sense/answers the question.

"davetelling" wrote:
I'm a newbie that is still struggling with OOP concepts & how to make things
work they way I want. Using Visual C# Express, I have a form in which I added
a user control to display a graph, based upon data received via the serial
port. If I run the serial port in the main form code, I can get data and,
using public properties of the user control, transfer the data to be shown on
the graph. However, I am trying to add a feature that will poll the external
device via the serial port and update the graph within the user control.
However, when I try to add a serial port command (such as
serialPort1.WriteLine()) within the user control code I get an error:
"The name 'serialPort1' does not exist in the current context"
How do I make the serial port control "visible" to my user control?
Thanks,
Dave T.
Nov 7 '06 #2

"davetelling" <da*********@discussions.microsoft.comwrote in message
news:57**********************************@microsof t.com...
I'm a newbie that is still struggling with OOP concepts & how to make
things
work they way I want. Using Visual C# Express, I have a form in which I
added
a user control to display a graph, based upon data received via the serial
port. If I run the serial port in the main form code, I can get data and,
using public properties of the user control, transfer the data to be shown
on
the graph. However, I am trying to add a feature that will poll the
external
device via the serial port and update the graph within the user control.
However, when I try to add a serial port command (such as
serialPort1.WriteLine()) within the user control code I get an error:
"The name 'serialPort1' does not exist in the current context"
How do I make the serial port control "visible" to my user control?
One way is to add another public property to your user control, and have the
main form set it to the serial port.

However, I think you should re-evaluate your software design, because
UserControl should typically be responsible for user interface and not
device I/O.
Thanks,
Dave T.

Nov 7 '06 #3

davetelling wrote:
>..I get an error:
"The name 'serialPort1' does not exist in the current context"
How do I make the serial port control "visible" to my user control?
I agree with the other posters that this may not be wise. However, to
answer your question, you could make it a static public control on the
main form, or you can simply pass it in as a parameter and assign it to
a local member field in the user control.

Option 1 in main form:

using System.IO.Ports;

public static SerialPort SharedSerialPort = new SerialPort("Com1",
115200, Parity.None, 8, StopBits.One);

You don't have to set the parameters in the static initializer; it
depends on how you want to handle the comm params. You can even hold
off on the "new" and do it later (I'd init it to null in this case).

Nov 8 '06 #4
Brian, Ben, Eric;
I see what you are saying. Obviously, this whole OO thing is new and
confusing to me, as I tend to think of a serial port as a resource ANYTHING
should be able to work with.
Anyhow, the reason I am trying to add serial port access to the method that
refreshes my graph is that it seems to be a "looping" process (i.e. if I run
the app in debig mode and just step through, it continually comes back to the
paint function) and I need something like this to continually update the
graph. However, when I tried this in my main form, I (obviously!) lock up the
app, as once it starts sending & receiving serial data, it stops responding
to anything else. So, how do I write a loop that also looks for a specific
button click to break out of it? I could not find any way to write code that
says something like, "If you see that this button was clicked, break out of
this loop"
The help files in the Visual C# have been virtually useless to me, as pretty
much every single isue with which I have had problems either has not been
addressed, or may have been, but I just couldn't phrase the query correctly
to find the answer. This forum is about th eonlyplace I've found help, and I
appreciate it very much!
Anyhow, what I want to be able to do is click a button, which starts the
loop that does the serial port interface, and if click the same button again,
will break out of the loop and just go back to the main function. The first
part has been easy - I have an event handler that calls th eloop when I click
the button, but once th eloop starts, I haven't found anything that lets me
get out gracefully.
TIA for the help!

"davetelling" wrote:
I'm a newbie that is still struggling with OOP concepts & how to make things
work they way I want. Using Visual C# Express, I have a form in which I added
a user control to display a graph, based upon data received via the serial
port. If I run the serial port in the main form code, I can get data and,
using public properties of the user control, transfer the data to be shown on
the graph. However, I am trying to add a feature that will poll the external
device via the serial port and update the graph within the user control.
However, when I try to add a serial port command (such as
serialPort1.WriteLine()) within the user control code I get an error:
"The name 'serialPort1' does not exist in the current context"
How do I make the serial port control "visible" to my user control?
Thanks,
Dave T.
Nov 8 '06 #5
It sounds like you are looking for an interruptable threading system... I
would hazard that BackgroundWorker would meet your needs...

have a looksee here; yes it is VB, but we'll forgive it... look in
particular at how SumNumbers loops checking for cancellation - very similar
scenario, but perhaps while instead of for...

http://www.ondotnet.com/pub/a/dotnet...html?page=last

Marc
Nov 8 '06 #6
Marc,
I'll take a look at the link you sent & see if it helps with my app.
Thanks!

"Marc Gravell" wrote:
It sounds like you are looking for an interruptable threading system... I
would hazard that BackgroundWorker would meet your needs...

have a looksee here; yes it is VB, but we'll forgive it... look in
particular at how SumNumbers loops checking for cancellation - very similar
scenario, but perhaps while instead of for...

http://www.ondotnet.com/pub/a/dotnet...html?page=last

Marc
Nov 8 '06 #7

davetelling wrote:
Anyhow, the reason I am trying to add serial port access to the method that
refreshes my graph is that it seems to be a "looping" process (i.e. if I run
the app in debig mode and just step through, it continually comes back to the
paint function) and I need something like this to continually update the
graph. However, when I tried this in my main form, I (obviously!) lock up the
app, as once it starts sending & receiving serial data
You're likely using the event-driven option to receive new data from
the port, right?

The other poster here is correct that a threading architecture is
ideal, but I'm fairly sure that's beyond your current experience level.

But you already have threading in your application, and you don't
realize it. The thread that receives new data and calls your event
handler is a background thread, and this is why your UI is freezing.
You can't allow background threads to update your UI.

The easy fix to to use Control.Invoke to automatically marshal the data
from your received event over to your current UI thread, where you can
safely process the data. This Google query will make your day:
c# "windows forms" threading control.invoke

If you'll be using a high baud rate, however, this isn't the best
approach. Windows XP buffers the incoming data and it doesn't fire that
received event often enough to let you stream data at 115K baud (which
is what I frequently do). To solve this, you need to poll for data
inside a loop. The code would be a little harder because you have to
use DoEvents and avoid re-entrancy unless you want to roll your own
threads..

Eric

Nov 9 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ^CeFoS^ | last post: by
3 posts views Thread by rusttree | last post: by
6 posts views Thread by Peter Krikelis | last post: by
13 posts views Thread by Al the programmer | last post: by
1 post views Thread by henrycortezwu | last post: by
15 posts views Thread by xkenneth | last post: by
2 posts views Thread by evle | last post: by
13 posts views Thread by Rob | last post: by
reply views Thread by leo001 | last post: by

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.