| Author |
Message |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Thu Jun 01, 2006 8:02 am Post subject: changing serial port properties from another form |
|
|
i have a serial port control on 1 form
that form is my main. it uses the control to communicate with a device (surprise surprise )
i have another form which is my setup form to configure the serial port control (baudrate and com port number)
i have problems though to change the settings of the control itself, with the values which are entered in the setup form
in vb6 (which is where i come from) i could do (in form2) form1.mscomm1.port=1
i am sure its quite simple what i want .. but i fail to do it
(even a public variable which i use to configure my control doesnt work as it doesnt 'see' the public variable from the other form) |
|
| Back to top |
|
 |
Sentax Scruffy
Joined: 22 Oct 2004 Posts: 208
|
Posted: Thu Jun 01, 2006 10:41 am Post subject: |
|
|
Boy its been a while since I opened up .net but I remember something about forms are considered classes in vb.net (most likely C# also) and you have to declare the code inside the form a public class. Then you can access that forms objects from throughout your project.
If it doesn't work don't get mad at me  |
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Thu Jun 01, 2006 7:29 pm Post subject: |
|
|
Sentax is right, forms are classes. Forms are classes in vb6 as well. I'm not sure if it works in C# but i think in VB.NET they brought back the ability to access a form instance by using its name.
if you have a public variable in a form then you will need a reference to the form to get to it. You could declare public variables in a module and have the settings form set those and the other form use them. you could also create the settings form as a dialog and add properties (or public variables) to it:
| Code: |
frmSettings frm = New frmSettings();
if (frm.ShowDialog() == DialogResults.OK)
{
somevalue = frm.Baudrate;
someothervalue = frm.PortNumber;
}
|
you'll have to excuse my attempt at C#  |
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
Posted: Fri Jun 02, 2006 12:05 am Post subject: |
|
|
If you save those settings to the Application Settings, you could then use databinding on your serial port control to the relevent application settings. Saves code.  |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Fri Jun 02, 2006 1:01 am Post subject: |
|
|
| *narf* wrote: | you could also create the settings form as a dialog and add properties (or public variables) to it:
| Code: |
frmSettings frm = New frmSettings();
if (frm.ShowDialog() == DialogResults.OK)
{
somevalue = frm.Baudrate;
someothervalue = frm.PortNumber;
}
|
|
i already called the form as dialog .. but i am stumped how to create a property or how to use the public variable
this is my new attempt
| Code: |
namespace MobTerm
{
public partial class frmMobTerm : Form
{
string strPort = "COM1";
int intBaud = 115200;
public void setPort(string strNewPort)
{
strPort = strNewPort;
}
public frmMobTerm()
{
InitializeComponent();
InitPort();
}
private void InitPort()
{
if (comPort.IsOpen) comPort.Close();
comPort.PortName=strPort;
comPort.Close();
comPort.BaudRate=intBaud;
comPort.Parity=System.IO.Ports.Parity.None;
comPort.DataBits=8;
comPort.StopBits=System.IO.Ports.StopBits.One;
comPort.Open();
}
private void mnuSetup_Click(object sender, EventArgs e)
{
SetupForm frmSetup = new SetupForm();
if (frmSetup.ShowDialog() == DialogResult.OK) InitPort();
}
}
}
|
what am i doing wrong ?  |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Fri Jun 02, 2006 1:03 am Post subject: |
|
|
| bdi wrote: | If you save those settings to the Application Settings, you could then use databinding on your serial port control to the relevent application settings. Saves code.  |
wouah! wha ?
application settings ? can i change them from code so the user can change them ? where can i find those ? |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Fri Jun 02, 2006 1:06 am Post subject: |
|
|
| Sentax wrote: | Boy its been a while since I opened up .net but I remember something about forms are considered classes in vb.net (most likely C# also) and you have to declare the code inside the form a public class. Then you can access that forms objects from throughout your project.
|
*nod* thats what i am trying .. but i guess i am declaring the variables in the wrong place .. i think i tried all though
i will try something else now .. i will add a new class to my project/solution and see if i can do it through that one  |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Fri Jun 02, 2006 5:16 am Post subject: |
|
|
found it!!
(although i dont understand )
| Code: |
namespace MobTerm
{
public partial class frmMobTerm : Form
{
public static string strPort = "COM1";
public static int intBaud = 115200;
|
the keyword 'static' did the trick .. although i wonder why  |
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Fri Jun 02, 2006 6:04 am Post subject: |
|
|
| static makes it a class member not an instance member. therefore you can access it directly: frmMobTerm.strPort. However, since this is a class member there will only ever be one of them. If you had multiple instances of frmMobTerm they would all be using the same variable/value. |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Fri Jun 02, 2006 6:55 am Post subject: |
|
|
ah ok .. that explains it indeed
(i only knew 'static' as 'once it has a value it will keep it until its changed' )
thanks!
i now have my serial communication program changed from vb6 to c# and i can even run it on my pda
i should call it narfterm  |
|
| Back to top |
|
 |
Sentax Scruffy
Joined: 22 Oct 2004 Posts: 208
|
Posted: Fri Jun 02, 2006 8:03 am Post subject: |
|
|
| Hrqls wrote: | i now have my serial communication program changed from vb6 to c# and i can even run it on my pda  |
I tried to get the serial control to work from vb6 to vb.net a couple of years ago but it was always giving me access violation errors. I found a C# project that I could include in my solution and then use that to read the serial port. It isn't dependent on the serial control and access the hardware directly I assume. I should share for everyone to see.
Are you using .net 2005? Maybe that was the problem cause I was using the first version of .net so they may have fixed this in 2005. Amazing they didn't include a serial control in .net to use. I guess microsoft assumed that programmers don't need to use the serial port anymore and should begin to learn the usb. Yeah that is nice but there is still devices/products out there that only still communicate via serial. In my line of work which is AV all the devices that I can connect to and control are controlled via serial. So yeah I was a little upset. Some of these compatibility issues has kept me working in vb6 for so long. Oh well everything still works well in vb6 and I can do all that I need to with it.
Thats my rant.
Glad you got it to work! |
|
| Back to top |
|
 |
bdi Nobody
Joined: 21 Oct 2004 Posts: 1646 Location: Chicago
|
Posted: Fri Jun 02, 2006 11:51 am Post subject: |
|
|
To use the application settings:
Right click on the project and select Properties.
On the left tabs click on Settings.
Give your setting a name, a type, scope, and a default value. (scope is if it belongs to the specific user or is the same no matter who runs the program).
Now, in your program you can use it like a variable. Call it by using:
Properties.Settings.Default.<setting name>
If you set it to a new value, and want to persist it to the next time you run the program, at some point you will need to add:
Properties.Settings.Default.Save();
to save the new settings. I would do that in the OK button on the setup form. |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Wed Jun 07, 2006 7:51 am Post subject: |
|
|
| Sentax wrote: | | Are you using .net 2005? Maybe that was the problem cause I was using the first version of .net so they may have fixed this in 2005. Amazing they didn't include a serial control in .net to use. |
when i converted my vb6 project to vb.net (2005) it included a mscomm control with the references to it .. it worked almost the same
i had to use it on a pda with windows mobile 5.0 ... which didnt offer the mscomm control they included in the other project
there is a new control 'serial port' which you can use on windows mobile 5.0 as well
i used that one ... and it works!
| Quote: | | I guess microsoft assumed that programmers don't need to use the serial port anymore and should begin to learn the usb. Yeah that is nice but there is still devices/products out there that only still communicate via serial. In my line of work which is AV all the devices that I can connect to and control are controlled via serial. |
me too .. i communicate with a lot of devices .. all with serial port .. none with usb
the pda did only have an usb cable by the default package though .. so we had to order a separate rs232 (serial) cable .. but its working
the only problem is that we cant recharge the pda at the same time as we use the rs232 cable .. (while we can do that with the usb cable as its a dual connector .. but they dont have that connector for rs232 cables ) .. they are trying to kill rs232 for sure! |
|
| Back to top |
|
 |
Hrqls De Blonde Puffin
Joined: 22 Oct 2004 Posts: 562 Location: Alkmaar, The Netherlands
|
Posted: Wed Jun 07, 2006 7:55 am Post subject: |
|
|
| bdi wrote: | To use the application settings:
Right click on the project and select Properties.
On the left tabs click on Settings.
Give your setting a name, a type, scope, and a default value. (scope is if it belongs to the specific user or is the same no matter who runs the program).
|
thats nice! i wish i could use that .. nice ordered list for which i normally used a module (and should have used a class )
i dont have a 'Settings' in my left menu though .. i am using visual studio 2005 professional
| Quote: |
Now, in your program you can use it like a variable. Call it by using:
Properties.Settings.Default.<setting name>
If you set it to a new value, and want to persist it to the next time you run the program, at some point you will need to add:
Properties.Settings.Default.Save();
to save the new settings. I would do that in the OK button on the setup form. |
thats nice and easy indeed  |
|
| Back to top |
|
 |
|