Click here to Skip to main content
11,813,379 members (70,600 online)
Rate this: bad
Please Sign up or sign in to vote.
I am making an attempt to design a Windows application in c# that communicates through a serial port. The project will have a connection select screen, a main form with a menu bar and multiple dialogs connected to each menu item. The flow of the application would look like this:

The connection select dialog will be shown on the MainForm_Load event and capture the serial port name.

When a menu items is selected, the associated command will be sent through the serial port and the dialog will show the response data.

I am having trouble with the basic c# concepts coming from c++.

How can I create just one serial class that can share data between all the dialogs?
How can I access the serial read response from my dialog and display that data in a text field or a more meaningful way?

When I draw this up, it appears simple, but I do not know how to access data from the serial port to display in each dialog.
Posted 15-Aug-12 10:24am
Wes Aday at 15-Aug-12 16:54pm
I think that what you are groping for here (from your comments) is create a class to handle your serial communication and then create some delegate functions that your dialogs can subscribe to, to get information and notifications.
dsandru1 at 15-Aug-12 16:56pm
Do you have more information on this I can look into? It sounds like what I am after.
Wes Aday at 15-Aug-12 17:02pm
Go through this and start reading. Start simple and work your way up.
Kenneth Haugland at 15-Aug-12 17:06pm
Yes, that is precisely what I meant :) Thank you Wes. It seems to be an easier way of doing this without delagates though...

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

dsandru1 at 15-Aug-12 16:36pm
I have read through this, but it doesn't help me with my application because it is for the console not a Windows Form. I am trying to create a class that the other Forms can access, and then pull data from that class to post on the calling Form.
Kenneth Haugland at 15-Aug-12 16:42pm
It dosent matter if its a consol or Windows application, the same code for the serial port should still work. You should perhaps choose a setting dialog as the startup window, and call the main form when the settings are ok.

What is the trouble in creating a serialport class anyway?
dsandru1 at 15-Aug-12 16:46pm
I guess the trouble isn't creating the serial class, it is finding a way to access the data in the class through multiple forms. For instance in one dialog I might have serialPort.Send(0x10), but I would have had created a new instance of serialPort without the settings selected during start-up. Also when I go to retrive the data, it is always null. *In my dialog code* textbox1.Text = serialPort.message;
Kenneth Haugland at 15-Aug-12 16:48pm
Think you'll have to add handlers in the Serial class if thats going to work
dsandru1 at 15-Aug-12 16:50pm
This is where I start getting confused. I am used to being able to pass pointers to objects around, but I don't know where to begin with handlers.
Kenneth Haugland at 15-Aug-12 16:53pm
Take a look here:

You basically would have to have a handler around the read and write sub/function to be abe to reach them from several forms.
Kenneth Haugland at 15-Aug-12 16:58pm
Meaning you'll have to raise an event when the serial port is finished reading the port, and that event can be picked up in another form.
dsandru1 at 15-Aug-12 16:57pm
I'll try to take a look at this.
Sergey Alexandrovich Kryukov at 15-Aug-12 17:44pm
And then please accept the answer formally (green button) -- it is correct.
Clifford Nelson at 15-Aug-12 17:09pm
My 5
Kenneth Haugland at 15-Aug-12 17:16pm
Thanks :)
Sergey Alexandrovich Kryukov at 15-Aug-12 17:44pm
Correct, a 5.
Kenneth Haugland at 15-Aug-12 17:45pm
Thanks Sergey :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Abhinav S 270
1 CHill60 180
2 KrunalRohit 154
3 Palash Mondal_ 145
4 Sergey Alexandrovich Kryukov 140
0 OriginalGriff 3,877
1 KrunalRohit 2,496
2 CPallini 2,240
3 Maciej Los 2,205
4 ppolymorphe 1,750

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100