473,414 Members | 1,627 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,414 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 5342
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
3
by: rusttree | last post by:
Many moons ago, I took a class in embedded control at school. The course focused on a micro-controller mounted on a small electric car that was programmed using simple C code. The...
6
by: Peter Krikelis | last post by:
Hi All, I am having a problem setting up input mode for serial communications. (Sorry about the long code post). The following code is what I use to set up my comm port.
13
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have...
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. ...
1
by: henrycortezwu | last post by:
Hi All, I'm trying to connect to a virtual port (COM19, OUTGOING, "Bluetooth Serial Port") using VS2005 System.IO.Ports. When I ran the ff code below here's what happens. 1) VS2005 Compiles w/o...
15
by: xkenneth | last post by:
Hi, I'm writing a couple python applications that use the serial port (RS-232) quite extensively. Is there any way I can monitor all activity on the serial port and have it printed as the...
2
by: evle | last post by:
haw to read data from an Infrared Infrared Remote Control
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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 project—planning, coding, testing,...
0
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...
0
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...

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.