[code]
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO.Ports;
- namespace WindowsFormsApplication1
- {
- public partial class MainForm : Form
- {
- public MainForm()
- {
- InitializeComponent();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.PortName = "COM1";
- serialPort1.BaudRate = 9600;
- serialPort1.DataBits = 8;
- serialPort1.Parity = Parity.None;
- serialPort1.StopBits = StopBits.One;
- serialPort1.Open();
- disconnectBtn.Enabled = true;
- connectBtn.Enabled = false;
- }
- catch
- {
- MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- if (connectBtn.Enabled == false)
- {
- textBox1.Text = "Connected";
- }
- }
- private void disconnectBtn_Click(object sender, EventArgs e)
- {
- try
- {
- serialPort1.Close();
- connectBtn.Enabled = true;
- disconnectBtn.Enabled = false;
- }
- catch
- {
- MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- if (disconnectBtn.Enabled == false)
- {
- textBox1.Text = "Disconnected";
- }
- }
- private void button1_Click_1(object sender, EventArgs e)
- {
- serialPort1.Write(new byte[] {0x55}, 0, 1); //OK
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Class1 sp = new Class1();
- sp.serialOut();
- }
- }
- }
[/code]
But, when i click button2 link to class2 to call serial port, it does not work.
[code]
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO.Ports;
- namespace WindowsFormsApplication1
- {
- class Class1
- {
- public void serialOut()
- {
- MainForm _mainForm = new MainForm();
- _mainForm.serialPort1.Open(); //UnauthorizedAccessException was unhandle
- _mainForm.serialPort1.Write(new byte[] { 0x55 }, 0, 1); //InvalidOperationException was unhandle
- }
- }
- }
[/code]
I had set the serialPort1 to public in properties.
How can i solve this? Thank you.

FroglegRank #1,020 · 1.9K reputationPosted Mar 12, 2011, 11:28 PM EST
FroglegRank #1,020 · 1.9K reputationPosted Mar 10, 2011, 3:09 AM EST
namespace WindowsFormsApplication1
{
public partial class MainForm : Form
{
public static MainForm Fm1 = null; // creates single instance
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Fm1 = this;
}
private void button1_Click(object sender, EventArgs e)
{
try
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;
serialPort1.Open();
disconnectBtn.Enabled = true;
connectBtn.Enabled = false;
}
catch
{
MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (connectBtn.Enabled == false)
{
textBox1.Text = "Connected";
}
}
private void disconnectBtn_Click(object sender, EventArgs e)
{
try
{
serialPort1.Close();
connectBtn.Enabled = true;
disconnectBtn.Enabled = false;
}
catch
{
MessageBox.Show("Error", "Unable To Access", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
if (disconnectBtn.Enabled == false)
{
textBox1.Text = "Disconnected";
}
}
private void button1_Click_1(object sender, EventArgs e)
{
serialPort1.Write(new byte[] {0x55}, 0, 1); //OK
}
private void button2_Click(object sender, EventArgs e)
{
Class1 sp = new Class1();
sp.serialOut();
}
}
}
[/code]
But, when i click button2 link to class2 to call serial port, it does not work.
[code]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
namespace WindowsFormsApplication1
{
class Class1
{
public void serialOut()
{
MainForm.Fm1.serialPort1.Open(); //UnauthorizedAccessException was unhandle
MainForm.Fm1.serialPort1.Write(new byte[] { 0x55 }, 0, 1); //InvalidOperationException was unhandle
}
}
}
This code works with serial port set to public, but with a bit more code you can set it back to private
hello leong12 reputationPosted Mar 11, 2011, 4:29 PM EST
But, another problem,
MainForm.Fm1.serialPort1.Open(); //NullRefenceException was unhandle
MainForm.Fm1.serialPort1.Write(new byte[] { 0x55 }, 0, 1); //NullRefenceException was unhandle
How to create new instance for this? Thanks
FroglegRank #1,020 · 1.9K reputationPosted Mar 11, 2011, 9:17 PM EST
public partial class MainForm : Form
{
public static MainForm Fm1 = null; // creates single instance<<<<<<<<<
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Fm1 = this;//<<<<<<<<<<<<<<<
}
Suthish NairRank #92 · 22.8K reputationPosted Mar 12, 2011, 7:57 AM EST
hello leong12 reputationPosted Mar 12, 2011, 8:20 AM EST
FroglegRank #1,020 · 1.9K reputationPosted Mar 12, 2011, 8:47 AM EST
VulpesRank #55 · 29.2K reputationPosted Mar 12, 2011, 8:52 AM EST
I am therefore puzzled why this exception has now gone away and you're getting a null reference exception on this line:
MainForm.Fm1.serialPort1.Open(); //NullRefenceException was unhandled
On the face of it, it's simply not possible for either Fm1 to be null (given the nature of Frogleg's amendments) or for serialPort1 (assuming you've added it from the VS designer) to be null. Do you perhaps have another variable called MainForm in Class1 which is hiding the class of the same name?
Sam HobbsRank #107 · 20.5K reputationPosted Mar 12, 2011, 10:48 AM EST
hello leong12 reputationPosted Mar 12, 2011, 6:46 PM EST
PS. Scan before open the attach file. Thanks.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 12, 2011, 7:43 PM EST
FroglegRank #1,020 · 1.9K reputationPosted Mar 12, 2011, 10:29 PM EST
It's now working on my machine
hello leong12 reputationPosted Mar 13, 2011, 3:25 AM EDT
FroglegRank #1,020 · 1.9K reputationPosted Mar 13, 2011, 11:43 AM EDT
hello leong12 reputationPosted Mar 15, 2011, 4:28 AM EDT