473,756 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Wri teLine()) 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 5394
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.

"davetellin g" 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.Wri teLine()) 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

"davetellin g" <da*********@di scussions.micro soft.comwrote in message
news:57******** *************** ***********@mic rosoft.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.Wri teLine()) 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 SharedSerialPor t = new SerialPort("Com 1",
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!

"davetellin g" 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.Wri teLine()) 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 BackgroundWorke r 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 BackgroundWorke r 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
9094
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 same port to change the direction of the robot. The trajectory frame is managed by an applet, and the project works good when the applet is called by a html document allocated in the same local machine under W98 where the classes and the serial port...
3
4589
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 micro-controller chip had several pins, some of which were for output and some were for input. The crux of the project was to make the program set the ouput pins to high or low to drive the servos and motors and read the input pins that were attached to various...
6
6983
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
4833
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 been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is connected to a PC through serial ports. Our customer wants to control the hardware from a remote...
4
11206
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. What I would like to do is make the serial port accessible via a web service. The web service and the legacy application would be running on the same machine. The mobile application would access the web service via a network connection. It...
1
10919
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 errors 2) My Nokia 6600 prompted me the message "Accept Connection request from DEMON?" note: DEMON is my computers name. 3) Using my Nokia 6600, I hit the button that refers to the "Yes"
15
8192
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 transactions occur? I'm trying to reverse engineer a microcontroller serial routine and I'd like to see any response the chip sends back. Regards, Ken
2
3792
by: evle | last post by:
haw to read data from an Infrared Infrared Remote Control
13
6209
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 am sending a short message to a group of embedded boxes daisy chained via the serial port. When I send a 'global' message, all the connected units should reply with their Id and Ack in this format '0 Ack' To be certain that I didn't miss a...
0
9455
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...
0
10031
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9869
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9838
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,...
1
7242
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
6534
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
5140
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3805
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
3
2665
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.